public NavigatingCancelEventArgs(IViewMappingItem mapping, NavigationMode navigationMode, string parameter)
 {
     _mapping = mapping;
     _navigationMode = navigationMode;
     _parameter = parameter;
     _isCancelable = true;
 }
        protected virtual void StartActivity(Context context, Intent intent, IViewMappingItem source, IDataContext dataContext)
        {
            var activity = context.GetActivity();
            Action <Context, Intent, IViewMappingItem, IDataContext> startAction = null;

            if (activity != null)
            {
                startAction = activity.GetBindingMemberValue(AttachedMembers.Activity.StartActivityDelegate);
            }
            if (startAction == null)
            {
                var bundle = dataContext.GetData(NavigationConstants.NavigationParameter) as Bundle;
                if (bundle == null)
                {
                    context.StartActivity(intent);
                }
                else
                {
                    context.StartActivity(intent, bundle);
                }
            }
            else
            {
                startAction(context, intent, source, dataContext);
            }
        }
        /// <summary>
        /// Adds the view mapping to internal collection.
        /// </summary>
        protected void AddMapping(IViewMappingItem mappingItem, bool throwOnError)
        {
            List <IViewMappingItem> list;

            if (!_viewTypeToMapping.TryGetValue(mappingItem.ViewType, out list))
            {
                list = new List <IViewMappingItem>();
                _viewTypeToMapping[mappingItem.ViewType] = list;
            }
            list.Add(mappingItem);

            Dictionary <string, IViewMappingItem> value;

            if (!_viewModelToMapping.TryGetValue(mappingItem.ViewModelType, out value))
            {
                value = new Dictionary <string, IViewMappingItem>();
                _viewModelToMapping[mappingItem.ViewModelType] = value;
            }
            IViewMappingItem item;
            string           name = mappingItem.Name ?? string.Empty;

            if (value.TryGetValue(name, out item))
            {
                if (throwOnError)
                {
                    throw ExceptionManager.DuplicateViewMapping(item.ViewType, item.ViewModelType, item.Name);
                }
                return;
            }
            value[name] = mappingItem;
            Tracer.Info("The view mapping to view model was created: ({0} ---> {1}), name: {2}",
                        mappingItem.ViewModelType, mappingItem.ViewType, mappingItem.Name);
        }
예제 #4
0
 public NavigatingCancelEventArgs(IViewMappingItem mapping, NavigationMode navigationMode, string parameter)
 {
     _mapping        = mapping;
     _navigationMode = navigationMode;
     _parameter      = parameter;
     _isCancelable   = true;
 }
예제 #5
0
        /// <summary>
        ///     Displays the content located at the specified <see cref="IViewMappingItem" />.
        /// </summary>
        /// <param name="source">
        ///     The <c>IViewPageMappingItem</c> of the content to display.
        /// </param>
        /// <param name="parameter">
        ///     A <see cref="T:System.Object" /> that contains data to be used for processing during
        ///     navigation.
        /// </param>
        /// <param name="dataContext">
        ///     The specified <see cref="IDataContext" />.
        /// </param>
        /// <returns>
        ///     <c>true</c> if the content was successfully displayed; otherwise, <c>false</c>.
        /// </returns>
        public bool Navigate(IViewMappingItem source, object parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, "source");
            if (!RaiseNavigating(new NavigatingCancelEventArgs(source, NavigationMode.New, parameter)))
            {
                return(false);
            }
            if (dataContext == null)
            {
                dataContext = DataContext.Empty;
            }

            var  viewModel = dataContext.GetData(NavigationConstants.ViewModel);
            Page page;

            if (viewModel == null)
            {
                page = (Page)ServiceProvider.IocContainer.Get(source.ViewType);
            }
            else
            {
                page = (Page)ViewManager.GetOrCreateView(viewModel, null, dataContext).GetUnderlyingView();
            }
            page.SetNavigationParameter(parameter);
            _rootPage.PushAsync(page);
            return(true);
        }
예제 #6
0
        private IWindowViewMediator TryCreateWindowViewMediator(IViewModel viewModel, IDataContext context)
        {
            bool data;

            if (context.TryGetData(NavigationConstants.SuppressWindowNavigation, out data) && data)
            {
                return(null);
            }

            var viewName = viewModel.GetViewName(context);
            IViewMappingItem mappingItem = ViewMappingProvider.FindMappingForViewModel(viewModel.GetType(), viewName, false);

            if (mappingItem == null)
            {
                return(null);
            }

            IWindowViewMediator viewMediator;

            if (!viewModel.Settings.Metadata.TryGetData(WindowViewMediatorConstant, out viewMediator))
            {
                viewMediator = CreateWindowViewMediator(viewModel, mappingItem.ViewType, context);
                if (viewMediator != null)
                {
                    viewModel.Settings.Metadata.Add(WindowViewMediatorConstant, viewMediator);
                }
            }
            return(viewMediator);
        }
예제 #7
0
 bool INavigationService.Navigate(IViewMappingItem source, string parameter, IDataContext dataContext)
 {
     if (Navigate == null)
     {
         return(false);
     }
     return(Navigate(source, parameter, dataContext));
 }
 public NavigatingCancelEventArgs(IViewMappingItem mapping, NavigationMode navigationMode, string parameter, IDataContext context)
 {
     _mapping        = mapping;
     _navigationMode = navigationMode;
     _parameter      = parameter;
     _context        = context.ToNonReadOnly();
     _isCancelable   = true;
 }
예제 #9
0
        protected virtual object GetView([NotNull] IViewModel viewModel, [NotNull] IDataContext context)
        {
            string           viewBindingName = viewModel.GetViewName(context);
            Type             vmType          = viewModel.GetType();
            IViewMappingItem mappingItem     = ViewMappingProvider.FindMappingForViewModel(vmType, viewBindingName, true);

            return(GetView(mappingItem, context));
        }
 public NavigatingCancelEventArgs(IViewMappingItem mapping, NavigationMode navigationMode, string parameter, bool isCancelable, bool isBackButton)
 {
     _mapping = mapping;
     _navigationMode = navigationMode;
     _parameter = parameter;
     _isCancelable = isCancelable;
     _isBackButton = isBackButton;
 }
