Exemplo n.º 1
0
        /// <summary>
        /// Creates the page.
        /// </summary>
        /// <param name="rootModel">The root model.</param>
        /// <param name="context">The context.</param>
        /// <param name="settings">The optional popup settings.</param>
        /// <returns>The page.</returns>
        public virtual async Task <Page> CreatePageAsync(object rootModel, object context, IDictionary <string, object> settings)
        {
            var view = EnsurePage(rootModel, ViewLocator.LocateForModel(rootModel, null, context));

            ViewModelBinder.Bind(rootModel, view, context);

            var haveDisplayName = rootModel as IHaveDisplayName;

            if (string.IsNullOrEmpty(view.Title) && haveDisplayName != null && !ConventionManager.HasBinding(view, Page.TitleProperty))
            {
                var binding = new Binding("DisplayName")
                {
                    Mode = BindingMode.TwoWay
                };
                view.SetBinding(Page.TitleProperty, binding);
            }

            ApplySettings(view, settings);

            if (rootModel is IActivate activator)
            {
                await activator.ActivateAsync();
            }

            if (rootModel is IDeactivate deactivatable)
            {
                view.Unloaded += async(s, e) => await deactivatable.DeactivateAsync(true);
            }

            return(view);
        }
Exemplo n.º 2
0
        private async Task PushAsync(Element view, object parameter, bool animated)
        {
            var page = view as Page;

            if (page == null && !(view is ContentView))
            {
                throw new NotSupportedException(String.Format("{0} does not inherit from either {1} or {2}.", view.GetType(), typeof(Page), typeof(ContentView)));
            }

            var viewModel = ViewModelLocator.LocateForView(view);

            if (viewModel != null)
            {
                TryInjectParameters(viewModel, parameter);

                ViewModelBinder.Bind(viewModel, view, null);

                if (viewModel is IActivate activator)
                {
                    await activator.ActivateAsync();
                }
            }

            var contentView = view as ContentView;

            if (contentView != null)
            {
                page = CreateContentPage(contentView, viewModel);
            }

            await navigationPage.PushAsync(page, animated);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a window.
        /// </summary>
        /// <param name="rootModel">The view model.</param>
        /// <param name="isDialog">Whethor or not the window is being shown as a dialog.</param>
        /// <param name="context">The view context.</param>
        /// <param name="settings">The optional popup settings.</param>
        /// <returns>The window.</returns>
        protected virtual async Task <Window> CreateWindowAsync(object rootModel, bool isDialog, object context, IDictionary <string, object> settings)
        {
            var view = EnsureWindow(rootModel, ViewLocator.LocateForModel(rootModel, null, context), isDialog);

            ViewModelBinder.Bind(rootModel, view, context);

            var haveDisplayName = rootModel as IHaveDisplayName;

            if (string.IsNullOrEmpty(view.Title) && haveDisplayName != null && !ConventionManager.HasBinding(view, Window.TitleProperty))
            {
                var binding = new Binding("DisplayName")
                {
                    Mode = BindingMode.TwoWay
                };
                view.SetBinding(Window.TitleProperty, binding);
            }

            ApplySettings(view, settings);

            var conductor = new WindowConductor(rootModel, view);

            await conductor.InitialiseAsync();

            return(view);
        }
Exemplo n.º 4
0
        static void ModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (View.InDesignMode || e.NewValue == null || e.NewValue == e.OldValue)
            {
                return;
            }

            var fe = d as FrameworkElement;

            if (fe == null)
            {
                return;
            }

            View.ExecuteOnLoad(fe, delegate {
                var target = e.NewValue;

                d.SetValue(View.IsScopeRootProperty, true);

#if XFORMS
                var context = fe.Id.ToString("N");
#else
                var context = string.IsNullOrEmpty(fe.Name)
                                  ? fe.GetHashCode().ToString()
                                  : fe.Name;
#endif

                ViewModelBinder.Bind(target, d, context);
            });
        }
