Skip to content

Adding a busy label to one of the commands #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions ReactiveUI.Winforms.Samples.Commands/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace ReactiveUI.Winforms.Samples.Commands.ViewModels
Expand All @@ -8,17 +10,24 @@ 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.Create(Parameterless);
ParameterlessCommand = ReactiveCommand.CreateFromTask(Parameterless);

// Create command with parameter
WithParameterCommand = ReactiveCommand.Create<string>(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
Expand All @@ -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)
Expand Down
16 changes: 14 additions & 2 deletions ReactiveUI.Winforms.Samples.Commands/Views/MainView.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ReactiveUI.Winforms.Samples.Commands/Views/MainView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down