예제 #11
0
 public NavigatingCancelEventArgs(IViewMappingItem mapping, NavigationMode navigationMode, string parameter, bool isCancelable, bool isBackButton)
 {
     _mapping        = mapping;
     _navigationMode = navigationMode;
     _parameter      = parameter;
     _isCancelable   = isCancelable;
     _isBackButton   = isBackButton;
 }
예제 #12
0
        public void VmProviderShouldReturnMappingWithoutNameBasedOnNamedConventionInheritedVm2()
        {
            IViewMappingProvider viewMappingProvider = GetViewMappingProvider(typeof(MappingViewModel), typeof(ChildMappingViewModel), typeof(MappingView), typeof(ChildMappingView));
            IViewMappingItem     viewMap             = viewMappingProvider.FindMappingForViewModel(typeof(ChildMappingViewModel), null, true);

            viewMap.Name.ShouldBeNull();
            viewMap.ViewModelType.ShouldEqual(typeof(ChildMappingViewModel));
            viewMap.ViewType.ShouldEqual(typeof(ChildMappingView));
        }
예제 #13
0
        public void VmProviderShouldResolveGenericVm()
        {
            IViewMappingProvider viewMappingProvider = GetViewMappingProvider(typeof(MappingViewGeneric));
            IViewMappingItem     mappingItem         = viewMappingProvider.FindMappingForViewModel(typeof(GridViewModel <object>), null, true);

            mappingItem.ViewModelType.ShouldEqual(typeof(GridViewModel <>));
            mappingItem.ViewType.ShouldEqual(typeof(MappingViewGeneric));
            mappingItem.Name.ShouldBeNull();
        }
예제 #14
0
        /// <summary>
        ///     Displays the content located at the specified <see cref="IViewMappingItem" />.
        /// </summary>
        /// <param name="source">
        ///     The <c>IViewPageMappingItem</c> of the content to display.
        /// </param>
        /// <param name="parameter">
        ///     A <see cref="T:System.Object" /> that contains data to be used for processing during
        ///     navigation.
        /// </param>
        /// <param name="dataContext">
        ///     The specified <see cref="IDataContext" />.
        /// </param>
        /// <returns>
        ///     <c>true</c> if the content was successfully displayed; otherwise, <c>false</c>.
        /// </returns>
        public virtual bool Navigate(IViewMappingItem source, object parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, "source");
            if (!RaiseNavigating(new NavigatingCancelEventArgs(source, NavigationMode.New, parameter)))
            {
                return(false);
            }
            bool    isFirstActivity = _currentActivity == null;
            Context context;
            var     activity = _currentActivity ?? SplashScreenActivityBase.Current;

            if (activity == null)
            {
                context = Application.Context;
            }
            else
            {
                var attr = activity.GetType()
                           .GetCustomAttributes(typeof(ActivityAttribute), false)
                           .OfType <ActivityAttribute>()
                           .FirstOrDefault();
                if (attr == null || !attr.NoHistory)
                {
                    _prevIntent = activity.Intent;
                }
                context = activity;
            }

            var intent = new Intent(context, source.ViewType);

            if (activity == null)
            {
                intent.AddFlags(ActivityFlags.NewTask);
            }
            if (isFirstActivity)
            {
                intent.PutExtra(FirstActivityKey, true);
            }
            if (parameter != null)
            {
                var s = parameter as string;
                if (s == null)
                {
                    using (var stream = _serializer.Serialize(parameter))
                        intent.PutExtra(ParameterSerializer, stream.ToArray());
                }
                else
                {
                    intent.PutExtra(ParameterString, s);
                }
            }
            _isNew     = true;
            _parameter = parameter;
            context.StartActivity(intent);
            return(true);
        }
예제 #15
0
        public void VmProviderShouldReturnMappingWithoutNameUsingAttribute()
        {
            IViewMappingProvider viewMappingProvider = GetViewMappingProvider(typeof(MappingViewModel),
                                                                              typeof(MappingView), typeof(MappingViewWithAttribute));
            IViewMappingItem viewMap = viewMappingProvider.FindMappingForViewModel(typeof(MappingViewModel), null, true);

            viewMap.Name.ShouldBeNull();
            viewMap.ViewModelType.ShouldEqual(typeof(MappingViewModel));
            viewMap.ViewType.ShouldEqual(typeof(MappingViewWithAttribute));
        }
예제 #16
0
        public void VmProviderShouldReturnMappingWithUriUsingAttribute()
        {
            IViewMappingProvider viewMappingProvider = GetViewMappingProvider(typeof(MappingViewModel),
                                                                              typeof(MappingView), typeof(MappingViewWithUri));
            IViewMappingItem viewMap = viewMappingProvider.FindMappingForViewModel(typeof(MappingViewModel), null, true);

            viewMap.Name.ShouldBeNull();
            viewMap.Uri.ShouldEqual(new Uri(TestUrl, UriKind.Absolute));
            viewMap.ViewModelType.ShouldEqual(typeof(MappingViewModel));
            viewMap.ViewType.ShouldEqual(typeof(MappingViewWithUri));
        }
예제 #17
0
        public bool Navigate(IViewMappingItem source, string parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, nameof(source));
            var result = NavigateInternal(source, parameter);

            if (result)
            {
                ClearNavigationStackIfNeed(dataContext);
            }
            return(result);
        }
