/// <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) { activator.Activate(); } // frame.NavigateToType(typeof(view), true, new FrameNavigationOptions() { IsNavigationStackEnabled = true }); }
public bool Navigate(Type pageType) { // Do guardclose and deactivate stuff here by looking at shell.ActiveItem // e.g. var guard = shell.ActiveItem as IGuardClose; if (guard != null) { var shouldCancel = false; guard.CanClose(result => { shouldCancel = !result; }); if (shouldCancel) { e.Cancel = true; return; } } // etc // Obviously since the guard is async you'd have to not call this code right // here but I've just stuck it in here as an example // You might get away with calling shell.ActivateItem(pageType) as I'm not sure // if the CM conductor in RT would resolve this for you, but if it doesnt... // Init the view and then resolve the VM type ViewLocator.InitializeComponent(pageType); var viewModel = ViewModelLocator.LocateForView(pageType); // Activate the VM in the shell) shell.ActivateItem(viewModel); }
/// <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(); } }
public bool Navigate(Type pageType) { // Do guardclose and deactivate stuff here by looking at shell.ActiveItem // e.g. var guard = shell.ActiveItem as IGuardClose; if (guard != null) { var shouldCancel = false; guard.CanClose(result => { shouldCancel = !result; }); if (shouldCancel) { e.Cancel = true; return; } } // etc // Obviously since the guard is probably async (assume it is, if not you are ok to continue!) you'd have to not call this code right // here but I've just stuck it in here as an example // edit: looking at the code above (the guard code) it looks like this is all sync so the below code should be fine // You might get away with calling shell.ActivateItem(pageType) as I'm not sure // if the viewmodel binder in RT would resolve this all for you, but if it doesnt... // Init the view and then resolve the VM type ViewLocator.InitializeComponent(pageType); var viewModel = ViewModelLocator.LocateForView(pageType); // Activate the VM in the shell) shell.ActivateItem(viewModel); }
private UIElement GetOrCreateViewType(Type viewType) { Contract.Requires(viewType != null); var cached = IoC.GetAllInstances(viewType).OfType <UIElement>().FirstOrDefault(); if (cached != null) { ViewLocator.InitializeComponent(cached); return(cached); } if (viewType.IsInterface || viewType.IsAbstract || !typeof(UIElement).IsAssignableFrom(viewType)) { return(new TextBlock { Text = string.Format("Cannot create {0}.", viewType.FullName) }); } var newInstance = (UIElement)Activator.CreateInstance(viewType); var frameworkElement = newInstance as FrameworkElement; if (frameworkElement != null) { var themeManager = (IThemeManager)this.GetInstance(typeof(IThemeManager), null); frameworkElement.Resources.MergedDictionaries.Add(themeManager.GetThemeResources()); } ViewLocator.InitializeComponent(newInstance); return(newInstance); }
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 PhoneApplicationPage or one of its descendents."); } TryInjectQueryString(viewModel, page); ViewModelBinder.Bind(viewModel, page, null); var activator = viewModel as IActivate; if (activator != null) { activator.Activate(); } GC.Collect(); }
/// <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 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(); } var viewAware = viewModel as ViewAware; if (viewAware != null) { EventHandler onLayoutUpdate = null; onLayoutUpdate = delegate { //viewAware.OnViewReady(page); page.LayoutUpdated -= onLayoutUpdate; }; page.LayoutUpdated += onLayoutUpdate; } GC.Collect(); }
private void FrameOnNavigated(object sender, NavigationEventArgs e) { ViewLocator.InitializeComponent(e.Content); var viewModel = ViewModelLocator.LocateForView(e.Content); if (viewModel == null) { return; } ParametersHelper.InjectParameter(viewModel, e.Parameter as IDictionary <string, object>); ViewModelBinder.Bind(viewModel, e.Content as DependencyObject, null); ScreenExtensions.TryActivate(viewModel); this.Changed?.Invoke(this, new NavigationStepChangedEventArgs(viewModel)); }
public override void Load(Uri targetUri, Uri currentUri) { //tamper protection Boolean isMatchRequired = targetUri.OriginalString.Contains(Constants.ADDNEW) && targetUri.OriginalString.Contains(Constants.TRACKING_KEY); try { PersitableScreen viewmodel; _conductor.ViewModels.TryGetValue(targetUri.OriginalString, out viewmodel); if (isMatchRequired && viewmodel == null) { throw new InvalidOperationException("Uri missing from cache"); } if (viewmodel != null) { Object result = viewmodel.GetView(); if (result is Uri) { base.Complete((Uri)result); } else { base.Complete(result); } } else { _result = this.Loader.BeginLoad(targetUri, currentUri, (res) => { try { LoadResult loadResult = this.Loader.EndLoad(res); if (loadResult.RedirectUri != null) { base.Complete(loadResult.RedirectUri); } else { DependencyObject content = loadResult.LoadedContent as DependencyObject; if (content != null) { String currentOriginalString = null; if (currentUri == null || String.IsNullOrWhiteSpace(currentUri.OriginalString)) { currentOriginalString = Constants.HOME_URI_STRING; } else { currentOriginalString = currentUri.OriginalString; } String targetUriString = targetUri.OriginalString; String trackingKey = String.Empty; if (targetUri.OriginalString.EndsWith(Constants.ADD_NEW_QUERYSTRING)) { trackingKey = Guid.NewGuid().ToString(); targetUriString = String.Concat(targetUriString.Replace(Constants.ADD_NEW_QUERYSTRING, String.Empty), Constants.TRACKING_KEY_QUERYSTRING, trackingKey); } ViewLocator.InitializeComponent(content); var vm = ViewModelLocator.LocateForView(content); if (vm == null) { // to avoid shellviewmodel to be set on cild screens datacontext property vm = new PersitableScreen(); } viewmodel = vm as PersitableScreen; if (viewmodel != null) { viewmodel.ViewType = content.GetType(); viewmodel.UriString = targetUriString; viewmodel.ParentUriOriginalString = currentOriginalString; viewmodel.TrackingKey = trackingKey; // TODO: Add query string Uri auri = new Uri("dummy://" + targetUriString, UriKind.Absolute); string query = auri.Query; // _conductor.NavigationService.TryInjectQueryString(viewmodel,content); } // dont rebind when you're bound to yourself if (vm != content) { ViewModelBinder.Bind(vm, content, null); } base.Complete(content); } else { throw new InvalidOperationException("LoadedContent was null or not a DependencyObject"); } return; } } catch (Exception e) { base.Error(e); return; } }, null); } } catch (Exception e) { base.Error(e); return; } }