In my code behind, I have an button click event handler to do some IO work with ConfigureAwait(false)
suffixed to the Task.
Now I would expect when the async
task is completed, it would not resume on the UI thread. So, TxtStatus.Text
assignment is expected to fail and it did.
Question is, if this TextBox is bound using a Model that implements INotifyPropertyChanged
then it works and the UI is updated.
Why is that possible, shouldn't the UI update fail as well?
The following code snippets are my current code with sufficient details to establish the context of my question.
View
<TextBox Name="TxtStatus" Text="{Binding Model.Status, Mode=TwoWay}/>
View's CodeBehind
private async void Button_Click(object sender, RoutedEventArgs e)
{
await _viewModel.DoWorkAsync().ConfigureAwait(false);
//TxtStatus.Text = "This does not work.";
_viewModel.Model.Status= "This works.";
}
Model
public class MyModel : INotifyPropertyChanged
{
private string _status;
public string Status
{
get => _status;
set
{
_status= value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}