ReactiveUI.RoutableViewModelMixin.WhenNavigatedTo C# (CSharp) Method

WhenNavigatedTo() public static method

This method allows you to set up connections that only operate while the ViewModel has focus, and cleans up when the ViewModel loses focus.
public static WhenNavigatedTo ( this This, Func onNavigatedTo ) : IDisposable
This this
onNavigatedTo Func Called when the ViewModel is navigated /// to - return an IDisposable that cleans up all of the things that are /// configured in the method.
return IDisposable
        public static IDisposable WhenNavigatedTo(this IRoutableViewModel This, Func<IDisposable> onNavigatedTo)
        {
            IDisposable inner = null;

            var router = This.HostScreen.Router;
            return router.NavigationStack.CountChanged.Subscribe(_ => {
                if (router.GetCurrentViewModel() == This) {
                    if (inner != null)  inner.Dispose();
                    inner = onNavigatedTo();
                } else {
                    if (inner != null) {
                        inner.Dispose();
                        inner = null;
                    }
                }
            });
        }