INotifyPropertyChanged 仅在方法存在时更新 UI
INotifyPropertyChanged only updates UI when method exists
我需要更新 Button_Click 方法中的 UI。
当我在 Test = "sss" 之后放置一个 break stop 时; ui 不会更新,但是当我继续直到方法完成时,它会更新 UI.
这是一个仅用于演示的简单场景,但在另一个示例中,我有一个 for 循环正在评估一长串对象,因此在整个 for 循环的评估过程中有多个更新。
如何在方法内更新ui而不是等到结束....
xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<TextBox Text="{Binding Path=Test}"></TextBox>
<Button Click="Button_Click">Click</Button>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private string test;
public string Test { get { return test; } set { test = value; OnPropertyChanged("Test"); } }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Test = "aaa";
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Test = "sss";
//do other stuff here
}
}
让我们使用异步方法,参见示例:
private async void Button_Click(object sender, RoutedEventArgs e)
{
Test = "sss";
// text box updated immediately via property Test
await Task.Delay(5000); // wait 5 sec.
Test = "ttt";
// after 5 sec. updated again
}
我需要更新 Button_Click 方法中的 UI。
当我在 Test = "sss" 之后放置一个 break stop 时; ui 不会更新,但是当我继续直到方法完成时,它会更新 UI.
这是一个仅用于演示的简单场景,但在另一个示例中,我有一个 for 循环正在评估一长串对象,因此在整个 for 循环的评估过程中有多个更新。
如何在方法内更新ui而不是等到结束....
xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<TextBox Text="{Binding Path=Test}"></TextBox>
<Button Click="Button_Click">Click</Button>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private string test;
public string Test { get { return test; } set { test = value; OnPropertyChanged("Test"); } }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Test = "aaa";
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Test = "sss";
//do other stuff here
}
}
让我们使用异步方法,参见示例:
private async void Button_Click(object sender, RoutedEventArgs e)
{
Test = "sss";
// text box updated immediately via property Test
await Task.Delay(5000); // wait 5 sec.
Test = "ttt";
// after 5 sec. updated again
}