昨晚我花了5个小时试图弄清楚如何在Xamarin Forms中更新标签值,但是没有进展。
我的xaml代码如下:
按钮在页面第一次加载时正确显示文本为“选择声音”。但是,当我回到这个页面时,如果application.current.properties键值存在,则无法更新文本。 有人知道为什么标签似乎只在页面第一次加载时更新,而不是持续更新。更重要的是,有人能提供代码,在按钮文本最初设置后更新它吗?
我的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