WPF使用命令来减慢UI控件的更新 [英] WPF Using commands to slow to update UI controls

查看:154
本文介绍了WPF使用命令来减慢UI控件的更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过命令属性将命令绑定到按钮,并在放置的页面中执行命令绑定。在execute方法中,创建一个包含后台worker的类的实例,然后启动它(这是一个长任务)。后台worker(bw)类包含一个变量isRunning,它在执行DoWork方法之前设置为true,在执行RunWorkerCompleted时设置为false。因此,从放置按钮的页面的代码后面,在CanExecute方法中,如果bw没有运行(isRunning = false),则将e.canExecute设置为true,如果isRunning = true,则将e.canExecute设置为false。

I bind a command to a button through command property and doing a command binding in the page it is placed. In execute method I create an instance of a class that contains the background worker and I start it (this is a long task). The background worker (bw) class contains a variable isRunning that it is set to true before DoWork method is executed and to false when RunWorkerCompleted is executed. So from the code behind of the page where button is placed, in CanExecute method I set e.canExecute to true if bw is no running (isRunning = false), and e.canExecute to false if isRunning = true.

当我按下按钮,它启动bw进程很长时间,按钮被禁用。确定这是正确的,但是当后台工作人员(bw)完成后,按钮不会返回到启用,直到我再次按下它。当它被禁用,我按(当bw完成时)它被启用。为什么按钮在bw结尾时未自动返回启用?

WHen I press the button, it launch bw process for a long time and the button gets disabled. Ok this is correct, but when background worker (bw) finishes, the button doesn't returns to enabled until I press it again. When it is disabled and I press (when bw is finished) it gets enabled. Why the button is not returning automatically to enabled at the end of the bw?

我的代码片段:

<Page  x:Class="GParts.Pages.MyPage" 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"       
   xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;
    assembly=PresentationFramework.Aero"            
    xmlns:local="clr-namespace:GParts"      
    Loaded="Page_Loaded"  
    Unloaded="Page_Unloaded" 
    Height="Auto">

  <Page.CommandBindings>
    <CommandBinding Command="{x:Static local:Pages.MyPage.rcmd}"
               Executed="CommandBinding_Executed"
               CanExecute="CommandBinding_CanExecute"/>
  </Page.CommandBindings>

   <...>
   <Button Command="{x:Static local:Pages.MyPage.rcmd}" />

<...>
</Page>

页面后面的代码:

 namespace GParts.Pages
 {

   public partial class MyPage : Page
   {

    public static RoutedCommand rcmd = new RoutedCommand();

    private cBgWorker bw;

    <...>

    // ExecutedRoutedEventHandler for the custom button remove all command.
    private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {

            // get an isntance of the background worker class
            bw = new cBgWorker();

            // start the long task
            bw.StartTask();

    }

    // CanExecuteRoutedEventHandler for the custom button remove all command.
    private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        // bw is the instance of background worker class
        if (bw == null)
        {
            e.CanExecute = true;
        }
        else
        {
            e.CanExecute = !bw.isRunning;// isRunning indicates if bw is
                                         //executing now         
        }
    }

    <...>

} // end class
} // end namespace


推荐答案

当BackgroundWorker完成后,请调用 CommandManager.InvalidateRequerySuggested();

When your BackgroundWorker completes, call CommandManager.InvalidateRequerySuggested();

仅由WPF偶尔请求。否则,在每个 ICommand 实现上不断调用CanExecute会产生大量的开销。调用上述方法会迫使CommandManager立即更新。

By default, Commands are only requeried occasionally by WPF. Otherwise, there would be a huge amount of overhead in constantly calling "CanExecute" on every ICommand implementation. Calling the above method forces the CommandManager to update immediately.

这将强制命令重新启用/禁用。

This will force the Commands to re-enable/disable appropriately.

这篇关于WPF使用命令来减慢UI控件的更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