예제 #18
0
        private static bool CanShowViewModelNavigationPresenter(IViewModel viewModel, IDataContext dataContext,
                                                                IViewModelPresenter arg3)
        {
            string           viewName        = viewModel.GetViewName(dataContext);
            IIocContainer    container       = viewModel.GetIocContainer(true);
            var              mappingProvider = container.Get <IViewMappingProvider>();
            IViewMappingItem mappingItem     = mappingProvider.FindMappingForViewModel(viewModel.GetType(), viewName, false);

            return(mappingItem != null &&
                   typeof(Page).GetTypeInfo().IsAssignableFrom(mappingItem.ViewType.GetTypeInfo()));
        }
        private void AddMapping(IViewMappingItem mappingItem, bool throwOnError, bool rewrite = false)
        {
            if (!rewrite)
            {
                var builder = ToolkitServiceProvider.BootstrapCodeBuilder;
                if (builder != null)
                {
                    if (!_isCodeBuilderInitialized)
                    {
                        builder.Append(typeof(ViewMappingProvider).Name, $"var mappingProvider = ({typeof(ViewMappingProvider).FullName}) context.{nameof(IModuleContext.IocContainer)}.Get(typeof({typeof(IViewMappingProvider).FullName}));");
                        _isCodeBuilderInitialized = true;
                    }
                    string newUri = mappingItem.Uri == ViewMappingItem.Empty ? "null" : $"new {typeof(Uri).FullName}(\"{mappingItem.Uri}\")";
                    builder.Append(typeof(ViewMappingProvider).Name,
                                   $"mappingProvider.{nameof(AddMapping)}(new {typeof(ViewMappingItem).FullName}(typeof({mappingItem.ViewModelType.GetPrettyName()}), typeof({mappingItem.ViewType.GetPrettyName()}), \"{mappingItem.Name}\", {newUri}));");
                }
            }
            List <IViewMappingItem> list;

            if (!_viewTypeToMapping.TryGetValue(mappingItem.ViewType, out list))
            {
                list = new List <IViewMappingItem>();
                _viewTypeToMapping[mappingItem.ViewType] = list;
            }
            list.Add(mappingItem);

            Dictionary <string, IViewMappingItem> value;

            if (!_viewModelToMapping.TryGetValue(mappingItem.ViewModelType, out value))
            {
                value = new Dictionary <string, IViewMappingItem>();
                _viewModelToMapping[mappingItem.ViewModelType] = value;
            }
            IViewMappingItem item;
            string           name = mappingItem.Name ?? string.Empty;

            if (value.TryGetValue(name, out item))
            {
                if (throwOnError)
                {
                    throw ExceptionManager.DuplicateViewMapping(item.ViewType, item.ViewModelType, item.Name);
                }
                if (!rewrite)
                {
                    return;
                }
            }
            value[name] = mappingItem;
            if (Tracer.TraceInformation)
            {
                Tracer.Info("The view mapping to view model was created: ({0} ---> {1}), name: {2}",
                            mappingItem.ViewModelType, mappingItem.ViewType, mappingItem.Name);
            }
        }
        public bool Navigate(IViewMappingItem source, string parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, nameof(source));
            Should.NotBeNull(dataContext, nameof(dataContext));
            dataContext.TryGetData(NavigationProvider.BringToFront, out _bringToFront);
            var result = Navigate(source.ViewType, parameter, dataContext);

            if (result)
            {
                ClearNavigationStackIfNeed(dataContext);
            }
            return(result);
        }
예제 #21
0
        protected virtual object GetView([NotNull] IViewMappingItem viewMapping, [NotNull] IDataContext context)
        {
            object viewObj = ServiceProvider.Get(viewMapping.ViewType);

            if (ApplicationSettings.ViewManagerDisposeView)
            {
                ServiceProvider.AttachedValueProvider.SetValue(viewObj, ViewManagerCreatorPath, null);
            }
            if (Tracer.TraceInformation)
            {
                Tracer.Info("The view {0} for the view-model {1} was created.", viewObj.GetType(), viewMapping.ViewModelType);
            }
            return(viewObj);
        }
예제 #22
0
        public virtual void Start()
        {
            Initialize();
            var app          = MvvmApplication.Current;
            var iocContainer = app.IocContainer;
            var ctx          = new DataContext(app.Context);

            if (!ctx.Contains(NavigationConstants.IsDialog))
            {
                ctx.Add(NavigationConstants.IsDialog, false);
            }
            var viewModelType = app.GetStartViewModelType();

            NavigationWindow rootWindow = null;
            var mappingProvider         = iocContainer.Get <IViewMappingProvider>();
            IViewMappingItem mapping    = mappingProvider.FindMappingForViewModel(viewModelType, ctx.GetData(NavigationConstants.ViewName), true);

            if (typeof(Page).IsAssignableFrom(mapping.ViewType))
            {
                rootWindow = CreateNavigationWindow();
                var service = CreateNavigationService(rootWindow);
                iocContainer.BindToConstant(service);
            }
            var vm = iocContainer
                     .Get <IViewModelProvider>()
                     .GetViewModel(viewModelType, ctx);

            vm.ShowAsync((model, result) =>
            {
                model.Dispose();
                if (ShutdownOnMainViewModelClose)
                {
                    Application application = Application.Current;
                    if (application != null)
                    {
                        Action action = application.Shutdown;
                        application.Dispatcher.BeginInvoke(action);
                    }
                }
            }, context: ctx);
            if (rootWindow != null)
            {
                IWindowViewMediator mediator = new WindowViewMediator(rootWindow, vm, iocContainer.Get <IThreadManager>(),
                                                                      iocContainer.Get <IViewManager>(), iocContainer.Get <IWrapperManager>(),
                                                                      iocContainer.Get <IOperationCallbackManager>());
                mediator.UpdateView(new PlatformWrapperRegistrationModule.WindowViewWrapper(rootWindow), true, ctx);
                rootWindow.Show();
            }
        }
        public bool Navigate(IViewMappingItem source, string parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, "source");
            if (dataContext == null)
            {
                dataContext = DataContext.Empty;
            }
            var result = Navigate(source.ViewType, parameter, dataContext.GetData(NavigationConstants.ViewModel));

            if (result)
            {
                ClearNavigationStackIfNeed(dataContext);
            }
            return(result);
        }
        /// <summary>
        ///     Displays the content located at the specified <see cref="IViewMappingItem" />.
        /// </summary>
        /// <param name="source">
        ///     The <c>IViewPageMappingItem</c> of the content to display.
        /// </param>
        /// <param name="parameter">
        ///     A <see cref="T:System.Object" /> that contains data to be used for processing during
        ///     navigation.
        /// </param>
        /// <param name="dataContext">
        ///     The specified <see cref="IDataContext" />.
        /// </param>
        /// <returns>
        ///     <c>true</c> if the content was successfully displayed; otherwise, <c>false</c>.
        /// </returns>
        public bool Navigate(IViewMappingItem source, object parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, "source");
            Uri uri = source.Uri;

            if (parameter != null)
            {
                var s            = parameter as string;
                var uriParameter = s == null
                    ? new KeyValuePair <string, string>(UriParameterSerializer, _serializer.SerializeToBase64String(parameter))
                    : new KeyValuePair <string, string>(UriParameterString, s);
                uri = uri.MergeUri(new[] { uriParameter });
            }
            return(_frame.Navigate(uri));
        }
        private bool NavigateInternal(IViewMappingItem source, string parameter)
        {
            Uri uri = source.Uri;

            if (!string.IsNullOrEmpty(parameter))
            {
                uri =
                    uri.MergeUri(new[]
                {
                    new KeyValuePair <string, string>(UriParameterString, parameter),
                    new KeyValuePair <string, string>("_timestamp",
                                                      DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture))
                });
            }
            return(_frame.Navigate(uri));
        }
