如何在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
}
我看到了一个使用后台工作线程的解决方案,但我不知道如何使用它。我想知道是否应该通过命令创建一个实例。
有没有更简洁/更好的方法?