-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathMainViewModel.cs
More file actions
68 lines (57 loc) · 2.29 KB
/
MainViewModel.cs
File metadata and controls
68 lines (57 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace ReactiveUI.Winforms.Samples.Commands.ViewModels
{
public class MainViewModel : ReactiveObject
{
private string _applicationTitle;
private string _withCanExecuteParameter;
private ObservableAsPropertyHelper<bool> _isBusy;
public bool IsBusy => _isBusy.Value;
public MainViewModel()
{
// Set properties
ApplicationTitle = "ReactiveUI Winforms Samples by Asesjix - Commands";
// Create parameterless command
ParameterlessCommand = ReactiveCommand.CreateFromTask(Parameterless);
// Create command with parameter
WithParameterCommand = ReactiveCommand.Create<string>(WithParameter);
// Create command with can execute
WithCanExecuteCommand = ReactiveCommand.Create(WithCanExecute,
this.WhenAnyValue(vm => vm.WithCanExecuteParameter).Select(s => string.IsNullOrEmpty(s) == false));
this.WhenAnyObservable(x => x.ParameterlessCommand.IsExecuting)
.ToProperty(this, y => y.IsBusy, out _isBusy);
}
public string ApplicationTitle
{
get => _applicationTitle;
set => this.RaiseAndSetIfChanged(ref _applicationTitle, value);
}
public string WithCanExecuteParameter
{
get => _withCanExecuteParameter;
set => this.RaiseAndSetIfChanged(ref _withCanExecuteParameter, value);
}
public ReactiveCommand ParameterlessCommand { get; }
public ReactiveCommand WithParameterCommand { get; }
public ReactiveCommand WithCanExecuteCommand { get; }
private async Task Parameterless()
{
await Task.Run(() =>
{
Thread.Sleep(3000);
MessageBox.Show("You pressed the button!", ApplicationTitle, MessageBoxButton.OK);
});
}
private void WithParameter(string message)
{
MessageBox.Show(message, ApplicationTitle, MessageBoxButton.OK);
}
private void WithCanExecute()
{
MessageBox.Show(WithCanExecuteParameter, ApplicationTitle, MessageBoxButton.OK);
}
}
}