public virtual async Task <TResult> Navigate <TViewModel, TResult>(IMvxBundle presentationBundle = null, CancellationToken cancellationToken = default(CancellationToken))
            where TViewModel : IMvxViewModelResult <TResult>
            where TResult : class
        {
            var viewModel = (IMvxViewModelResult <TResult>)LoadViewModel <TViewModel>();
            var request   = new MvxViewModelInstanceRequest(viewModel)
            {
                PresentationValues = presentationBundle?.SafeGetData()
            };

            var args = new NavigateEventArgs(viewModel);

            OnBeforeNavigate(this, args);

            var tcs = new TaskCompletionSource <TResult>();

            viewModel.SetClose(tcs, cancellationToken);

            ViewDispatcher.ShowViewModel(request);
            await viewModel.Initialize();

            OnAfterNavigate(this, args);

            try
            {
                return(await tcs.Task);
            }
            catch
            {
                return(default(TResult));
            }
        }
        public virtual async Task <TResult> Navigate <TParameter, TResult>(IMvxViewModel <TParameter, TResult> viewModel, TParameter param, IMvxBundle presentationBundle = null)
            where TParameter : class
            where TResult : class
        {
            RunViewModelLifecycle(viewModel);
            var request = new MvxViewModelInstanceRequest(viewModel)
            {
                PresentationValues = presentationBundle?.SafeGetData()
            };

            var args = new NavigateEventArgs(viewModel);

            OnBeforeNavigate(this, args);

            var tcs = new TaskCompletionSource <TResult>();

            viewModel.SetClose(tcs);

            ViewDispatcher.ShowViewModel(request);
            await viewModel.Initialize(param);

            OnAfterNavigate(this, args);

            try
            {
                return(await tcs.Task);
            }
            catch
            {
                return(default(TResult));
            }
        }
Exemple #3
0
        protected virtual async Task Navigate <TParameter>(Type viewModelType, TParameter param) where TParameter : notnull
        {
            var request = new MvxViewModelInstanceRequest(viewModelType);

            request.ViewModelInstance = _viewModelLoader.LoadViewModel(request, param, null);
            await Navigate(request, request.ViewModelInstance).ConfigureAwait(false);
        }
Exemple #4
0
        protected virtual async Task Navigate(Type viewModelType)
        {
            var request = new MvxViewModelInstanceRequest(viewModelType);

            request.ViewModelInstance = _viewModelLoader.LoadViewModel(request, null);
            await Navigate(request, request.ViewModelInstance).ConfigureAwait(false);
        }
Exemple #5
0
        public async Task <TResult> Navigate <TViewModel, TResult>()
            where TViewModel : IMvxViewModelResult <TResult>
            where TResult : class
        {
            var args = new NavigateEventArgs(typeof(TViewModel));

            OnBeforeNavigate(this, args);

            var viewModel = (IMvxViewModelResult <TResult>)Mvx.IocConstruct <TViewModel>();
            var request   = new MvxViewModelInstanceRequest(viewModel);

            var tcs = new TaskCompletionSource <TResult>();

            viewModel.SetClose(tcs);

            _viewDispatcher.ShowViewModel(request);

            OnAfterNavigate(this, args);

            try
            {
                return(await tcs.Task);
            }
            catch
            {
                return(default(TResult));
            }
        }
        public virtual IMvxIosView CreateView(IMvxViewModel viewModel)
        {
            var request = new MvxViewModelInstanceRequest(viewModel);
            var view    = CreateView(request);

            return(view);
        }