Exemplo n.º 5
0
        static void DataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!View.InDesignMode)
            {
                return;
            }

            var enable = d.GetValue(AtDesignTimeProperty);

            if (enable == null || ((bool)enable) == false || e.NewValue == null)
            {
                return;
            }

            var fe = d as FrameworkElement;

            if (fe == null)
            {
                return;
            }
#if XFORMS
            ViewModelBinder.Bind(e.NewValue, d, fe.Id.ToString("N"));
#else
            ViewModelBinder.Bind(e.NewValue, d, string.IsNullOrEmpty(fe.Name) ? fe.GetHashCode().ToString() : fe.Name);
#endif
        }
Exemplo n.º 6
0
        static void OnContextChanged(DependencyObject targetLocation, DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue == e.NewValue)
            {
                return;
            }

            var model = GetModel(targetLocation);

            if (model == null)
            {
                return;
            }

            var view = ViewLocator.LocateForModel(model, targetLocation, e.NewValue);

            if (!SetContentProperty(targetLocation, view))
            {
                Log.Warn("SetContentProperty failed for ViewLocator.LocateForModel, falling back to LocateForModelType");

                view = ViewLocator.LocateForModelType(model.GetType(), targetLocation, e.NewValue);

                SetContentProperty(targetLocation, view);
            }

            ViewModelBinder.Bind(model, view, e.NewValue);
        }