예제 #26
0
 private bool NavigateInternal(IViewMappingItem source, object parameter)
 {
     if (_useUrlNavigation)
     {
         if (parameter == null)
         {
             return(_window.Navigate(source.Uri));
         }
         return(_window.Navigate(source.Uri, parameter));
     }
     if (parameter == null)
     {
         return(_window.Navigate(CreateView(source, null)));
     }
     return(_window.Navigate(CreateView(source, parameter), parameter));
 }
 /// <summary>
 ///     Displays the content located at the specified <see cref="IViewMappingItem" />.
 /// </summary>
 /// <param name="source">
 ///     The <c>IViewPageMappingItem</c> of the content to display.
 /// </param>
 /// <param name="parameter">
 ///     A <see cref="T:System.Object" /> that contains data to be used for processing during
 ///     navigation.
 /// </param>
 /// <param name="dataContext">
 ///     The specified <see cref="IDataContext" />.
 /// </param>
 /// <returns>
 ///     <c>true</c> if the content was successfully displayed; otherwise, <c>false</c>.
 /// </returns>
 public bool Navigate(IViewMappingItem source, object parameter, IDataContext dataContext)
 {
     Should.NotBeNull(source, "source");
     if (_useUrlNavigation)
     {
         if (parameter == null)
         {
             return(_window.Navigate(source.Uri));
         }
         return(_window.Navigate(source.Uri, parameter));
     }
     if (parameter == null)
     {
         return(_window.Navigate(_viewFactory(source.ViewType)));
     }
     return(_window.Navigate(_viewFactory(source.ViewType), parameter));
 }
예제 #28
0
        public Task <object> GetViewAsync(IViewMappingItem viewMapping, IDataContext context = null)
        {
            Should.NotBeNull(viewMapping, nameof(viewMapping));
            var tcs = new TaskCompletionSource <object>();

            ThreadManager.InvokeOnUiThreadAsync(() =>
            {
                if (context == null)
                {
                    context = DataContext.Empty;
                }
                object view = GetView(viewMapping, context);
                ViewCreated?.Invoke(this, new ViewCreatedEventArgs(view, null, viewMapping, context));
                tcs.SetResult(view);
            });
            return(tcs.Task);
        }
예제 #29
0
        /// <summary>
        ///     Displays the content located at the specified <see cref="IViewMappingItem" />.
        /// </summary>
        /// <param name="source">
        ///     The <c>IViewPageMappingItem</c> of the content to display.
        /// </param>
        /// <param name="parameter">
        ///     A <see cref="T:System.Object" /> that contains data to be used for processing during
        ///     navigation.
        /// </param>
        /// <param name="dataContext">
        ///     The specified <see cref="IDataContext" />.
        /// </param>
        /// <returns>
        ///     <c>true</c> if the content was successfully displayed; otherwise, <c>false</c>.
        /// </returns>
        public virtual bool Navigate(IViewMappingItem source, object parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, "source");
            EnsureInitialized();
            if (!RaiseNavigating(new NavigatingCancelEventArgs(source, NavigationMode.New, parameter)))
            {
                return(false);
            }
            if (dataContext == null)
            {
                dataContext = DataContext.Empty;
            }

            IViewModel       viewModel = dataContext.GetData(NavigationConstants.ViewModel);
            UIViewController viewController;

            if (viewModel == null)
            {
                viewController = (UIViewController)ServiceProvider.IocContainer.Get(source.ViewType);
            }
            else
            {
                viewController = (UIViewController)ViewManager.GetOrCreateView(viewModel, null, dataContext).GetUnderlyingView();
            }

            viewController.SetNavigationParameter(parameter);
            bool shouldNavigate = true;

            if (_window != null)
            {
                var controller = _window.RootViewController as UINavigationController;
                if (controller == null)
                {
                    shouldNavigate             = false;
                    controller                 = new MvvmNavigationController(viewController);
                    _window.RootViewController = controller;
                }
                InitializeNavigationController(controller);
            }
            if (shouldNavigate)
            {
                NavigationController.PushViewController(viewController, true);
            }
            RaiseNavigated(viewController, NavigationMode.New, parameter);
            return(true);
        }
        protected virtual void StartActivity(Context context, Intent intent, IViewMappingItem source, IDataContext dataContext)
        {
            var activity = context.GetActivity();
            Action <Context, Intent, IViewMappingItem, IDataContext> startAction = null;

            if (activity != null)
            {
                startAction = activity.GetBindingMemberValue(AttachedMembers.Activity.StartActivityDelegate);
            }
            if (startAction == null)
            {
                context.StartActivity(intent);
            }
            else
            {
                startAction(context, intent, source, dataContext);
            }
        }
