WPF使用命令将放缓至更新UI控件 [英] WPF Using commands to slow to update UI controls

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

问题描述

我的命令绑定,通过命令属性按钮,并做了命令,它被放置在页面绑定。在执行方法我创建一个包含后台工作的类的实例,我启动它(这是一项艰巨的任务)。背景工人(BW)类包含执行RunWorkerCompleted时执行的DoWork方法之前,它被设置为true和false一个变量isRunning。因此,从页面,按键被安置在CanExecute方法我设置e.canExecute为true,如果体重没有运行(isRunning = FALSE)背后的code和e.canExecute为false isRunning = TRUE。

当我preSS按钮,它推出体重的过程很长一段时间,按钮被禁用。好吧,这是正确的,但在后台工作(BW)完成后,按钮重新不会返回启用,直到我preSS它。当它被禁用,并且我preSS(当体重已完成),它就会被激活。为什么按钮没有自动返回到在体重年底启用?

我的code片断:

 <第x页:类=GParts.Pages.MyPage
  的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
   的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
   的xmlns:Microsoft_Windows_Themes =CLR的命名空间:Microsoft.Windows.Themes;
    装配= presentationFramework.Aero
    XMLNS:地方=CLR的命名空间:GParts
    加载=PAGE_LOADED
    卸载=Page_Unloaded
    身高=自动>  < Page.CommandBindings>
    <命令的CommandBinding ={X:静态地方:Pages.MyPage.rcmd}
               执行=CommandBinding_Executed
               CanExecute =CommandBinding_CanExecute/>
  < /Page.CommandBindings>   < ...>
   <按钮命令={X:静态地方:Pages.MyPage.rcmd}/>< ...>
< /页>

页面背后的code:

 命名空间GParts.Pages
 {   公共部分类我的页面:页
   {    公共静态的RoutedCommand RCMD =新的RoutedCommand();    私人cBgWorker体重;    < ...>    // ExecutedRoutedEventHandler自定义按钮删除所有命令。
    私人无效CommandBinding_Executed(对象发件人,ExecutedRoutedEventArgs E)
    {            //获取后台工作类的isntance
            体重=新cBgWorker();            //启动的长期任务
            bw.StartTask();    }    // CanExecuteRoutedEventHandler自定义按钮删除所有命令。
    私人无效CommandBinding_CanExecute(对象发件人,CanExecuteRoutedEventArgs E)
    {
        //体重是后台工作类的实例
        如果(BW == NULL)
        {
            e.CanExecute = TRUE;
        }
        其他
        {
            e.CanExecute = bw.isRunning; // isRunning指示体重是
                                         //现在执行
        }
    }    < ...>} //结束类
} //结束空间


解决方案

当您完成BackgroundWorker的,叫<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.input.commandmanager.invalidaterequerysuggested.aspx\"><$c$c>CommandManager.InvalidateRequerySuggested();

在默认情况下,命令只是偶尔重新查询WPF。否则,将是不断的呼唤CanExecute每的ICommand 实施巨大的开销量。调用上面的方法强制CommandManager可以立即更新。

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

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.

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?

my code snippet:

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

The code behind of the 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

解决方案

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

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天全站免登陆