PropertyChangedEventHandler not firing asynchronically

后端 未结 1 1372
没有蜡笔的小新
没有蜡笔的小新 2021-01-28 18:24

I\'ve implemented this property to hide and show an element:

private bool isWorking = false;

public bool IsWorking {
    get {
        return this.isWorking;
           


        
相关标签:
1条回答
  • 2021-01-28 18:58

    You probably want to have an async Click event handler like this:

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        viewModel.IsWorking = true;
    
        await Task.Run(() =>
        {
           // do some long processing
        });
    
        viewModel.IsWorking = false;
    }
    

    Or an async method in your view model

    public async Task DoSomeProcessing()
    {
        IsWorking = true;
    
        await Task.Run(() =>
        {
           // do some long processing
        });
    
        IsWorking = false;
    }
    

    that is called from an async Click handler like

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        await viewModel.DoSomeProcessing();
    }
    
    0 讨论(0)
提交回复
热议问题