Does anyone know a good example of ReactiveCommand for ReactiveUI?
Asked Answered
B

1

8

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();
}
Borchers answered 10/2, 2013 at 4:55 Comment(1)
This looks correct - can you show me the constructor of your CodeBehind for the view?Deandreadeane
D
8

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?

Deandreadeane answered 10/2, 2013 at 23:4 Comment(1)
Paul, thanks for the explanation. With no experience using Lambdas, I tried the code that I have added to my edited question. I inserted break points at both the AddToDailyUsedExecuted and AddToDailyUsedAction methods, and neither was ever reached. Can you tell me what I'm missing?Borchers

© 2022 - 2024 — McMap. All rights reserved.