0

I have a question regarding Xamarin forms. Is reactive extensions just an extension to existing .net library? does all mvvm frameworks(FreshMvvm, MvvmCross,MvvmLight) support it? Or is it a new mvvm framework?

TheDeveloper
  • 1,127
  • 1
  • 18
  • 55

1 Answers1

3

I can't answer if all of the MVVM frameworks support Rx, but I can definitely say that Rx is not a replacement for MVVM and you certainly wouldn't call it an MVVM framework.

Rx is a framework for composing collections of values that get pushed (like events do). It certainly can be used in an MVVM framework, again, like events can be used.

Here's a small example of using Rx to consume an event:

void Main()
{
    var foo = new Foo();
    var bars = Observable.FromEventPattern<int>(h => foo.Bar += h, h => foo.Bar -= h);
    var subsciption = bars.Subscribe(ep => Console.WriteLine(ep.EventArgs));
    foo.OnBar(42);
}

public class Foo
{
    public event EventHandler<int> Bar;
    public void OnBar(int i)
    {
        this.Bar?.Invoke(this, i);
    }
}

This will write 42 to the console.

But I could do this:

    var query =
        from b in bars
        where b.EventArgs > 21
        select b.EventArgs * 2;

    var subsciption = query.Subscribe(i => Console.WriteLine(i));

    foo.OnBar(42);
    foo.OnBar(7);
    foo.OnBar(20);
    foo.OnBar(21);
    foo.OnBar(22);

And now I have a more complex query that will write only 84 && 44 to the console.

This type of thing can be used in MVVM frameworks to make them more powerful.

And excellent example of an MVVM framework that makes strong use of Rx is http://reactiveui.net/. Give it a go.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Is there any specific circumstances where using reactiveUI makes more sense ? – TheDeveloper Jul 18 '16 at 17:35
  • @AzharAli - Makes more sense than what? More sense than using Rx? When you need an MVVM. More sense than another MVVM? When you are familiar with Rx it would be a fabulous choice. Can you clarify your question? – Enigmativity Jul 18 '16 at 22:00
  • [Here's a detailed article](http://fullstackmark.com/post/5/a-simple-vocabulary-app-using-reactiveui-and-xamarin-forms) that uses MVVM with ReactiveUI and Xamarin Forms. – mmacneil007 Feb 16 '17 at 23:11