我的MainPage和设置上有TextBlock。我想动态更改此文本块的文本。我在设置中有按钮来确认更改:
private async void OK_Tapped(object sender, TappedRoutedEventArgs e)
{
_main.ChangeTbText(Common.NewName);
}
Common.NewName - 我的新文字;
public void ChangeTbText(string newName)
{
UserTextBlock.Text = newName;
}
它有效,但我必须刷新页面,我想在不刷新的情况下这样做。
编辑:
感谢您的回答,我创造了类似的东西:
设置中的:
public event PropertyChangedEventHandler PropertyChanged;
private async void OK_Tapped(object sender, TappedRoutedEventArgs e)
{
PropertyChanged += _main.ChangeUserName;
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() => OnPropertyChanged("Text")));
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
_main.ChangeUser(Common.NewName);
}
在主页上:
public void ChangeUser(string Text)
{
UserTextBlock.Text = Text;
}
调试此UserTextBlock.Text时设置正常,但我仍需要刷新页面才能看到它。