Exemple #7
0
        protected async Task <MvxViewModelInstanceRequest> NavigationRouteRequest <TParameter>(
            string path, TParameter param, IMvxBundle?presentationBundle = null)
            where TParameter : notnull
        {
            ValidateArguments(path, param);

            if (!TryGetRoute(path, out var entry))
            {
                throw new MvxException($"Navigation route request could not be obtained for path: {path}");
            }

            var regex           = entry.Key;
            var match           = regex.Match(path);
            var paramDict       = BuildParamDictionary(regex, match);
            var parameterValues = new MvxBundle(paramDict);

            var viewModelType = entry.Value;

            var request = new MvxViewModelInstanceRequest(viewModelType)
            {
                PresentationValues = presentationBundle?.SafeGetData(),
                ParameterValues    = parameterValues.SafeGetData()
            };

            if (viewModelType.GetInterfaces().Contains(typeof(IMvxNavigationFacade)))
            {
                var facade = (IMvxNavigationFacade)_iocProvider.IoCConstruct(viewModelType);

                try
                {
                    var facadeRequest = await facade.BuildViewModelRequest(path, paramDict).ConfigureAwait(false);

                    if (facadeRequest == null)
                    {
                        throw new MvxException($"{nameof(MvxNavigationService)}: Facade did not return a valid {nameof(MvxViewModelRequest)}.");
                    }

                    request.ViewModelType = facadeRequest.ViewModelType;

                    if (facadeRequest.ParameterValues != null)
                    {
                        request.ParameterValues = facadeRequest.ParameterValues;
                    }

                    request.ViewModelInstance = ViewModelLoader.LoadViewModel(request, param, null);
                }
                catch (Exception ex)
                {
                    ex.MvxWrap($"{nameof(MvxNavigationService)}: Exception thrown while processing URL: {path} with RoutingFacade: {viewModelType}");
                }
            }
            else
            {
                request.ViewModelInstance = ViewModelLoader.LoadViewModel(request, param, null);
            }

            return(request);
        }
        public virtual async Task Navigate <TParameter>(IMvxViewModel <TParameter> viewModel, TParameter param, IMvxBundle presentationBundle = null) where TParameter : class
        {
            var request = new MvxViewModelInstanceRequest(viewModel)
            {
                PresentationValues = presentationBundle?.SafeGetData()
            };

            await Navigate <TParameter, object>(request, viewModel, param, presentationBundle);
        }
        public virtual async Task Navigate(IMvxViewModel viewModel, IMvxBundle presentationBundle = null)
        {
            var request = new MvxViewModelInstanceRequest(viewModel)
            {
                PresentationValues = presentationBundle?.SafeGetData()
            };

            await Navigate <object, object>(request, viewModel, null, presentationBundle);
        }
        public virtual async Task <TResult> Navigate <TResult>(IMvxViewModelResult <TResult> viewModel, IMvxBundle presentationBundle = null, CancellationToken cancellationToken = default(CancellationToken)) where TResult : class
        {
            var request = new MvxViewModelInstanceRequest(viewModel)
            {
                PresentationValues = presentationBundle?.SafeGetData()
            };

            return(await Navigate <object, TResult>(request, viewModel, null, presentationBundle, cancellationToken));
        }
        public virtual async Task Navigate <TViewModel>(IMvxBundle presentationBundle = null) where TViewModel : IMvxViewModel
        {
            var viewModel = LoadViewModel <TViewModel>();
            var request   = new MvxViewModelInstanceRequest(viewModel)
            {
                PresentationValues = presentationBundle?.SafeGetData()
            };

            await Navigate <object, object>(request, viewModel, null, presentationBundle);
        }
Exemple #12
0
        public MvxBasePresentationAttribute PresentationAttribute(MvxViewModelRequest request)
        {
            MvxViewModelInstanceRequest instanceRequest = request as MvxViewModelInstanceRequest;
            WindowViewModel             viewModel       = instanceRequest?.ViewModelInstance as WindowViewModel;

            return(new MvxWindowPresentationAttribute
            {
                Identifier = $"{nameof(WindowView)}.{viewModel}"
            });
        }
Exemple #13
0
        public virtual async Task <TResult> Navigate <TResult>(Type viewModel, IMvxBundle presentationBundle = null, CancellationToken cancellationToken = default)
            where TResult : notnull
        {
            var request = new MvxViewModelInstanceRequest(viewModel)
            {
                PresentationValues = presentationBundle?.SafeGetData()
            };

            request.ViewModelInstance = _viewModelLoader.LoadViewModel(request, presentationBundle, null);
            return(await Navigate <TResult>(request, (IMvxViewModelResult <TResult>) request.ViewModelInstance, cancellationToken).ConfigureAwait(false));
        }
        public MvxBasePresentationAttribute PresentationAttribute(MvxViewModelRequest request)
        {
            MvxViewModelInstanceRequest instanceRequest = request as MvxViewModelInstanceRequest;
            WatchdogViewModel           viewModel       = instanceRequest?.ViewModelInstance as WatchdogViewModel;

            return(new MvxContentPresentationAttribute
            {
                WindowIdentifier = $"{nameof(RootView)}.{viewModel?.ParentNo}",
                StackNavigation = false
            });
        }
