Last night I spent 5 hours trying to work out how to update a label value in Xamarin Forms but got nowhere.
My xaml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Meditation.HomePage">
<ContentPage.Content>
<Button Text="{Binding SelectedSound}" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="OnButtonClicked" />
</ContentPage.Content>
My main page class looks like this:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Meditation
{
public partial class HomePage : ContentPage
{
public String SelectedSound { get; set; }
public HomePage()
{
InitializeComponent();
this.Title = AppInfo.AppName;
}
protected override void OnAppearing()
{
ShowSelectedSound();
BindingContext = this;
}
protected override void OnDisappearing()
{
DependencyService.Get<IAudio>().StopAudio();
}
// User Actions
void OnButtonClicked(object sender, EventArgs args)
{
Navigation.PushAsync(new SoundSelectionPage());
}
// Private
private void ShowSelectedSound()
{
if (Application.Current.Properties.ContainsKey(AppInfo.keySoundSelected))
{
SelectedSound = Application.Current.Properties[AppInfo.keySoundSelected] as string;
}
else
{
this.SelectedSound = "Choose sound";
}
}
}
}
The button correctly shows the text as 'Choose sound' when the page first loads. However, when I come back to this page it fails to update the text, when the application.current.properties key value exists.
Anyone know why the label only seems to update when the page first loads and not ongoing. And more importantly can anyone provide the code to get the button text to update after it has initially set?