执行其他操作之前的WPF更新UI

时间:2016-02-02 21:43:00

标签: c# .net wpf xaml caliburn.micro

当我更改正在显示的视图时,我正在使用WPF,Caliburn.Micro和WPF Extended Toolkit来显示忙碌指示符。问题是繁忙的指示器永远不会显示在被切换的视图之间(这就是我需要它的原因)。

破碎时的工作原理

  1. 点击菜单项
  2. 设置IsBusy = true
  3. 没有显示忙碌指示
  4. 更改视图(ChangeActiveItem(ViewModel,bool))
  5. 查看显示
  6. 显示忙碌指示
  7. 应如何运作

    1. 点击菜单项
    2. 设置IsBusy = true
    3. 忙碌指示符显示
    4. 更改视图
    5. 查看完整显示和渲染
    6. IsBusy = false
    7. BusyIndi​​cator隐藏

      ShellView(BusyIndi​​cator代码段)

      <xctk:BusyIndicator IsBusy="{Binding IsBusy}">
          <ContentControl x:Name="ActiveItem" />
      </xctk:BusyIndicator>
      

      NavigationViewModel(方法 - 点击菜单项时触发)

      public void NavigationItemClicked(NavigationItem item)
      {
          _eventAggregator.PublishOnUIThread(new NavigationItemClicked(item.ViewModelType));
      }
      

      ShellViewModel(方法 - 从NavigationViewModel.NavigationItemClicked捕获消息)

      public void Handle(NavigationItemClicked message)
      {
          worker = new BackgroundWorker();
      
          worker.DoWork += (s, e) =>
          {
              ChangeActiveItem(QuestDetails, true);
              IsBusy = false;
          };
      
          IsBusy = true;
          worker.RunWorkerAsync();
      }
      

      还尝试

      1. 将'PublishOnUiThread'更改为'PublishOnBackgroundThread'
      2. Ayyappan Subramanian建议

1 个答案:

答案 0 :(得分:0)

  • 在runworkercompleted
  • 中更改isBusy状态
  • 制造isbusy公共财产
  • 在页面构造函数中添加datacontext
  • IsBusy =“{Binding IsBusy,UpdateSourceTrigger = PropertyChanged}”

    public void Handle(NavigationItemClicked message)
     {
     worker = new BackgroundWorker();
    
     worker.DoWork += (s, e) =>
      {
        ChangeActiveItem(QuestDetails, true);
    IsBusy = false;
    };
     worker.RunWorkerCompleted += (s, e) =>
     {
    
      IsBusy = true;
     };
    
     worker.RunWorkerAsync();
    }