Task in Click event does not handle UI related changes wpf

泪湿孤枕 提交于 2019-12-12 03:23:37

问题


I have a button that has a complex API call. So I put that portion of code in Task Factory. I set the visibility of Progress Bar accordingly. But on the click, the progress bar doesnt get visible. Dont know where I am going wrong!

private RelayCommand<string> _selectGenderCommand;

public RelayCommand<string> SelectGenderCommand
{
    get
    {
        return _selectGenderCommand
            ?? (_selectGenderCommand = new RelayCommand<string>(
         p =>
          {
             ProgressBarVisibility = Visibility.Visible;
              Utilities utility = new Utilities();
              Task tskOpen = Task.Factory.StartNew(() =>
              {
                  if (p.Equals(Constants.GenderMale))
                  {
                      App.PatientMatchGender = Constants.GenderMale;
                  }
                  else if (p.Equals(Constants.GenderFemale))
                  {
                      App.PatientMatchGender = Constants.GenderFemale;
                  }
                  else
                  {
                      App.PatientMatchGender = Constants.GenderInterSex;
                  }

                  utility.MatchPatient(App.PatientMatchGender);

              }
            ).ContinueWith(t =>
            {
                Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => utility.PatientMatchForwardNavigation()));
                ProgressBarVisibility = Visibility.Hidden;
            }, TaskScheduler.FromCurrentSynchronizationContext());
          }));
    }
}

Why does the progress bar not show up? UI thread should be working right?

来源:https://stackoverflow.com/questions/28320641/task-in-click-event-does-not-handle-ui-related-changes-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!