static void BindEvents(NavigationPage page) { page.Popped += (s, args) => { TinyPubSub.Unsubscribe(args.Page); TinyPubSub.Unsubscribe(args.Page.BindingContext); if (args.Page is MultiPage <Page> multipage) { foreach (var child in multipage.Children) { TinyPubSub.Unsubscribe(child); TinyPubSub.Unsubscribe(child.BindingContext); } } }; page.PoppedToRoot += (s, args) => { var poppedToRootEventArgs = args as PoppedToRootEventArgs; foreach (var poppedPage in poppedToRootEventArgs.PoppedPages) { TinyPubSub.Unsubscribe(poppedPage); TinyPubSub.Unsubscribe(poppedPage?.BindingContext); } }; }
/// <summary> /// Scans an object after attributes to hook up to TinyPubSub /// </summary> /// <param name="obj">The object to scan</param> public static void Register(object obj) { //// TODO: EB: Move the reflection code to a separate type, for performance - add a cache (ConcurrentDictionary) to scanned objects and use expressions to invoke the subscriber methods instead of method.Invoke().. var typeInfo = IntrospectionExtensions.GetTypeInfo(obj.GetType()); foreach (var method in typeInfo.DeclaredMethods) { var attributes = method.GetCustomAttributes(typeof(TinySubscribeAttribute)).OfType <TinySubscribeAttribute>(); foreach (var attribute in attributes) { var channel = attribute.Channel; var methodParameters = method.GetParameters(); if (methodParameters.Length > 0) { // Concept code for subscriptions var firstParam = methodParameters.First(); var paramType = firstParam.ParameterType; // EB: Not used TinyPubSub.Subscribe <object>(obj, channel, (data) => method.Invoke(obj, new object[] { data })); } else { // Register without parameters since the target method has none TinyPubSub.Subscribe(obj, channel, () => method.Invoke(obj, null)); } } } }
public static void SubscribeOnMainThread <T>(object owner, string channel, Action <T> action) { TinyPubSub.Subscribe <T>(owner, channel, (obj) => { Device.BeginInvokeOnMainThread(() => action(obj)); }); }
public static void Init(Application app) { TinyPubSub.Clear(); app.PropertyChanged += App_PropertyChanged; }
public static void Deregister(object obj) { TinyPubSub.Unsubscribe(obj); }