简体   繁体   中英

Xamarin Forms - update a label value

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?

You must implement INotifyPropertyChanged in your homepage class to be able to update the page.

public partial class HomePage : ContentPage,  INotifyPropertyChanged
{

        private string selectedSound;
        public string SelectedSound
        {
            get
            {
                return selectedSound;
            } set
            {
                if (selectedSound!=value)
                {
                    selectedSound = value;
                    this.OnPropertyChanged("SelectedSound");
                }
            }
        }

   .....

public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName){
        if (PropertyChanged != null {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));}}

}

but I would recommend to implement MVVM pattern and define a ViewModel instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM