Пример #1
0
        /// <summary>
        /// Occurs after navigation
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event args.</param>
        protected virtual void OnNavigated(object sender, NavigationEventArgs e)
        {
            if (e.Uri.IsAbsoluteUri)
            {
                return;
            }

            ViewLocator.InitializeComponent(e.Content);

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

            if (viewModel == null)
            {
                return;
            }

            ViewModelBinder.Bind(viewModel, (DependencyObject)e.Content, null);

            TryInjectQueryString(viewModel, e.Content);

            var activator = viewModel as IActivate;

            if (activator != null)
            {
                activator.Activate();
            }
        }
Пример #2
0
        /// <summary>
        /// Binds the view model.
        /// </summary>
        /// <param name="view">The view.</param>
        protected virtual void BindViewModel(DependencyObject view)
        {
            ViewLocator.InitializeComponent(view);

            var viewModel = ViewModelLocator.LocateForView(view);

            if (viewModel == null)
            {
                return;
            }

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

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

            var activator = viewModel as IActivate;

            if (activator != null)
            {
                activator.Activate();
            }
        }
        protected virtual void OnNavigated(object sender, NavigationEventArgs e)
        {
            if (e.Content == null)
            {
                return;
            }

            ViewLocator.InitializeComponent(e.Content);

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

            if (viewModel == null)
            {
                return;
            }

            var view = e.Content as Page;

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

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

            TryInjectParameter(viewModel, e.Parameter);

            ViewModelBinder.Bind(viewModel, view, null);

            var activator = viewModel as IActivate;

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

            var viewAware = viewModel as ViewAware;

            if (viewAware != null)
            {
                EventHandler <object> onLayoutUpdate = null;

                onLayoutUpdate = delegate
                {
                    viewAware.OnViewReady(view);
                    view.LayoutUpdated -= onLayoutUpdate;
                };

                view.LayoutUpdated += onLayoutUpdate;
            }

            GC.Collect(); // Why?
        }
Пример #4
0
        /// <summary>
        /// Occurs after navigation
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event args.</param>
        protected virtual 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 PhoneApplicationPage;

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

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

            ViewModelBinder.Bind(viewModel, page, null);

            TryInjectQueryString(viewModel, page);

            var activator = viewModel as IActivate;

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

            var viewAware = viewModel as ViewAware;

            if (viewAware != null)
            {
                EventHandler onLayoutUpdate = null;
                onLayoutUpdate = delegate {
                    viewAware.OnViewReady(page);
                    page.LayoutUpdated -= onLayoutUpdate;
                };
                page.LayoutUpdated += onLayoutUpdate;
            }

            GC.Collect();
        }
Пример #5
0
        /// <summary>
        ///   Occurs after navigation
        /// </summary>
        /// <param name="sender"> The event sender. </param>
        /// <param name="e"> The event args. </param>
        protected virtual 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);

            var activator = viewModel as IActivate;

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

            GC.Collect();
        }
Пример #6
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();
            }
        }
        /// <summary>
        ///   Occurs after navigation
        /// </summary>
        /// <param name="sender"> The event sender. </param>
        /// <param name="e"> The event args. </param>
        protected virtual void OnNavigated(object sender, NavigationEventArgs e)
        {
            if (e.Uri.IsAbsoluteUri || e.Content == null)
            {
                return;
            }

            ViewLocator.InitializeComponent(e.Content);

            // Do not use LocateForView because Frame control already has its
            // parent's DataContext. Use LocateForViewType instead.
            var viewModel = ViewModelLocator.LocateForViewType(e.Content.GetType());

            //var viewModel = ViewModelLocator.LocateForView(e.Content);
            if (viewModel == null)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    var msg = "'" + e.Content.GetType().FullName + "' must be registered in the bootstrapper (or INavigationService might never get injected)";
                    var exc = new InvalidOperationException(msg);
                    LogManager.GetLog(typeof(FrameAdapter)).Error(exc);
                    throw exc;
                }
                return;
            }

            // Inject dependency properties
            IoC.BuildUp(viewModel);

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

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

            TryInjectQueryString(viewModel, page);
            ViewModelBinder.Bind(viewModel, page, null);

            var activator = viewModel as IActivate;
            if (activator != null)
            {
                activator.Activate();
            }

#if WINDOWS_PHONE || WinRT
            var page      = e.Content as PhoneApplicationPage;
            var viewAware = viewModel as IViewAware;
            if (viewAware != null)
            {
                View.ExecuteOnLayoutUpdated(page, (s, a) => viewAware.OnViewReady(page));
            }
#endif

            GC.Collect();
        }