Skip to content

Instantly share code, notes, and snippets.

@JimmyPun610
Last active July 15, 2020 00:45
Show Gist options
  • Save JimmyPun610/c245a4a3bc87489fb8f4bab549b8f2ae to your computer and use it in GitHub Desktop.
Save JimmyPun610/c245a4a3bc87489fb8f4bab549b8f2ae to your computer and use it in GitHub Desktop.

Content

Type of Subjects

Class Each next subscribers recevie...
Subject Only upcoming values
BehaviorSubject one previous value and upcoming values
ReplaySubject all previous value and upcoming values
AsyncSubject latest value and stream will close upcoming values

Replacing MessageCenter

Case : Subsribe a change

Approach by MessageCenter
//Subscribe
    MessagingCenter.Instance.Subscribe<string>("ObjectChange", "ObjectChange", (str) =>
            {
                //
                Your action
            });

//Notify change
    MessagingCenter.Instance.Send("ObjectChange", "ObjectChange");
Approach by RX
//Import library
using System.Reactive.Linq;
using System;
//Create Observable item
    public class ObservableObjects
    {
        public static ISubject<string> CurrentAppLanguageObservable = new Subject<string>();
    }

//Subscribe 
    ObservableObjects.CurrentAppLanguageObservable.Subscribe(str=>{
        //action on change
    });
    
//Notify change
    ObservableObjects.CurrentAppLanguageObservable.OnNext("Str");

Search Box search on TextChangedEvent with time control

Case : Create a search box which will search after no text changed in 1 second

  <!-- Create SearchBar in XAML -->
  <SearchBar x:Name="SearchBox"/>
   Observable.FromEventPattern(SearchBox, "TextChanged")
                .Do(sender =>
                {
                    var searchbar = (sender as SearchBar);
                    //Action will be done on every text change here
                })
                //Wait for 1 second
                .Throttle(new TimeSpan(0, 0, 1))
                .Subscribe(sender =>
                {
                    var searchbar = (sender as SearchBar);
                    //Do your search logic which is heavy load here
                });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment