diff --git a/ReactiveUI.Winforms.Samples.Commands/ViewModels/MainViewModel.cs b/ReactiveUI.Winforms.Samples.Commands/ViewModels/MainViewModel.cs index 4eacca1..68dc929 100644 --- a/ReactiveUI.Winforms.Samples.Commands/ViewModels/MainViewModel.cs +++ b/ReactiveUI.Winforms.Samples.Commands/ViewModels/MainViewModel.cs @@ -1,4 +1,6 @@ using System.Reactive.Linq; +using System.Threading; +using System.Threading.Tasks; using System.Windows; namespace ReactiveUI.Winforms.Samples.Commands.ViewModels @@ -8,17 +10,24 @@ public class MainViewModel : ReactiveObject private string _applicationTitle; private string _withCanExecuteParameter; + private ObservableAsPropertyHelper _isBusy; + public bool IsBusy => _isBusy.Value; + public MainViewModel() { // Set properties ApplicationTitle = "ReactiveUI Winforms Samples by Asesjix - Commands"; // Create parameterless command - ParameterlessCommand = ReactiveCommand.Create(Parameterless); + ParameterlessCommand = ReactiveCommand.CreateFromTask(Parameterless); + // Create command with parameter WithParameterCommand = ReactiveCommand.Create(WithParameter); // Create command with can execute - WithCanExecuteCommand = ReactiveCommand.Create(WithCanExecute, + 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 @@ -37,9 +46,13 @@ public string WithCanExecuteParameter public ReactiveCommand WithParameterCommand { get; } public ReactiveCommand WithCanExecuteCommand { get; } - private void Parameterless() + private async Task Parameterless() { - MessageBox.Show("You pressed the button!", ApplicationTitle, MessageBoxButton.OK); + await Task.Run(() => + { + Thread.Sleep(3000); + MessageBox.Show("You pressed the button!", ApplicationTitle, MessageBoxButton.OK); + }); } private void WithParameter(string message) diff --git a/ReactiveUI.Winforms.Samples.Commands/Views/MainView.Designer.cs b/ReactiveUI.Winforms.Samples.Commands/Views/MainView.Designer.cs index b8236b2..4eed87f 100644 --- a/ReactiveUI.Winforms.Samples.Commands/Views/MainView.Designer.cs +++ b/ReactiveUI.Winforms.Samples.Commands/Views/MainView.Designer.cs @@ -34,6 +34,7 @@ private void InitializeComponent() this.tbWithCanExecuteParameter = new System.Windows.Forms.TextBox(); this.btWithCanExecute = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); + this.busyLabel = new System.Windows.Forms.Label(); this.SuspendLayout(); // // btParameterless @@ -87,18 +88,28 @@ private void InitializeComponent() this.label1.TabIndex = 5; this.label1.Text = "Required"; // - // ShellView + // busyLabel + // + this.busyLabel.AutoSize = true; + this.busyLabel.Location = new System.Drawing.Point(238, 43); + this.busyLabel.Name = "busyLabel"; + this.busyLabel.Size = new System.Drawing.Size(39, 13); + this.busyLabel.TabIndex = 6; + this.busyLabel.Text = "Busy..."; + // + // MainView // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.busyLabel); this.Controls.Add(this.label1); this.Controls.Add(this.tbWithCanExecuteParameter); this.Controls.Add(this.btWithCanExecute); this.Controls.Add(this.tbParameter); this.Controls.Add(this.btWithParameter); this.Controls.Add(this.btParameterless); - this.Name = "ShellView"; + this.Name = "MainView"; this.Text = "Form1"; this.ResumeLayout(false); this.PerformLayout(); @@ -113,6 +124,7 @@ private void InitializeComponent() private System.Windows.Forms.TextBox tbWithCanExecuteParameter; private System.Windows.Forms.Button btWithCanExecute; private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label busyLabel; } } diff --git a/ReactiveUI.Winforms.Samples.Commands/Views/MainView.cs b/ReactiveUI.Winforms.Samples.Commands/Views/MainView.cs index ec0c127..b394113 100644 --- a/ReactiveUI.Winforms.Samples.Commands/Views/MainView.cs +++ b/ReactiveUI.Winforms.Samples.Commands/Views/MainView.cs @@ -11,6 +11,7 @@ public MainView() this.WhenActivated(d => { + d(this.OneWayBind(ViewModel, vm => vm.IsBusy, v => v.busyLabel.Visible)); // Bind properties d(this.OneWayBind(ViewModel, vm => vm.ApplicationTitle, v => v.Text)); // Bind property for command with can execute