public static IMvxViewModel ReflectionGetViewModel(this IMvxView view) { if (view == null) { return(null); } #if NETFX_CORE var propertyInfo = view.GetType().GetTypeInfo().RecursiveGetDeclaredProperty("ViewModel"); if (propertyInfo == null) { return(null); } return((IMvxViewModel)propertyInfo.GetValue(view, new object[] { })); #else var propertyInfo = view.GetType().GetProperty("ViewModel"); if (propertyInfo == null) { return(null); } return((IMvxViewModel)propertyInfo.GetGetMethod().Invoke(view, new object[] {})); #endif }
public static void OnViewNewIntent <TViewModel>(this IMvxView <TViewModel> view, Func <TViewModel> viewModelLoader) where TViewModel : class, IMvxViewModel { var newViewModel = viewModelLoader(); view.ReplaceViewModel(newViewModel); }
public static void OnViewDestroy <TViewModel>(this IMvxView <TViewModel> view) where TViewModel : class, IMvxViewModel { if (view.ViewModel != null) { view.ViewModel.UnRegisterView(view); } }
public void RegisterView(IMvxView view) { lock (this) { _views[view] = true; SafeFireEvent(ViewRegistered); } }
public void UnRegisterView(IMvxView view) { lock (this) { _views.Remove(view); SafeFireEvent(ViewUnRegistered); } }
internal static Type GetViewModelType(this IMvxView view) { var viewType = view.GetType(); var props = viewType.GetProperties(BindingFlags.Public | BindingFlags.Instance); var prop = props.Where(p => p.Name == "ViewModel").FirstOrDefault(); return(prop?.PropertyType); }
public static void verify(IMvxView view, Type viewModel = null) { verifyApp(view); if (_instance == null && !_initialized) { Task.Factory.StartNew(() => startInitialization(view, viewModel)); } }
private static bool TryUnregisterView <T>(this IMvxView <T> view) where T : class, IMvxViewModel { if (view.ViewModel == null) { return(false); } view.ViewModel.UnRegisterView(view); return(true); }
public static IMvxBundle CreateSaveStateBundle(this IMvxView view) { var viewModel = view.ViewModel; if (viewModel == null) { return(new MvxBundle()); } return(viewModel.SaveStateBundle()); }
public static void EnsureViewModel <TViewModel>(IMvxView <TViewModel> mvxView) where TViewModel : class, IMvxViewModel { if (mvxView.ViewModel == null) { try { mvxView.ViewModel = Mvx.IoCConstruct <TViewModel>(); } catch (Exception e) { Console.WriteLine(e); } } }
protected override async Task <Boolean> ShowContentView(FrameworkElement element, MvxContentPresentationAttribute attribute, MvxViewModelRequest request) { try { // Everything that passes here should be a view IMvxView view = element as IMvxView; IMesManager manager = Mvx.IoCProvider.Resolve <IMesManager>(); // from which we can now get the view model. switch (view.ViewModel) { case IMesDocument document: // Try to set view, this is needed for DocumentManager IMesDocument docViewModel = (IMesDocument)view.ViewModel; docViewModel.View = view; // Needed for Binding with AvalonDock docViewModel.ViewAppearing(); docViewModel.ViewAppeared(); // Add to manager model manager.Documents.Add(docViewModel); _log.Trace($"Add {document.ToString()} to IManager.Documents"); return(true); case IMesTool tool: // Try to set view, this is needed for DocumentManager IMesTool toolViewModel = (IMesTool)view.ViewModel; toolViewModel.View = view; // Needed for Binding with AvalonDock toolViewModel.ViewAppearing(); toolViewModel.ViewAppeared(); // Add to manager model manager.Tools.Add(toolViewModel); _log.Trace($"Add {tool.ToString()} to IManager.Tools"); return(true); default: _log.Trace($"Passing to parent {view.ViewModel.ToString()}"); return(await base.ShowContentView(element, attribute, request)); } } catch (Exception exception) { if (_log == null) { _log = Mvx.IoCProvider.Resolve <IMvxLog>(); } _log.ErrorException("Error seen during navigation request to {0} - error {1}", exception, request.ViewModelType.Name, exception.ToLongString()); return(false); } }
public static void OnViewCreate <TViewModel>(this IMvxView <TViewModel> view, Func <TViewModel> viewModelLoader) where TViewModel : class, IMvxViewModel { if (view.ViewModel != null) { return; } var viewModel = viewModelLoader(); viewModel.RegisterView(view); view.ViewModel = (TViewModel)viewModel; }
private static void ReplaceViewModel <T>(this IMvxView <T> view, T viewModel) where T : class, IMvxViewModel { if (view.ViewModel == viewModel) { return; } if (view.ViewModel != null) { view.TryUnregisterView(); } view.TryRegisterView(); }
public override Task <bool> ChangePresentation(MvxPresentationHint hint) { if (hint is MvxClosePresentationHint) { var viewModel = (hint as MvxClosePresentationHint).ViewModelToClose; Activity activity = this.CurrentActivity; IMvxView mvxView = activity as IMvxView; activity.Finish(); } return(Task.FromResult(true)); }
public override void ChangePresentation(MvxPresentationHint hint) { if (hint is MvxClosePresentationHint) { var viewModel = (hint as MvxClosePresentationHint).ViewModelToClose; Activity activity = this.Activity; IMvxView mvxView = activity as IMvxView; activity.Finish(); return; } }
public static void FixupTracking <T>(this IMvxView <T> view, T viewModel, Action setViewModelCallback) where T : class, IMvxViewModel { if (view.ViewModel == viewModel) { return; } if (view.ViewModel != null) { view.TryUnregisterView(); } setViewModelCallback(); view.TryRegisterView(); }
public static IMvxViewModel ReflectionGetViewModel(this IMvxView view) { if (view == null) { return(null); } var propertyInfo = view.GetType().GetProperty("ViewModel"); if (propertyInfo == null) { return(null); } return((IMvxViewModel)propertyInfo.GetGetMethod().Invoke(view, new object[] {})); }
public static Type FindAssociatedViewModelTypeOrNull(this IMvxView view) { if (view == null) { return(null); } IMvxViewModelTypeFinder associatedTypeFinder; if (!Mvx.TryResolve(out associatedTypeFinder)) { MvxTrace.Trace( "No view model type finder available - assuming we are looking for a splash screen - returning null"); return(typeof(MvxNullViewModel)); } return(associatedTypeFinder.FindTypeOrNull(view.GetType())); }
public static Type?FindAssociatedViewModelTypeOrNull(this IMvxView view) { if (view == null) { throw new ArgumentNullException(nameof(view)); } IMvxViewModelTypeFinder associatedTypeFinder; if (!Mvx.IoCProvider.TryResolve(out associatedTypeFinder)) { MvxLogHost.Default?.Log(LogLevel.Trace, "No view model type finder available - assuming we are looking for a splash screen - returning null"); return(typeof(MvxNullViewModel)); } return(associatedTypeFinder.FindTypeOrNull(view.GetType())); }
private static IMvxViewModel LoadViewModel(IMvxView androidView) { var viewModelType = androidView.GetType() .GetProperties() .Where(p => p.Name == nameof(androidView.ViewModel)) .First(vmp => vmp.PropertyType != typeof(IMvxViewModel)) .PropertyType; var request = new MvxViewModelRequest(viewModelType); var loader = Mvx.Resolve <IMvxViewModelLoader>(); var viewModel = loader.LoadViewModel(request, null); if (viewModel == null) { Mvx.Resolve <IMvxLog>().Warn("ViewModel not loaded for {0}", request.ViewModelType.FullName); } return(viewModel); }
public override void ChangePresentation(MvxPresentationHint hint) { if (HandlePresentationChange(hint)) { return; } Activity activity = this.Activity; if (activity is MvxFormsApplicationActivity) { if (hint is MvxClosePresentationHint) { var mainPage = MvxFormsApp.MainPage as NavigationPage; if (mainPage.Navigation.NavigationStack.Count == 1) { // Clear the MainPage ////MvxFormsApp.MainPage.Navigation.RemovePage(MvxFormsApp.MainPage.Navigation.NavigationStack[0]); // "System.InvalidOperationException: Cannot remove root page when it is also the currently displayed page." IMvxView mvxView = activity as IMvxView; activity.Finish(); } else { var page = mainPage.PopAsync(); System.Diagnostics.Debug.WriteLine(page); } } } else { base.ChangePresentation(hint); } }
public static void OnViewCreate(this IMvxView view, Func <IMvxViewModel> viewModelLoader) { // note - we check the DataContent before the ViewModel to avoid casting errors // in the case of 'simple' binding code if (view.DataContext != null) { return; } if (view.ViewModel != null) { return; } var viewModel = viewModelLoader(); if (viewModel == null) { MvxTrace.Warning("ViewModel not loaded for view {0}", view.GetType().Name); return; } view.ViewModel = viewModel; }
public static async ValueTask OnViewCreate(this IMvxView view, Func <ValueTask <IMvxViewModel?> > viewModelLoader) { // note - we check the DataContent before the ViewModel to avoid casting errors // in the case of 'simple' binding code if (view.DataContext != null) { return; } if (view.ViewModel != null) { return; } var viewModel = await viewModelLoader().ConfigureAwait(false); if (viewModel == null) { MvxLog.Instance.Warn("ViewModel not loaded for view {0}", view.GetType().Name); return; } view.ViewModel = viewModel; }
public static void startInitialization(IMvxView view = null, Type viewModelType = null) { if (_initialized) return; lock (_initLock) { try { _instance.Initialize(); _initialized = true; } catch (Exception ex) { throw ex; } finally { _initializationCompletion.SetResult(_instance); _instance.InitializationComplete(); } } }
public static IMvxViewModel?ReflectionGetViewModel(this IMvxView view) { var propertyInfo = view?.GetType().GetProperty("ViewModel"); return(propertyInfo?.GetGetMethod().Invoke(view, Array.Empty <object>()) as IMvxViewModel); }
public static void OnViewDestroy(this IMvxView view) { // nothing needed currently }
public static void OnViewNewIntent(this IMvxView view, Func <IMvxViewModel> viewModelLoader) { MvxTrace.Warning( "OnViewNewIntent isn't well understood or tested inside MvvmCross - it's not really a cross-platform concept."); throw new MvxException("OnViewNewIntent is not implemented"); }
public static IMvxViewModel ReflectionGetViewModel(this IMvxView view) { var propertyInfo = view?.GetType().GetProperty("ViewModel"); return((IMvxViewModel)propertyInfo?.GetGetMethod().Invoke(view, new object[] { })); }
public static ServicesExtensionPoint Services(this IMvxView viewController) { return(new ServicesExtensionPoint()); }