Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anyone know a good example of ReactiveCommand for ReactiveUI?

I'm inexperienced, especially at MVVM, but trying to use ReactiveUI, and I'm not understanding the examples that I'm finding that demonstrate ReactiveCommand. I have used ICommand / DelegateCommand one time before, but this is different, and I'm not getting it.

What I'm trying to do is really simple. Click a button in the view, and have that execute a method in the view model. The examples that I'm finding all involve IObservable<>, and I don't get that, as they don't explanations that are geared to the total noob that I am.

Basically, I'm trying to use this as a learning experience, and what I'd ideally like to do is bind the button's Command property in xaml to a command (however that works, I don't know), which causes a method to execute. No collections, I'd just be passing a single int variable.

Thanks for the help. I really appreciate it.

Edit - Below appears code using Paul Betts' suggestions:

C#

public ReactiveCommand AddToDailyUsed { get; protected set; }

public MainPageVM()
{
    Initialize();
    AddToDailyUsed = new ReactiveCommand();
    AddToDailyUsed.Subscribe(AddToTodayUsedAction => this.AddToDailyUsedExecuted());
}

private object AddToDailyUsedExecuted()
{
    MessageBox.Show("AddToDailyUsedAction");
    return null;
}

private void AddToDailyUsedAction(object obj)
{
    MessageBox.Show("AddToDailyUsedAction");
}

XAML

<Button Content="{Binding Strings.add, Source={StaticResource LocalStrings}}"
        Command="{Binding AddToTodayUsed}"
        Margin="-5,-10, -10,-10"
        Grid.Row="3"
        Grid.Column="2" />

Obviously I'm missing something. I inserted break points at the AddToDailyUsedExecuted and AddToDailyUsedAction methods, and they are never reached.

Edit Constructor for code behind the view:

MainPageVM mainPageVM = new MainPageVM();

public MainPage()
{
    InitializeComponent();
    Speech.Initialize();
    DataContext = mainPageVM;
    ApplicationBar = new ApplicationBar();
    TaskRegistration.RegisterScheduledTask();

    this.Loaded += new RoutedEventHandler(MainPage_Loaded);

    //Shows the rate reminder message, according to the settings of the RateReminder.
    (App.Current as App).rateReminder.Notify();
}
like image 894
Rich Hopkins Avatar asked Feb 10 '13 04:02

Rich Hopkins


1 Answers

So, ReactiveCommand is itself an IObservable<object> - in this case, you can conceptualize IObservable as an Event - this Event fires when the command is invoked (i.e. when the button is pressed). So, in your constructor, you might write:

MyCommand = new ReactiveCommand();
MyCommand.Subscribe(param => this.MyCommandHasExecuted());

However, what's neat about IObservable that isn't true about regular events, is that you can use LINQ on them:

// Now, MyCommandHasExecuted only gets run when the UserName isn't null
MyCommand.Where(param => this.UserName != null)
    .Subscribe(param => this.MyCommandHasExecuted());

Update: Your Xaml binds to AddToTodayUsed but your ViewModel command is called AddToDailyUsed. Could that be it?

like image 164
Ana Betts Avatar answered Oct 23 '22 16:10

Ana Betts