예제 #31
0
        public virtual bool Navigate(IViewMappingItem source, string parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, "source");
            if (_rootPage == null)
            {
                return(false);
            }
            if (!RaiseNavigating(new NavigatingCancelEventArgs(source, NavigationMode.New, parameter, true, false)))
            {
                return(false);
            }
            if (dataContext == null)
            {
                dataContext = DataContext.Empty;
            }

            var  viewModel = dataContext.GetData(NavigationConstants.ViewModel);
            bool animated;

            if (dataContext.TryGetData(NavigationConstants.UseAnimations, out animated))
            {
                if (viewModel != null)
                {
                    viewModel.Settings.State.AddOrUpdate(NavigationConstants.UseAnimations, animated);
                }
            }
            else
            {
                animated = UseAnimations;
            }
            Page page;

            if (viewModel == null)
            {
                page = (Page)ServiceProvider.Get <IViewManager>().GetViewAsync(source, dataContext).Result;
            }
            else
            {
                page = (Page)ViewManager.GetOrCreateView(viewModel, null, dataContext);
            }
            page.SetNavigationParameter(parameter);
            ClearNavigationStackIfNeed(dataContext, page, _rootPage.PushAsync(page, animated));
            return(true);
        }
예제 #32
0
        /// <summary>
        ///     Gets an instance of <see cref="IView" /> for the specified view model.
        /// </summary>
        /// <param name="viewModel">The view model which is now initialized.</param>
        /// <param name="dataContext">The specified <see cref="IDataContext" />.</param>
        /// <returns>
        ///     An instance of <see cref="IView" />.
        /// </returns>
        protected virtual IView GetView(IViewModel viewModel, IDataContext dataContext)
        {
            var              viewBindingName = viewModel.GetViewName(dataContext);
            Type             viewType        = viewModel.GetType();
            IViewMappingItem mappingItem     = ViewMappingProvider.FindMappingForViewModel(viewType, viewBindingName, true);
            object           viewObj         = viewModel.GetIocContainer(true).Get(mappingItem.ViewType);
            var              view            = viewObj as IView;

            if (view == null)
            {
                view = WrapToView(viewObj, dataContext);
            }
            else if (DisposeView)
            {
                ServiceProvider.AttachedValueProvider.SetValue(viewObj, ViewManagerCreatorPath, null);
            }
            Tracer.Info("The view {0} for the view-model {1} was created.", view.GetUnderlyingView().GetType(), viewModel.GetType());
            return(view);
        }
        private void AddMapping(IViewMappingItem mappingItem, bool throwOnError, bool rewrite = false)
        {
            var builder = ServiceProvider.BootstrapCodeBuilder;
            if (builder != null)
            {
                string newUri = mappingItem.Uri == ViewMappingItem.Empty ? "null" : $"new {typeof(Uri).FullName}(\"{mappingItem.Uri}\")";
                builder.Append(typeof(ViewMappingProvider).Name,
   $"mappingProvider.{nameof(AddMapping)}(new {typeof(ViewMappingItem).FullName}(typeof({mappingItem.ViewModelType.GetPrettyName()}), typeof({mappingItem.ViewType.GetPrettyName()}), \"{mappingItem.Name}\", {newUri}));");
            }
            List<IViewMappingItem> list;
            if (!_viewTypeToMapping.TryGetValue(mappingItem.ViewType, out list))
            {
                list = new List<IViewMappingItem>();
                _viewTypeToMapping[mappingItem.ViewType] = list;
            }
            list.Add(mappingItem);

            Dictionary<string, IViewMappingItem> value;
            if (!_viewModelToMapping.TryGetValue(mappingItem.ViewModelType, out value))
            {
                value = new Dictionary<string, IViewMappingItem>();
                _viewModelToMapping[mappingItem.ViewModelType] = value;
            }
            IViewMappingItem item;
            string name = mappingItem.Name ?? string.Empty;
            if (value.TryGetValue(name, out item))
            {
                if (throwOnError)
                    throw ExceptionManager.DuplicateViewMapping(item.ViewType, item.ViewModelType, item.Name);
                if (!rewrite)
                    return;
            }
            value[name] = mappingItem;
            if (Tracer.TraceInformation)
                Tracer.Info("The view mapping to view model was created: ({0} ---> {1}), name: {2}",
                    mappingItem.ViewModelType, mappingItem.ViewType, mappingItem.Name);
        }
        public virtual bool Navigate(IViewMappingItem source, string parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, "source");
            if (!RaiseNavigating(new NavigatingCancelEventArgs(source, NavigationMode.New, parameter)))
                return false;
            if (dataContext == null)
                dataContext = DataContext.Empty;
            var activity = PlatformExtensions.CurrentActivity ?? SplashScreenActivityBase.Current;
            var context = activity ?? Application.Context;

            var intent = new Intent(context, source.ViewType);
            if (activity == null)
                intent.AddFlags(ActivityFlags.NewTask);
            else if (dataContext.GetData(NavigationConstants.ClearBackStack))
            {
                if (PlatformExtensions.IsApiLessThanOrEqualTo10)
                {
                    intent.AddFlags(ActivityFlags.NewTask | ActivityFlags.ClearTop);
                    ServiceProvider.EventAggregator.Publish(this, MvvmActivityMediator.FinishActivityMessage.Instance);
                }
                else
                    intent.AddFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
                dataContext.AddOrUpdate(NavigationProvider.ClearNavigationCache, true);
            }
            if (parameter != null)
                intent.PutExtra(ParameterString, parameter);
            _isNew = true;
            _parameter = parameter;
            StartActivity(context, intent, dataContext);
            return true;
        }
 public bool Navigate(IViewMappingItem source, string parameter, IDataContext dataContext)
 {
     Should.NotBeNull(source, nameof(source));
     if (dataContext == null)
         dataContext = DataContext.Empty;
     dataContext.TryGetData(NavigationProviderConstants.BringToFront, out _bringToFront);
     var bringToFront = _bringToFront;
     var result = Navigate(source.ViewType, parameter, dataContext.GetData(NavigationConstants.ViewModel));
     if (result)
     {
         if (bringToFront)
             dataContext.AddOrUpdate(NavigationProviderConstants.InvalidateCache, true);
         ClearNavigationStackIfNeed(dataContext);
     }
     return result;
 }
