ReactiveUI

ReactiveUI

Use the Reactive Extensions for .NET to create elegant, testable User Interfaces that run on any mobile or desktop platform. Supports Xamarin.iOS, Xamarin.Android, Xamarin.Mac, WPF, Windows Forms, Windows Phone 8 and Windows Store apps.

Download via NuGet »


public class SearchViewModel : ISearchViewModel
{
    public ReactiveList<SearchResults> SearchResults { get; set; }

    private string searchQuery;
    public string SearchQuery {
        get { return searchQuery; }
        set { this.RaiseAndSetIfChanged(ref searchQuery, value); }

    }

    public ReactiveCommand<List<SearchResults>> Search { get; set; }

    public ISearchService SearchService { get; set; }
}

A Compelling Example

public SearchViewModel(ISearchService searchService = null) : ReactiveObject, IRoutableViewHost
{
    SearchService = searchService ?? Locator.Current.GetService<ISearchService>();

    // Here we're describing here, in a *declarative way*, the conditions in
    // which the Search command is enabled.  Now our Command IsEnabled is
    // perfectly efficient, because we're only updating the UI in the scenario
    // when it should change.
    var canSearch = this.WhenAny(x => x.SearchQuery, x => !String.IsNullOrWhiteSpace(x.Value));

    // ReactiveCommand has built-in support for background operations and
    // guarantees that this block will only run exactly once at a time, and
    // that the CanExecute will auto-disable and that property IsExecuting will
    // be set according whilst it is running.
    Search = ReactiveCommand.CreateAsyncTask(canSearch, async _ => {
        return await searchService.Search(this.SearchQuery);
    });

    // ReactiveCommands are themselves IObservables, whose value are the results
    // from the async method, guaranteed to arrive on the UI thread. We're going
    // to take the list of search results that the background operation loaded, 
    // and them into our SearchResults.
    Search.Subscribe(results => {
        SearchResults.Clear();
        SearchResults.AddRange(results);
    });

    // ThrownExceptions is any exception thrown from the CreateAsyncTask piped
    // to this Observable. Subscribing to this allows you to handle errors on
    // the UI thread. 
    Search.ThrownExceptions
        .Subscribe(ex => {
            UserError.Throw("Potential Network Connectivity Error", ex);
        });

    // Whenever the Search query changes, we're going to wait for one second
    // of "dead airtime", then automatically invoke the subscribe command.
    this.WhenAnyValue(x => x.SearchQuery)
        .Throttle(TimeSpan.FromSeconds(1), RxApp.MainThreadScheduler)
        .InvokeCommand(this, x => x.SearchService);
}

Get Involved

Found an issue? Let us know! Want to help us improve ReactiveUI? Check out the source and our contributor's guide!