Xamarin Forms-更新标签值 [英] Xamarin Forms - update a label value

查看:233
本文介绍了Xamarin Forms-更新标签值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨晚我花了5个小时试图弄清楚如何在Xamarin Forms中更新标签值,但是却无济于事.

Last night I spent 5 hours trying to work out how to update a label value in Xamarin Forms but got nowhere.

我的xaml看起来像这样:

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>

我的主页类如下:

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键值时,它无法更新此文本.

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?

推荐答案

您必须在homepage类中实现INotifyPropertyChanged才能更新页面.

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

}

,但我建议实现MVVM pattern并定义一个ViewModel.

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

这篇关于Xamarin Forms-更新标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