Xamarin Forms - 更新标签值

3
昨晚我花了5个小时试图弄清楚如何在Xamarin Forms中更新标签值,但是没有进展。
我的xaml代码如下:
<?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>

我的主页类看起来像这样:

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";
        }
    }
}
}

按钮在页面第一次加载时正确显示文本为“选择声音”。但是,当我回到这个页面时,如果application.current.properties键值存在,则无法更新文本。 有人知道为什么标签似乎只在页面第一次加载时更新,而不是持续更新。更重要的是,有人能提供代码,在按钮文本最初设置后更新它吗?

你正在绑定到 SelectSoundButtonText,但你的属性名称是 SelectedSound。它是如何工作的? - esiprogrammer
抱歉,那是一个打字错误(已在问题中更新)。问题仍然存在。第一次加载页面时,按钮文本出现了,但之后就再也没有更新过了。 - Charlie S
1个回答

4

为了能够更新页面,您需要在homepage类中实现INotifyPropertyChanged接口。

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));}}

}

但我建议实现MVVM模式并定义一个ViewModel


刚刚尝试在XAML中设置按钮的“x:Name”属性。然后在我的代码中引用该名称以获取按钮对象。然后更新文本值,一切都正常工作,无需PropertyChanged代码。这是一个有效的替代方案吗? - Charlie S
2
你总是可以使用代码后台更改你的对象。x:name 会生成一个字段,你可以在代码后台中使用它。但这不是一种聪明的实现方式。正如我所说,强烈建议使用数据绑定代替。使用像 MVVM 这样的模式是一个加分项。 - esiprogrammer
啊,好的。为什么MVVM更好呢? - Charlie S
1
为什么选择MVVM,它的核心优势是什么? - esiprogrammer
这真的很有趣。非常感谢你。我来自Xcode(MVC)背景,所以感觉比我习惯的要花费更多的努力,但我很欣赏对额外抽象化的理解。 - Charlie S

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接