Exemplo n.º 7
0
        static void OnModelChanged(DependencyObject targetLocation, DependencyPropertyChangedEventArgs args)
        {
            if (args.OldValue == args.NewValue)
            {
                return;
            }

            if (args.NewValue != null)
            {
                var context = GetContext(targetLocation);

                var view = ViewLocator.LocateForModel(args.NewValue, targetLocation, context);
                ViewModelBinder.Bind(args.NewValue, view, context);
                if (!SetContentProperty(targetLocation, view))
                {
                    Log.Warn("SetContentProperty failed for ViewLocator.LocateForModel, falling back to LocateForModelType");

                    view = ViewLocator.LocateForModelType(args.NewValue.GetType(), targetLocation, context);

                    SetContentProperty(targetLocation, view);
                }
            }
            else
            {
                SetContentProperty(targetLocation, args.NewValue);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Locates the view model, locates the associate view, binds them and shows it as the root view.
        /// </summary>
        /// <param name="viewModelType">The view model type.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>A task that represents the asynchronous operation.</returns>
        protected async Task DisplayRootViewForAsync(Type viewModelType, CancellationToken cancellationToken)
        {
            Initialize();

            var viewModel = IoC.GetInstance(viewModelType, null);
            var view      = ViewLocator.LocateForModel(viewModel, null, null);

            ViewModelBinder.Bind(viewModel, view, null);

            if (viewModel is IActivate activator)
            {
                await activator.ActivateAsync(cancellationToken);
            }

            Window.Current.Content = view;
            Window.Current.Activate();
        }
Exemplo n.º 9
0
        /// <summary>
        /// Locates the view model, locates the associate view, binds them and shows it as the root view.
        /// </summary>
        /// <param name="viewModelType">The view model type.</param>
        protected async Task DisplayRootViewForAsync(Type viewModelType)
        {
            var viewModel = IoC.GetInstance(viewModelType, null);
            var view      = ViewLocator.LocateForModel(viewModel, null, null);

            if (!(view is Page page))
            {
                throw new NotSupportedException(String.Format("{0} does not inherit from {1}.", view.GetType(), typeof(Page)));
            }

            ViewModelBinder.Bind(viewModel, view, null);

            if (viewModel is IActivate activator)
            {
                await activator.ActivateAsync();
            }

            MainPage = page;
        }
Exemplo n.º 10
0
        /// <summary>
        ///   Occurs after navigation
        /// </summary>
        /// <param name="sender"> The event sender. </param>
        /// <param name="e"> The event args. </param>
        protected virtual async void OnNavigated(object sender, NavigationEventArgs e)
        {
            if (e.Uri.IsAbsoluteUri || e.Content == null)
            {
                return;
            }

            ViewLocator.InitializeComponent(e.Content);

            var viewModel = ViewModelLocator.LocateForView(e.Content);

            if (viewModel == null)
            {
                return;
            }

            var page = e.Content as Page;

            if (page == null)
            {
                throw new ArgumentException("View '" + e.Content.GetType().FullName + "' should inherit from Page or one of its descendents.");
            }

            if (treatViewAsLoaded)
            {
                page.SetValue(View.IsLoadedProperty, true);
            }

            TryInjectParameters(viewModel, e.ExtraData);
            ViewModelBinder.Bind(viewModel, page, null);

            if (viewModel is IActivate activator)
            {
                await activator.ActivateAsync();
            }

            GC.Collect();
        }
Exemplo n.º 11
0
        /// <summary>
        /// Locates the view model, locates the associate view, binds them and shows it as the root view.
        /// </summary>
        /// <param name="viewModelType">The view model type.</param>
        protected void DisplayRootViewFor(Type viewModelType)
        {
            var viewModel = IoC.GetInstance(viewModelType, null);
            var view      = ViewLocator.LocateForModel(viewModel, null, null);

            var page = view as Page;

            if (page == null)
            {
                throw new NotSupportedException(String.Format("{0} does not inherit from {1}.", view.GetType(), typeof(Page)));
            }

            ViewModelBinder.Bind(viewModel, view, null);

            var activator = viewModel as IActivate;

            if (activator != null)
            {
                activator.Activate();
            }

            MainPage = page;
        }
        private Task PushAsync(Element view, object parameter, bool animated)
        {
            var page = view as Page;

            if (page == null)
            {
                throw new NotSupportedException(String.Format("{0} does not inherit from {1}.", view.GetType(), typeof(Page)));
            }

            var viewModel = ViewModelLocator.LocateForView(view);

            if (viewModel != null)
            {
                TryInjectParameters(viewModel, parameter);

                ViewModelBinder.Bind(viewModel, view, null);
            }

            page.Appearing    += (s, e) => ActivateView(page);
            page.Disappearing += (s, e) => DeactivateView(page);

            return(navigationPage.PushAsync(page, animated));
        }
Exemplo n.º 13
0
        /// <summary>
        /// Shows a popup at the current mouse position.
        /// </summary>
        /// <param name="rootModel">The root model.</param>
        /// <param name="context">The view context.</param>
        /// <param name="settings">The optional popup settings.</param>
        public virtual async Task ShowPopupAsync(object rootModel, object context = null, IDictionary <string, object> settings = null)
        {
            var popup = CreatePopup(rootModel, settings);
            var view  = ViewLocator.LocateForModel(rootModel, popup, context);

            popup.Child = view;
            popup.SetValue(View.IsGeneratedProperty, true);

            ViewModelBinder.Bind(rootModel, popup, null);
            Action.SetTargetWithoutContext(view, rootModel);

            if (rootModel is IActivate activator)
            {
                await activator.ActivateAsync();
            }

            if (rootModel is IDeactivate deactivator)
            {
                popup.Closed += async(s, e) => await deactivator.DeactivateAsync(true);
            }

            popup.IsOpen = true;
            popup.CaptureMouse();
        }
Exemplo n.º 14
0
        /// <summary>
        /// Binds the view model.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="viewModel">The view model.</param>
        protected virtual async Task BindViewModel(DependencyObject view, object viewModel = null)
        {
            ViewLocator.InitializeComponent(view);

            viewModel = viewModel ?? ViewModelLocator.LocateForView(view);

            if (viewModel == null)
            {
                return;
            }

            if (treatViewAsLoaded)
            {
                view.SetValue(View.IsLoadedProperty, true);
            }

            TryInjectParameters(viewModel, CurrentParameter);
            ViewModelBinder.Bind(viewModel, view, null);

            if (viewModel is IActivate activator)
            {
                await activator.ActivateAsync();
            }
        }