【发布时间】:2017-06-10 11:50:55
【问题描述】:
在 WPF 中使用命令的最佳方式是什么?
我使用了一些命令,这些命令可能需要一些时间来执行。我希望我的应用程序在运行时不会冻结,但我希望禁用这些功能。
有我的 MainWindow.xaml :
<Window ...>
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<Button Grid.Row="0"
Grid.Column="0"
Style="{StaticResource StyleButton}"
Content="Load"
Command="{Binding LoadCommand}"/>
<Button Grid.Row="0"
Grid.Column="1"
Style="{StaticResource StyleButton}"
Content="Generate"
Command="{Binding GenerateCommand}"/>
</Grid>
</Window>
还有我的 MainViewModel.cs :
public class MainViewModel : ViewModelBase
{
#region GenerateCommand
#endregion
#region Load command
private ICommand _loadCommand;
public ICommand LoadCommand
{
get
{
if (_loadCommand == null)
_loadCommand = new RelayCommand(OnLoad, CanLoad);
return _loadCommand;
}
}
private void OnLoad()
{
//My code
}
private bool CanLoad()
{
return true;
}
#endregion
}
我看到了一个后台工作人员的解决方案,但我不知道如何使用它。我想知道我是否应该通过命令创建一个实例。
有没有更清洁/最好的方法?
【问题讨论】: