Exemple #1
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();
        }
Exemple #2
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;
        }
Exemple #3
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();
        }
        /// <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();
            }
        }