예제 #36
0
 public Task<object> GetViewAsync(IViewMappingItem viewMapping, IDataContext context = null)
 {
     Should.NotBeNull(viewMapping, nameof(viewMapping));
     var tcs = new TaskCompletionSource<object>();
     ThreadManager.InvokeOnUiThreadAsync(() =>
     {
         if (context == null)
             context = DataContext.Empty;
         object view = GetView(viewMapping, context);
         ViewCreated?.Invoke(this, null, view, context);
         tcs.SetResult(view);
     });
     return tcs.Task;
 }
        public virtual bool Navigate(IViewMappingItem source, string parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, nameof(source));
            if (dataContext == null)
                dataContext = DataContext.Empty;
            bool bringToFront;
            dataContext.TryGetData(NavigationProviderConstants.BringToFront, out bringToFront);
            if (!RaiseNavigating(new NavigatingCancelEventArgs(source, bringToFront ? NavigationMode.Refresh : NavigationMode.New, parameter)))
                return false;

            UIViewController viewController = null;
            IViewModel viewModel = dataContext.GetData(NavigationConstants.ViewModel);
            if (bringToFront && viewModel != null)
            {
                var viewControllers = new List<UIViewController>(NavigationController.ViewControllers);
                for (int i = 0; i < viewControllers.Count; i++)
                {
                    var controller = viewControllers[i];
                    if (controller.DataContext() == viewModel)
                    {
                        viewControllers.RemoveAt(i);
                        viewController = controller;
                        NavigationController.ViewControllers = viewControllers.ToArray();
                        dataContext.AddOrUpdate(NavigationProviderConstants.InvalidateCache, true);
                        break;
                    }
                }
            }

            if (viewController == null)
            {
                if (viewModel == null)
                    viewController = (UIViewController)ServiceProvider.Get<IViewManager>().GetViewAsync(source, dataContext).Result;
                else
                    viewController = (UIViewController)ViewManager.GetOrCreateView(viewModel, null, dataContext);
            }
            viewController.SetNavigationParameter(parameter);
            bool shouldNavigate = true;
            if (_window != null)
            {
                bool navigated;
                InitializeNavigationController(GetNavigationController(_window, viewController, out navigated));
                shouldNavigate = !navigated;
                _window = null;
            }
            if (shouldNavigate)
            {
                bool animated;
                if (dataContext.TryGetData(NavigationConstants.UseAnimations, out animated))
                {
                    if (viewModel != null)
                        viewModel.Settings.State.AddOrUpdate(NavigationConstants.UseAnimations, animated);
                }
                else
                    animated = UseAnimations;
                if (!ClearNavigationStackIfNeed(viewController, dataContext, animated))
                    NavigationController.PushViewController(viewController, animated);
            }
            var view = viewController as IViewControllerView;
            if (view == null || view.Mediator.IsAppeared)
                RaiseNavigated(viewController, bringToFront ? NavigationMode.Refresh : NavigationMode.New, parameter);
            else
            {
                if (bringToFront)
                    view.Mediator.ViewDidAppearHandler += OnViewDidAppearHandlerRefresh;
                else
                    view.Mediator.ViewDidAppearHandler += OnViewDidAppearHandlerNew;
            }
            return true;
        }
 private bool NavigateInternal(IViewMappingItem source, object parameter)
 {
     if (_useUrlNavigation)
     {
         if (parameter == null)
             return _frame.Navigate(source.Uri);
         return _frame.Navigate(source.Uri, parameter);
     }
     if (parameter == null)
         return _frame.Navigate(_viewFactory(source.ViewType));
     return _frame.Navigate(_viewFactory(source.ViewType), parameter);
 }
 public void AddMapping(IViewMappingItem mappingItem)
 {
     AddMapping(mappingItem, false, true);
 }
 private bool NavigateInternal(IViewMappingItem source, string parameter)
 {
     Uri uri = source.Uri;
     if (!string.IsNullOrEmpty(parameter))
     {
         uri =
             uri.MergeUri(new[]
             {
                 new KeyValuePair<string, string>(UriParameterString, parameter),
                 new KeyValuePair<string, string>("_timestamp",
                     DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture))
             });
     }
     return _frame.Navigate(uri);
 }
        public virtual bool Navigate(IViewMappingItem source, string parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, nameof(source));
            if (dataContext == null)
                dataContext = DataContext.Empty;
            bool bringToFront;
            dataContext.TryGetData(NavigationProviderConstants.BringToFront, out bringToFront);
            if (!RaiseNavigating(new NavigatingCancelEventArgs(source, bringToFront ? NavigationMode.Refresh : NavigationMode.New, parameter)))
                return false;
            var activity = PlatformExtensions.CurrentActivity ?? SplashScreenActivityBase.Current;
            var context = activity ?? Application.Context;

            var intent = new Intent(context, source.ViewType);
            if (activity == null)
                intent.AddFlags(ActivityFlags.NewTask);
            else if (dataContext.GetData(NavigationConstants.ClearBackStack))
            {
                if (PlatformExtensions.IsApiLessThanOrEqualTo10)
                    intent.AddFlags(ActivityFlags.NewTask | ActivityFlags.ClearTop);
                else
                    intent.AddFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
                ServiceProvider.EventAggregator.Publish(this, MvvmActivityMediator.FinishActivityMessage.Instance);
                dataContext.AddOrUpdate(NavigationProviderConstants.InvalidateAllCache, true);
            }
            if (parameter != null)
                intent.PutExtra(ParameterString, parameter);

            if (bringToFront)
            {
                _isReorder = true;
                //http://stackoverflow.com/questions/20695522/puzzling-behavior-with-reorder-to-front
                //http://code.google.com/p/android/issues/detail?id=63570#c2
                bool closed = false;
                if (PlatformExtensions.IsApiGreaterThanOrEqualTo19)
                {
                    var viewModel = dataContext.GetData(NavigationConstants.ViewModel);
                    if (viewModel != null)
                    {
                        var view = viewModel.Settings.Metadata.GetData(ViewModelConstants.View);
                        var activityView = ToolkitExtensions.GetUnderlyingView<object>(view) as Activity;
                        if (activityView != null && activityView.IsTaskRoot)
                        {
                            var message = new MvvmActivityMediator.FinishActivityMessage(viewModel);
                            ServiceProvider.EventAggregator.Publish(this, message);
                            closed = message.Finished;
                        }
                    }
                }
                if (!closed)
                    intent.AddFlags(ActivityFlags.ReorderToFront);
                dataContext.AddOrUpdate(NavigationProviderConstants.InvalidateCache, true);
            }
            _isNew = true;
            _parameter = parameter;
            StartActivity(context, intent, source, dataContext);
            return true;
        }
 private bool NavigateInternal(IViewMappingItem source, object parameter)
 {
     Uri uri = source.Uri;
     if (parameter != null)
     {
         var s = parameter as string;
         KeyValuePair<string, string> uriParameter = s == null
             ? new KeyValuePair<string, string>(UriParameterSerializer,
                 _serializer.SerializeToBase64String(parameter))
             : new KeyValuePair<string, string>(UriParameterString, s);
         uri =
             uri.MergeUri(new[]
             {
                 uriParameter,
                 new KeyValuePair<string, string>("_timestamp",
                     DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture))
             });
     }
     return _frame.Navigate(uri);
 }
