public Task InitializeViewAsync(IViewModel viewModel, object view, IDataContext context = null) { var tcs = new TaskCompletionSource <object>(); ThreadManager.InvokeOnUiThreadAsync(() => { if (context == null) { context = DataContext.Empty; } view = ToolkitExtensions.GetUnderlyingView <object>(view); var oldView = viewModel.GetCurrentView <object>(false); if (ReferenceEquals(oldView, view)) { tcs.SetResult(null); return; } if (oldView != null) { CleanupViewInternal(viewModel, oldView, context); } InitializeView(viewModel, view, context); ViewInitialized?.Invoke(this, new ViewInitializedEventArgs(view, viewModel, context)); tcs.SetResult(null); }); return(tcs.Task); }
public Task InitializeViewAsync(IViewModel viewModel, object view, IDataContext context = null) { var tcs = new TaskCompletionSource <object>(); ThreadManager.InvokeOnUiThreadAsync(() => { if (context == null) { context = DataContext.Empty; } view = ToolkitExtensions.GetUnderlyingView <object>(view); var oldView = viewModel.Settings.Metadata.GetData(ViewModelConstants.View); if (ReferenceEquals(oldView, view)) { tcs.SetResult(null); return; } if (oldView != null) { CleanupViewInternal(viewModel, oldView, context); } InitializeView(viewModel, view, context); Action <IViewManager, IViewModel, object, IDataContext> handler = ViewInitialized; if (handler != null) { handler(this, viewModel, view, context); } tcs.SetResult(null); }); return(tcs.Task); }
public Task <bool> GoToStateAsync(object view, string stateName, bool useTransitions, IDataContext context) { Should.NotBeNull(view, "view"); #if WPF var control = ToolkitExtensions.GetUnderlyingView <object>(view) as FrameworkElement; #else var control = ToolkitExtensions.GetUnderlyingView <object>(view) as Control; #endif if (control == null) { return(Empty.FalseTask); } var tcs = new TaskCompletionSource <bool>(); _threadManager.InvokeOnUiThreadAsync(() => { #if WPF var result = System.Windows.VisualStateManager.GoToState(control, stateName, useTransitions); #elif !WINDOWSCOMMON var result = System.Windows.VisualStateManager.GoToState(control, stateName, useTransitions); #else var result = Windows.UI.Xaml.VisualStateManager.GoToState(control, stateName, useTransitions); #endif tcs.SetResult(result); }); return(tcs.Task); }
public virtual IViewModel TryTakeViewModelFromCache(INavigationContext context, object view) { view = ToolkitExtensions.GetUnderlyingView <object>(view); var type = view.GetType(); List <IViewModel> list; if (!_cachedViewModels.TryGetValue(type, out list) || list == null || list.Count == 0) { if (Tracer.TraceInformation) { Tracer.Info("Navigation cache - the view model for the view {0} is not found in the cache, navigation mode: {1}", type, context.NavigationMode); } return(null); } IViewModel vm = list[0]; list.RemoveAt(0); if (list.Count == 0) { _cachedViewModels.Remove(type); } if (Tracer.TraceInformation) { Tracer.Info("Navigation cache - the view model {0} for the view {1} was taken from the cache, navigation mode: {2}", vm.GetType(), type, context.NavigationMode); } return(vm); }
public static TView GetCurrentView <TView>([NotNull] this IViewModel viewModel, bool underlyingView = true) where TView : class { Should.NotBeNull(viewModel, nameof(viewModel)); var view = viewModel.Settings.Metadata.GetData(ViewModelConstants.View); if (view == null || !underlyingView) { return((TView)view); } return(ToolkitExtensions.GetUnderlyingView <TView>(view)); }
protected virtual void CleanupView([NotNull] IViewModel viewModel, [NotNull] object view, [NotNull] IDataContext context) { InitializeViewInternal(null, view, context); viewModel.Settings.Metadata.Remove(ViewModelConstants.View); ReflectionExtensions.GetViewProperty(viewModel.GetType())?.SetValue <object>(viewModel, null); viewModel.Unsubscribe(ToolkitExtensions.GetUnderlyingView <object>(view)); if (ServiceProvider.AttachedValueProvider.Contains(view, ViewManagerCreatorPath)) { ServiceProvider.AttachedValueProvider.Clear(view, ViewManagerCreatorPath); (view as IDisposable)?.Dispose(); } }
public Task CleanupViewAsync(IViewModel viewModel, IDataContext context = null) { Should.NotBeNull(viewModel, nameof(viewModel)); object view = viewModel.GetCurrentView <object>(false); if (view == null) { return(Empty.Task); } var tcs = new TaskCompletionSource <object>(); ThreadManager.InvokeOnUiThreadAsync(() => { if (context == null) { context = DataContext.Empty; } CleanupViewInternal(viewModel, ToolkitExtensions.GetUnderlyingView <object>(view), context); tcs.SetResult(null); }, OperationPriority.Low); return(tcs.Task); }
public virtual void TryCacheViewModel(INavigationContext context, object view, IViewModel viewModel) { if (context.NavigationMode == NavigationMode.Back) { return; } view = ToolkitExtensions.GetUnderlyingView <object>(view); Type type = view.GetType(); List <IViewModel> list; if (!_cachedViewModels.TryGetValue(type, out list)) { list = new List <IViewModel>(); _cachedViewModels[type] = list; } list.Insert(0, viewModel); if (Tracer.TraceInformation) { Tracer.Info("Navigation cache - the view model {0} was cached, navigation mode: {1}, view: {2}", viewModel.GetType(), context.NavigationMode, type); } }
public virtual bool Navigate(IViewMappingItem source, string parameter, IDataContext dataContext) { Should.NotBeNull(source, nameof(source)); if (dataContext == null) { dataContext = DataContext.Empty; } bool bringToFront; dataContext.TryGetData(NavigationProviderConstants.BringToFront, out bringToFront); if (!RaiseNavigating(new NavigatingCancelEventArgs(source, bringToFront ? NavigationMode.Refresh : NavigationMode.New, parameter))) { return(false); } var activity = PlatformExtensions.CurrentActivity ?? SplashScreenActivityBase.Current; var context = activity ?? Application.Context; var intent = new Intent(context, source.ViewType); if (activity == null) { intent.AddFlags(ActivityFlags.NewTask); } else if (dataContext.GetData(NavigationConstants.ClearBackStack)) { if (PlatformExtensions.IsApiLessThanOrEqualTo10) { intent.AddFlags(ActivityFlags.NewTask | ActivityFlags.ClearTop); } else { intent.AddFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask); } ServiceProvider.EventAggregator.Publish(this, MvvmActivityMediator.FinishActivityMessage.Instance); dataContext.AddOrUpdate(NavigationProviderConstants.InvalidateAllCache, true); } if (parameter != null) { intent.PutExtra(ParameterString, parameter); } if (bringToFront) { _isReorder = true; //http://stackoverflow.com/questions/20695522/puzzling-behavior-with-reorder-to-front //http://code.google.com/p/android/issues/detail?id=63570#c2 bool closed = false; if (PlatformExtensions.IsApiGreaterThanOrEqualTo19) { var viewModel = dataContext.GetData(NavigationConstants.ViewModel); if (viewModel != null) { var view = viewModel.Settings.Metadata.GetData(ViewModelConstants.View); var activityView = ToolkitExtensions.GetUnderlyingView <object>(view) as Activity; if (activityView != null && activityView.IsTaskRoot) { var message = new MvvmActivityMediator.FinishActivityMessage(viewModel); ServiceProvider.EventAggregator.Publish(this, message); closed = message.Finished; } } } if (!closed) { intent.AddFlags(ActivityFlags.ReorderToFront); } dataContext.AddOrUpdate(NavigationProviderConstants.InvalidateCache, true); } _isNew = true; _parameter = parameter; StartActivity(context, intent, source, dataContext); return(true); }