问题
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