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); }
public static async Task PopupAsync <T>(T vm = null) where T : class { var view = ViewLocator.LocateForModelType(typeof(T), null, null); if (view == null) { throw new Exception(string.Format("Can't locate view for type : {0}", typeof(T).Name)); } if (!(view is PopupPage)) { throw new NotSupportedException("PopupAsync only support PopupPage"); } if (vm == null) { vm = IoC.Get <T>(); } if (vm != null) { ViewModelBinder.Bind(vm, view, null); } await PopupNavigation.PushAsync((PopupPage)view); }
private static void PopulateTabControl(TabablzControl tabablzControl, LayoutStructureTabSet layoutStructureTabSet) { bool wasAnyTabSelected = false; foreach (var tabItem in layoutStructureTabSet.TabItems) { var view = ViewLocator.LocateForModelType(tabItem.ViewModelType, null, null); var vm = typeof(IoC).GetMethod("Get").MakeGenericMethod(tabItem.ViewModelType).Invoke(tabablzControl, new Object[] { null }); ViewModelBinder.Bind(vm, view, null); (tabablzControl.DataContext as ShellViewModel).ActivateItem(vm as IScreen); tabablzControl.AddToSource(view); if (tabItem.Id == layoutStructureTabSet.SelectedTabItemId) { tabablzControl.Dispatcher.BeginInvoke(new System.Action(() => { tabablzControl.SetCurrentValue(Selector.SelectedItemProperty, view); }), DispatcherPriority.Loaded); wasAnyTabSelected = true; } } if (!wasAnyTabSelected && tabablzControl.Items.Count != 0) { tabablzControl.Dispatcher.BeginInvoke(new System.Action(() => { tabablzControl.SetCurrentValue(Selector.SelectedItemProperty, tabablzControl.Items.OfType <System.Windows.Controls.UserControl>().First()); }), DispatcherPriority.Loaded); wasAnyTabSelected = true; } }
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); // Trialing binding before setting content in Xamarin Forms #if XFORMS ViewModelBinder.Bind(args.NewValue, view, context); #endif 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); } #if !XFORMS ViewModelBinder.Bind(args.NewValue, view, context); #endif } else { SetContentProperty(targetLocation, args.NewValue); } }
public void NavigateToViewModel(Type viewModelType) { var view = ViewLocator.LocateForModelType(viewModelType, null, null); var viewModel = ViewModelLocator.LocateForView(view); ViewModelBinder.Bind(viewModel, view, null); var activator = viewModel as IActivate; activator?.Activate(); frame.Navigate(view); }
/// <summary> /// 展示对话框 /// </summary> public static async Task <bool?> ShowDialogAsync(this INavigationService navigationService, Screen viewModel) { Element view = ViewLocator.LocateForModelType(viewModel.GetType(), null, null); Dialog dialog = view as Dialog; #region # 验证 if (dialog == null) { throw new NotSupportedException($"\"{view.GetType()}\"未继承\"{typeof(Dialog)}\"!"); } #endregion ViewModelBinder.Bind(viewModel, view, null); NavigationPage navigationPage = ResolveMediator.Resolve <NavigationPage>(); bool? result = await navigationPage.Navigation.ShowPopupAsync(dialog); return(result); }
private static ViewModelPair ForViewModel(Type viewModelType, object parameters = null) { try { var view = ViewLocator.LocateForModelType(viewModelType, null, null) as Page; if (view == null) { throw new NotSupportedException(String.Format("{0} does not inherit from {1}.", view.GetType(), typeof(Page))); } var viewModel = ViewModelLocator.LocateForView(view) as ViewModelBase; ViewModelBinder.Bind(viewModel, view, parameters); TrySetParameters(viewModel, parameters); return(new ViewModelPair { View = view, ViewModel = viewModel }); } catch (Exception e) { throw; } }
/// <inheritdoc /> public Task NavigateToViewModelInstanceAsync <TViewModel>(TViewModel viewModel, bool animated = true) { Element element; try { element = ViewLocator.LocateForModelType(typeof(TViewModel), null, null); } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } var page = element as Page; if (page == null && !(element is ContentView)) { throw new NotSupportedException( $"{element.GetType()} does not inherit from either {typeof(Page)} or {typeof(ContentView)}."); } try { ViewModelBinder.Bind(viewModel, element, null); } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } if (element is ContentView view) { page = CreateContentPage(view, viewModel); } return(_navigationPage.PushAsync(page, animated)); }
public async Task NavigateModalToViewModelAsync <TViewModel>(object parameter = null, bool animated = true) { var view = ViewLocator.LocateForModelType(typeof(TViewModel), null, null); await PushModalAsync(view, parameter, animated); }