ton*_*oni 0 wpf performance command button
我通过命令属性将命令绑定到按钮,并在放置的页面中执行命令绑定.在execute方法中,我创建一个包含后台worker的类的实例,然后启动它(这是一个很长的任务).后台worker(bw)类包含一个变量isRunning,它在执行DoWork方法之前设置为true,在执行RunWorkerCompleted时设置为false.因此,从放置按钮的页面后面的代码,在CanExecute方法中,如果bw没有运行(isRunning = false),则将e.canExecute设置为true,如果isRunning = true,则将e.canExecute设置为false.
当我按下按钮时,它会长时间启动bw进程并且该按钮被禁用.确定这是正确的,但是当后台工作人员(bw)完成时,按钮不会返回启用,直到我再次按下它.当它被禁用时我按下(当bw完成时)它被启用.为什么按钮没有自动返回到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>
Run Code Online (Sandbox Code Playgroud)
页面背后的代码:
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
Run Code Online (Sandbox Code Playgroud)
当BackgroundWorker完成后,请致电 CommandManager.InvalidateRequerySuggested();
默认情况下,命令仅偶尔由WPF重新获取.否则,在每次ICommand实现时不断调用"CanExecute"会产生大量开销.调用上述方法会强制CommandManager立即更新.
这将强制命令适当地重新启用/禁用.
| 归档时间: |
|
| 查看次数: |
1815 次 |
| 最近记录: |