Exemple #15
0
        private async Task <IMvxViewModel> NavigateRoute(string path)
        {
            KeyValuePair <Regex, Type> entry;

            if (!TryGetRoute(path, out entry))
            {
                return(null);
            }

            var regex     = entry.Key;
            var match     = regex.Match(path);
            var paramDict = BuildParamDictionary(regex, match);

            var viewModelType           = entry.Value;
            MvxViewModelRequest request = null;
            IMvxViewModel       viewModel;

            if (viewModelType.GetInterfaces().Contains(typeof(IMvxNavigationFacade)))
            {
                var facade = (IMvxNavigationFacade)Mvx.IocConstruct(viewModelType);

                try
                {
                    var facadeRequest = await facade.BuildViewModelRequest(path, paramDict);

                    viewModel = (IMvxViewModel)Mvx.IocConstruct(facadeRequest.ViewModelType);

                    if (facadeRequest == null)
                    {
                        Mvx.TaggedWarning("MvxNavigationService", "Facade did not return a valid MvxViewModelRequest.");
                        return(null);
                    }
                }
                catch (Exception ex)
                {
                    Mvx.TaggedError("MvxNavigationService",
                                    "Exception thrown while processing URL: {0} with RoutingFacade: {1}, {2}",
                                    path, viewModelType, ex);
                    return(null);
                }
            }
            else
            {
                viewModel = (IMvxViewModel)Mvx.IocConstruct(viewModelType);
            }

            request = new MvxViewModelInstanceRequest(viewModel)
            {
                ParameterValues = new MvxBundle(paramDict).SafeGetData()
            };
            _viewDispatcher.ShowViewModel(request);

            return(viewModel);
        }
        public virtual async Task <TResult> Navigate <TViewModel, TParameter, TResult>(TParameter param, IMvxBundle presentationBundle = null, CancellationToken cancellationToken = default(CancellationToken))
            where TViewModel : IMvxViewModel <TParameter, TResult>
            where TParameter : class
            where TResult : class
        {
            var viewModel = (IMvxViewModel <TParameter, TResult>)LoadViewModel <TViewModel>();
            var request   = new MvxViewModelInstanceRequest(viewModel)
            {
                PresentationValues = presentationBundle?.SafeGetData()
            };

            return(await Navigate <TParameter, TResult>(request, viewModel, param, presentationBundle, cancellationToken));
        }
        private void RestoreFragmentsInfoState(Bundle bundle)
        {
            if (bundle == null)
            {
                return;
            }

            var fragmentInfoParcelables = bundle.GetParcelableArray(_bundleFragmentsInfoKey);

            if (fragmentInfoParcelables == null)
            {
                return;
            }

            // First, we create a list of the ViewPager fragments that were restored by Android.
            var fragments = GetFragmentsFromBundle(bundle);

            // Now we get the FragmentInfo data for each fragment from the bundle.
            int i = 0;

            foreach (ViewPagerFragmentInfoParcelable parcelable in fragmentInfoParcelables)
            {
                MvxViewPagerFragmentInfo fragInfo = null;

                if (i < fragments.Count)
                {
                    var f = fragments[i];
                    if (f is IMvxFragmentView fragment && fragment.ViewModel != null)
                    {
                        // The fragment was already restored by Android with its old ViewModel (cached by MvvmCross).
                        // Add the ViewModel to the FragmentInfo object so the adapter won't instantiate a new one.
                        var viewModelInstanceRequest = new MvxViewModelInstanceRequest(fragment.ViewModel);
                        fragInfo = new MvxViewPagerFragmentInfo(parcelable.Title, parcelable.Tag, parcelable.FragmentType, viewModelInstanceRequest);
                    }
                }

                if (fragInfo == null)
                {
                    // Either the fragment doesn't exist or it doesn't have a ViewModel.
                    // Fall back to a FragmentInfo with the ViewModelType. The adapter will create a ViewModel in GetItem where we will add it to the FragmentInfo.
                    var viewModelRequest = new MvxViewModelRequest(parcelable.ViewModelType);
                    fragInfo = new MvxViewPagerFragmentInfo(parcelable.Title, parcelable.Tag, parcelable.FragmentType, viewModelRequest);
                }

                FragmentsInfo.Add(fragInfo);
                i++;
            }

            NotifyDataSetChanged();
        }