예제 #43
0
 public Task<object> GetViewAsync(IViewMappingItem viewMapping, IDataContext context = null)
 {
     Should.NotBeNull(viewMapping, "viewMapping");
     var tcs = new TaskCompletionSource<object>();
     ThreadManager.InvokeOnUiThreadAsync(() =>
     {
         if (context == null)
             context = DataContext.Empty;
         object view = GetView(viewMapping, context);
         Action<IViewManager, IViewModel, object, IDataContext> handler = ViewCreated;
         if (handler != null)
             handler(this, null, view, context);
         tcs.SetResult(view);
     });
     return tcs.Task;
 }
 /// <summary>
 ///     Displays the content located at the specified <see cref="IViewMappingItem" />.
 /// </summary>
 /// <param name="source">
 ///     The <c>IViewPageMappingItem</c> of the content to display.
 /// </param>
 /// <param name="parameter">
 ///     A <see cref="T:System.Object" /> that contains data to be used for processing during
 ///     navigation.
 /// </param>
 /// <param name="dataContext">
 ///     The specified <see cref="IDataContext" />.
 /// </param>
 /// <returns>
 ///     <c>true</c> if the content was successfully displayed; otherwise, <c>false</c>.
 /// </returns>
 public bool Navigate(IViewMappingItem source, object parameter, IDataContext dataContext)
 {
     Should.NotBeNull(source, "source");
     if (dataContext == null)
         dataContext = DataContext.Empty;
     var result = Navigate(source.ViewType, parameter, dataContext.GetData(NavigationConstants.ViewModel));
     if (result)
         ClearNavigationStackIfNeed(dataContext);
     return result;
 }
        public virtual bool Navigate(IViewMappingItem source, string parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, nameof(source));
            if (_rootPage == null)
                return false;
            if (dataContext == null)
                dataContext = DataContext.Empty;
            dataContext.TryGetData(NavigationProviderConstants.BringToFront, out _bringToFront);
            if (!RaiseNavigating(new NavigatingCancelEventArgs(source, _bringToFront ? NavigationMode.Refresh : NavigationMode.New, parameter, true, false)))
                return false;

            var viewModel = dataContext.GetData(NavigationConstants.ViewModel);
            bool animated;
            if (dataContext.TryGetData(NavigationConstants.UseAnimations, out animated))
            {
                if (viewModel != null)
                    viewModel.Settings.State.AddOrUpdate(NavigationConstants.UseAnimations, animated);
            }
            else
                animated = UseAnimations;
            Page page = null;
            if (_bringToFront && viewModel != null)
            {
                var navigation = _rootPage.Navigation;
                if (navigation != null)
                {
                    for (int i = 0; i < navigation.NavigationStack.Count; i++)
                    {
                        var p = navigation.NavigationStack[i];
                        if (p.BindingContext == viewModel)
                        {
                            page = p;
                            navigation.RemovePage(p);
                            dataContext.AddOrUpdate(NavigationProviderConstants.InvalidateCache, true);
                            break;
                        }
                    }
                }
            }
            if (page == null)
            {
                if (viewModel == null)
                    page = (Page)ServiceProvider.Get<IViewManager>().GetViewAsync(source, dataContext).Result;
                else
                    page = (Page)ViewManager.GetOrCreateView(viewModel, null, dataContext);
            }
            page.SetNavigationParameter(parameter);
            ClearNavigationStackIfNeed(dataContext, page, _rootPage.PushAsync(page, animated));
            return true;
        }
 protected virtual void StartActivity(Context context, Intent intent, IViewMappingItem source, IDataContext dataContext)
 {
     var activity = context.GetActivity();
     Action<Context, Intent, IViewMappingItem, IDataContext> startAction = null;
     if (activity != null)
         startAction = activity.GetBindingMemberValue(AttachedMembers.Activity.StartActivityDelegate);
     if (startAction == null)
         context.StartActivity(intent);
     else
         startAction(context, intent, source, dataContext);
 }
 protected virtual object CreateView(IViewMappingItem viewMapping, object parameter)
 {
     return ServiceProvider.Get<IViewManager>().GetViewAsync(viewMapping, parameter as IDataContext).Result;
 }
 bool INavigationService.Navigate(IViewMappingItem source, string parameter, IDataContext dataContext)
 {
     if (Navigate == null)
         return false;
     return Navigate(source, parameter, dataContext);
 }
 private bool NavigateInternal(IViewMappingItem source, object parameter)
 {
     if (_useUrlNavigation)
     {
         if (parameter == null)
             return _window.Navigate(source.Uri);
         return _window.Navigate(source.Uri, parameter);
     }
     if (parameter == null)
         return _window.Navigate(CreateView(source, null));
     return _window.Navigate(CreateView(source, parameter), parameter);
 }
