Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单击事件中的任务不处理wpf中与UI相关的更改_Wpf_User Interface_Buttonclick - Fatal编程技术网

单击事件中的任务不处理wpf中与UI相关的更改

单击事件中的任务不处理wpf中与UI相关的更改,wpf,user-interface,buttonclick,Wpf,User Interface,Buttonclick,我有一个按钮,有一个复杂的API调用。所以我把那部分代码放在任务工厂里。我相应地设置了进度条的可见性。但是在点击时,进度条不可见。不知道我哪里出错了 private RelayCommand<string> _selectGenderCommand; public RelayCommand<string> SelectGenderCommand { get { return _selectGenderCommand

我有一个按钮,有一个复杂的API调用。所以我把那部分代码放在任务工厂里。我相应地设置了进度条的可见性。但是在点击时,进度条不可见。不知道我哪里出错了

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());
          }));
    }
}

为什么进度条不显示?UI线程应该正常工作?

您是否遵循MVVM概念?请发布您的ProgressBar绑定XAML。如果您使用的是.NET 4.5+,那么我建议您通过Task.Run而不是Task.Factory.StartNew启动新任务。我怀疑使用Task与此有关。最可能的罪魁祸首是错误的绑定-在调试模式下检查错误的路径输出,或者不正确/缺少INotifyPropertyChanged实现/用法。ProgressBarVisibility属性的设置如下:private Visibility\u ProgressBarVisibility;公共可见性ProgressBarVisibility{get{return}ProgressBarVisibility;}set{ProgressBarVisibility=value;RaisePropertyChanged=>ProgressBarVisibility;}}}@Grx70和@dymanoid:进度条是一个自定义的进度条,是一个用户控件。它在xaml中是这样引用的:``