Exemple #18
0
        /// <summary>
        /// 在程序的Application的OnStartup方法中使用
        /// </summary>
        /// <param name="mvxViewPresenter">呈现控制器</param>
        /// <param name="mvxSetup">启动流程控制器</param>
        /// <param name="mvxApplication">ViewModelCore当中的</param>
        /// <param name="mvxAppStart">程序启动控制</param>
        /// <param name="viewAssemblies">视图程序集</param>
        /// <param name="viewModelAssemblies">ViewModel程序集</param>
        /// <param name="serviceAssemblies">提供服务工具的程序集,更具服务工具的接口注册为单例(服务的名称必须以Service结尾)</param>
        public static void Init <TMainWindow, TViewModel>(MvxWpfViewPresenter mvxViewPresenter = null,
                                                          IMvxWpfSetup mvxSetup          = null, IMvxApplication mvxApplication = null, IMvxAppStart mvxAppStart = null, Assembly[] viewAssemblies = null,
                                                          Assembly[] viewModelAssemblies = null, Assembly[] serviceAssemblies   = null) where TMainWindow : Window where TViewModel : MvxViewModel
        {
            var mainWindow = Activator.CreateInstance <TMainWindow>();

            if (mvxViewPresenter == null)
            {
                mvxViewPresenter = new MvxWpfViewPresenter(mainWindow);
            }

            if (mvxSetup == null)
            {
                mvxSetup = new BaseSetup <TMainWindow, TViewModel>(viewAssemblies, viewModelAssemblies);
            }

            mvxSetup.PlatformInitialize(Application.Current.Dispatcher, mvxViewPresenter);
            mvxSetup.InitializePrimary();
            mvxSetup.InitializeSecondary();

            if (serviceAssemblies != null)
            {
                foreach (var serviceAssembly in serviceAssemblies)
                {
                    serviceAssembly.CreatableTypes()
                    .EndingWith("Service")
                    .AsInterfaces()
                    .RegisterAsLazySingleton();
                }
            }

            var start = Mvx.IoCProvider.Resolve <IMvxAppStart>();

            //如果是默认的启动页,则不调用启动方法
            if (!(start is BaseAppStart <TViewModel>))
            {
                start.Start();
            }
            else
            {
                var request   = new MvxViewModelInstanceRequest(typeof(TViewModel));
                var viewModel = Mvx.IoCProvider.GetSingleton <IMvxViewModelLoader>().LoadViewModel(request, null);
                mainWindow.DataContext = viewModel;
            }

            mainWindow.Show();
        }
        public virtual async Task Navigate <TParameter>(IMvxViewModel <TParameter> viewModel, TParameter param, IMvxBundle presentationBundle = null) where TParameter : class
        {
            RunViewModelLifecycle(viewModel);
            var request = new MvxViewModelInstanceRequest(viewModel)
            {
                PresentationValues = presentationBundle?.SafeGetData()
            };

            var args = new NavigateEventArgs(viewModel);

            OnBeforeNavigate(this, args);

            ViewDispatcher.ShowViewModel(request);
            await viewModel.Initialize(param);

            OnAfterNavigate(this, args);
        }
        public virtual async Task Navigate <TViewModel>(IMvxBundle presentationBundle = null) where TViewModel : IMvxViewModel
        {
            var viewModel = LoadViewModel <TViewModel>();
            var request   = new MvxViewModelInstanceRequest(viewModel)
            {
                PresentationValues = presentationBundle?.SafeGetData()
            };

            var args = new NavigateEventArgs(viewModel);

            OnBeforeNavigate(this, args);

            ViewDispatcher.ShowViewModel(request);
            await viewModel.Initialize();

            OnAfterNavigate(this, args);
        }
Exemple #21
0
        public async Task Navigate <TViewModel, TParameter>(TParameter param)
            where TViewModel : IMvxViewModel <TParameter>
            where TParameter : class
        {
            var args = new NavigateEventArgs(typeof(TViewModel));

            OnBeforeNavigate(this, args);

            var viewModel = (IMvxViewModel <TParameter>)Mvx.IocConstruct <TViewModel>();

            var request = new MvxViewModelInstanceRequest(viewModel);

            _viewDispatcher.ShowViewModel(request);
            await viewModel.Initialize(param);

            OnAfterNavigate(this, args);
        }
