What is the best way to use commands in WPF ?
I use some commands, thoses commands can take a time to execute. I want that my application not freeze while running but I want the features to be disabled.
there is my 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> and my 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 } I saw a solution with background worker but I don't know how to use it. And I wonder if I should create one instance by command.
Is there a cleaner/best way ?