Example #1
0
        private static async Task <TTarget> FrameNavigate <TTarget>(TTarget targetViewModel, Type type, Windows.UI.Xaml.Controls.Frame frame) where TTarget : class, IViewModel
        {
            var parameter = new StageNavigationContext <TTarget>()
            {
                ViewModel = targetViewModel
            };
            var t   = new TaskCompletionSource <object>();
            var dip = EventRouting.EventRouter.Instance
                      .GetEventChannel <NavigationEventArgs>()

                      .Where(e =>
                             object.ReferenceEquals(e.EventData.Parameter, parameter))
                      .Subscribe(e =>
            {
                var page = e.Sender as MVVMPage;

                if (parameter.ViewModel != null)
                {
                    page.ViewModel = parameter.ViewModel;
                }
                else
                {
                    var solveV = page.GetDefaultViewModel();
                    if (solveV != null)
                    {
                        targetViewModel = parameter.ViewModel = (TTarget)solveV;
                    }
                }

                if (targetViewModel == null)
                {
                    targetViewModel = (TTarget)page.ViewModel;
                }



                parameter.ViewModel = targetViewModel;

                if (!t.Task.IsCompleted)
                {
                    t.SetResult(null);;
                }
            });


            frame.Navigate(type, parameter);
            await t.Task;

            dip.DisposeWith(targetViewModel);
            return(targetViewModel);
        }
Example #2
0
        private static async Task <TTarget> FrameNavigate <TTarget>(TTarget targetViewModel, Type type, Windows.UI.Xaml.Controls.Frame frame) where TTarget : class, IViewModel
        {
            var parameter = new StageNavigationContext <TTarget>()
            {
                ViewModel = targetViewModel
            };
            var t   = new TaskCompletionSource <object>();
            var dip = EventRouting.EventRouter.Instance
                      .GetEventChannel <NavigationEventArgs>()

                      .Where(e =>
                             object.ReferenceEquals(e.EventData.Parameter, parameter))
                      .Subscribe(e =>
            {
                Page page  = null;
                IView view = null;
                switch (e.Sender)
                {
                case MVVMPage mvvmPage:
                    page = mvvmPage;
                    view = mvvmPage;
                    break;

                case PageViewDisguise disguise:
                    page = disguise.AssocatedObject;
                    view = disguise;
                    break;

                default:
                    break;
                }


                if (parameter.ViewModel != null)
                {
                    view.ViewModel = parameter.ViewModel;
                }
                else
                {
                    var solveV = view.GetDefaultViewModel();
                    if (solveV != null)
                    {
                        targetViewModel = parameter.ViewModel = (TTarget)solveV;
                    }
                }

                if (targetViewModel == null)
                {
                    targetViewModel = (TTarget)view.ViewModel;
                }

                view.ViewModel = parameter.ViewModel = targetViewModel;
                targetViewModel?.OnPageNavigatedTo(e.EventData);
                t.TrySetResult(null);
            });

            frame.Navigate(type, parameter);
            await t.Task;

            dip.DisposeWith(targetViewModel);
            return(targetViewModel);
        }