Exemple #22
0
        public override void ViewDidLoad()
        {
            Request = new MvxViewModelInstanceRequest(Mvx.IocConstruct <FirstViewModel>());
            base.ViewDidLoad();

            ViewModel.GetCats();

            // Use a copy of the MvvmCross collection view source so we can debug in project.
            var source = new MvxCollectionViewSourceCopy(CollectionView, new NSString("collectionCellId"));

            CollectionView.Source = source;
            (CollectionView.CollectionViewLayout as UICollectionViewFlowLayout).ItemSize = new CGSize(View.Frame.Width / 2, (View.Frame.Width / 2) + 25f);

            // Create binding set
            var set = this.CreateBindingSet <FirstView, FirstViewModel>();

            set.Bind(source).For(x => x.ItemsSource).To(vm => vm.Cats);
            set.Apply();
        }
        public override Fragment GetItem(int position, Fragment.SavedState fragmentSavedState = null)
        {
            var fragment = base.GetItem(position, fragmentSavedState);

            // If the MvxViewPagerFragmentInfo for this position doesn't have the ViewModel, overwrite it with a new MvxViewPagerFragmentInfo that has the ViewModel we just created.
            // Not doing this means the ViewModel gets recreated every time the Fragment gets recreated!
            if (FragmentsInfo != null && FragmentsInfo.Count > position && fragment is IMvxFragmentView mvxFragment && mvxFragment.ViewModel != null)
            {
                var oldFragInfo = FragmentsInfo[position];

                if (oldFragInfo != null && !(oldFragInfo.Request is MvxViewModelInstanceRequest))
                {
                    var viewModelInstanceRequest = new MvxViewModelInstanceRequest(mvxFragment.ViewModel);
                    var newFragInfo = new MvxViewPagerFragmentInfo(oldFragInfo.Title, oldFragInfo.Tag, oldFragInfo.FragmentType, viewModelInstanceRequest);
                    FragmentsInfo[position] = newFragInfo;
                }
            }

            return(fragment);
        }
        public MiniPlayerView()
        {
            InitializeComponent();

            if (!(ViewModel is MiniPlayerViewModel))
            {
                if (Mvx.IoCProvider.TryResolve <MiniPlayerViewModel>(out var miniPlayerViewModel))
                {
                    ViewModel = miniPlayerViewModel;
                    return;
                }

                var _viewModelLoader = Mvx.IoCProvider.Resolve <IMvxViewModelLoader>();
                var request          = new MvxViewModelInstanceRequest(typeof(MiniPlayerViewModel));
                request.ViewModelInstance = _viewModelLoader.LoadViewModel(request, null);
                ViewModel = request.ViewModelInstance as MiniPlayerViewModel;

                Mvx.IoCProvider.RegisterSingleton <MiniPlayerViewModel>(ViewModel);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MenuView" /> class.
        /// </summary>
        public MenuView()
        {
            this.InitializeComponent();

            // when viewmodel already created
            if (Mvx.IoCProvider.TryResolve <Notenverwaltung.WPF.UI.ViewModels.MenuViewModel>(out var someViewModel))
            {
                ViewModel = someViewModel;

                return;
            }

            // creating viewmodel
            var _viewModelLoader = Mvx.IoCProvider.Resolve <IMvxViewModelLoader>();
            var request          = new MvxViewModelInstanceRequest(typeof(Notenverwaltung.WPF.UI.ViewModels.MenuViewModel));

            request.ViewModelInstance = _viewModelLoader.LoadViewModel(request, null);
            ViewModel = request.ViewModelInstance as Notenverwaltung.WPF.UI.ViewModels.MenuViewModel;

            Mvx.IoCProvider.RegisterSingleton <Notenverwaltung.WPF.UI.ViewModels.MenuViewModel>(ViewModel);
        }
        public Task <MvxViewModelRequest> BuildViewModelRequest(string url, IDictionary <string, string> currentParameters)
        {
            if (url == "http://www.rseg.net/rewards")
            {
                return(Task.FromResult(new MvxViewModelRequest(typeof(RewardsViewModel), new MvxBundle(), null)));
            }
            else if (url.StartsWith("http://www.rseg.net/rewards/"))
            {
                // logic to create a ViewModelRequest and pass parameters as MvxBundle
                // causing Init() method on ViewModel to be called.

                //var parametersBundle = new MvxBundle();
                //var id = url.Substring(url.LastIndexOf('/') + 1);
                //parametersBundle.Data.Add("id", id);
                //var request = new MvxViewModelRequest(typeof(RewardDetailViewModel), parametersBundle, null);
                //return Task.FromResult(new MvxViewModelRequest(typeof(RewardDetailViewModel), parametersBundle, null));

                // logic to create a ViewModelInstanceRequest and pass parameters object
                // causing Prepare() method on ViewModel to be called. For this to work
                // you must provide a custom NavigationService and change NavigationRouteRequest()
                // to inspect the object returned from this method and if it is a
                // ViewModelInstanceRequest then use it as is rather than creating
                // another one.

                var request    = new MvxViewModelInstanceRequest(typeof(RewardDetailViewModel));
                var parameters = new RewardDetailViewModel.Parameteres();
                var idString   = url.Substring(url.LastIndexOf('/') + 1);
                int id;
                int.TryParse(idString, out id);
                parameters.RewardId       = id;
                request.ViewModelInstance = ViewModelLoader.LoadViewModel(request, parameters, null);

                return(Task.FromResult((MvxViewModelRequest)request));
            }
            else
            {
                return(Task.FromResult(new MvxViewModelRequest(typeof(FirstViewModel), new MvxBundle(), null)));
            }
        }