예제 #50
0
 public Task<object> GetViewAsync(IViewMappingItem viewMapping, IDataContext context = null)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 ///     Displays the content located at the specified <see cref="IViewMappingItem" />.
 /// </summary>
 /// <param name="source">
 ///     The <c>IViewPageMappingItem</c> of the content to display.
 /// </param>
 /// <param name="parameter">
 ///     A <see cref="T:System.Object" /> that contains data to be used for processing during
 ///     navigation.
 /// </param>
 /// <param name="dataContext">
 ///     The specified <see cref="IDataContext" />.
 /// </param>
 /// <returns>
 ///     <c>true</c> if the content was successfully displayed; otherwise, <c>false</c>.
 /// </returns>
 public bool Navigate(IViewMappingItem source, string parameter, IDataContext dataContext)
 {
     Should.NotBeNull(source, "source");
     var result = NavigateInternal(source, parameter);
     if (result)
         ClearNavigationStackIfNeed(dataContext);
     return result;
 }
예제 #52
0
        /// <summary>
        ///     Displays the content located at the specified <see cref="IViewMappingItem" />.
        /// </summary>
        /// <param name="source">
        ///     The <c>IViewPageMappingItem</c> of the content to display.
        /// </param>
        /// <param name="parameter">
        ///     A <see cref="T:System.Object" /> that contains data to be used for processing during
        ///     navigation.
        /// </param>
        /// <param name="dataContext">
        ///     The specified <see cref="IDataContext" />.
        /// </param>
        /// <returns>
        ///     <c>true</c> if the content was successfully displayed; otherwise, <c>false</c>.
        /// </returns>
        public bool Navigate(IViewMappingItem source, object parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, "source");
            if (_rootPage == null)
                return false;
            if (!RaiseNavigating(new NavigatingCancelEventArgs(source, NavigationMode.New, parameter)))
                return false;
            if (dataContext == null)
                dataContext = DataContext.Empty;

            var viewModel = dataContext.GetData(NavigationConstants.ViewModel);
            bool animated;
            if (dataContext.TryGetData(NavigationConstants.UseAnimations, out animated))
            {
                if (viewModel != null)
                    viewModel.Settings.State.AddOrUpdate(NavigationConstants.UseAnimations, animated);
            }
            else
                animated = UseAnimations;
            Page page;
            if (viewModel == null)
                page = (Page)ServiceProvider.IocContainer.Get(source.ViewType);
            else
                page = (Page)ViewManager.GetOrCreateView(viewModel, null, dataContext);
            page.SetNavigationParameter(parameter);
            ClearNavigationStackIfNeed(dataContext, page, _rootPage.PushAsync(page, animated));
            return true;
        }
예제 #53
0
        /// <summary>
        ///     Displays the content located at the specified <see cref="IViewMappingItem" />.
        /// </summary>
        /// <param name="source">
        ///     The <c>IViewPageMappingItem</c> of the content to display.
        /// </param>
        /// <param name="parameter">
        ///     A <see cref="T:System.Object" /> that contains data to be used for processing during
        ///     navigation.
        /// </param>
        /// <param name="dataContext">
        ///     The specified <see cref="IDataContext" />.
        /// </param>
        /// <returns>
        ///     <c>true</c> if the content was successfully displayed; otherwise, <c>false</c>.
        /// </returns>
        public virtual bool Navigate(IViewMappingItem source, object parameter, IDataContext dataContext)
        {
            Should.NotBeNull(source, "source");
            EnsureInitialized();
            if (!RaiseNavigating(new NavigatingCancelEventArgs(source, NavigationMode.New, parameter)))
                return false;
            if (dataContext == null)
                dataContext = DataContext.Empty;

            IViewModel viewModel = dataContext.GetData(NavigationConstants.ViewModel);
            UIViewController viewController;
            if (viewModel == null)
                viewController = (UIViewController)ServiceProvider.IocContainer.Get(source.ViewType);
            else
                viewController = (UIViewController)ViewManager.GetOrCreateView(viewModel, null, dataContext);

            viewController.SetNavigationParameter(parameter);
            bool shouldNavigate = true;
            if (_window != null)
            {
                var controller = _window.RootViewController as UINavigationController;
                if (controller == null)
                {
                    shouldNavigate = false;
                    controller = new MvvmNavigationController(viewController);
                    _window.RootViewController = controller;
                }
                InitializeNavigationController(controller);
            }
            if (shouldNavigate)
            {
                bool animated;
                if (dataContext.TryGetData(NavigationConstants.UseAnimations, out animated))
                {
                    if (viewModel != null)
                        viewModel.Settings.State.AddOrUpdate(NavigationConstants.UseAnimations, animated);
                }
                else
                    animated = UseAnimations;
                if (!ClearNavigationStackIfNeed(viewController, dataContext, animated))
                    NavigationController.PushViewController(viewController, animated);
            }
            var view = viewController as IViewControllerView;
            if (view == null || view.Mediator.IsAppeared)
                RaiseNavigated(viewController, NavigationMode.New, parameter);
            else
                view.Mediator.ViewDidAppearHandler += OnViewDidAppearHandlerNew;
            return true;
        }
        private void AddMapping(IViewMappingItem mappingItem, bool throwOnError, bool rewrite = false)
        {
            List<IViewMappingItem> list;
            if (!_viewTypeToMapping.TryGetValue(mappingItem.ViewType, out list))
            {
                list = new List<IViewMappingItem>();
                _viewTypeToMapping[mappingItem.ViewType] = list;
            }
            list.Add(mappingItem);

            Dictionary<string, IViewMappingItem> value;
            if (!_viewModelToMapping.TryGetValue(mappingItem.ViewModelType, out value))
            {
                value = new Dictionary<string, IViewMappingItem>();
                _viewModelToMapping[mappingItem.ViewModelType] = value;
            }
            IViewMappingItem item;
            string name = mappingItem.Name ?? string.Empty;
            if (value.TryGetValue(name, out item))
            {
                if (throwOnError)
                    throw ExceptionManager.DuplicateViewMapping(item.ViewType, item.ViewModelType, item.Name);
                if (!rewrite)
                    return;
            }
            value[name] = mappingItem;
            if (Tracer.TraceInformation)
                Tracer.Info("The view mapping to view model was created: ({0} ---> {1}), name: {2}",
                    mappingItem.ViewModelType, mappingItem.ViewType, mappingItem.Name);
        }