/// <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(); } GC.Collect(); // Why? }
/// <summary> /// Shows a popup at the current mouse position. /// </summary> /// <param name="rootModel">The root model.</param> /// <param name="context">The view context.</param> /// <param name="settings">The optional popup settings.</param> public virtual void ShowPopup(object rootModel, object context = null, IDictionary <string, object> settings = null) { var popup = CreatePopup(rootModel, settings); var view = ViewLocator.LocateForModel(rootModel, popup, context); popup.Child = view; popup.SetValue(View.IsGeneratedProperty, true); ViewModelBinder.Bind(rootModel, popup, null); Action.SetTargetWithoutContext(view, rootModel); var activatable = rootModel as IActivate; if (activatable != null) { activatable.Activate(); } var deactivator = rootModel as IDeactivate; if (deactivator != null) { popup.Closed += delegate { deactivator.Deactivate(true); }; } popup.IsOpen = true; popup.CaptureMouse(); }
/// <summary> /// Shows a toast notification for the specified model. /// </summary> /// <param name="rootModel">The root model.</param> /// <param name="durationInMilliseconds">How long the notification should appear for.</param> /// <param name="context">The context.</param> /// <param name="settings">The optional notification settings.</param> public virtual void ShowNotification(object rootModel, int durationInMilliseconds, object context = null, IDictionary <string, object> settings = null) { var window = new NotificationWindow(); var view = ViewLocator.LocateForModel(rootModel, window, context); ViewModelBinder.Bind(rootModel, view, null); window.Content = (FrameworkElement)view; ApplySettings(window, settings); var activator = rootModel as IActivate; if (activator != null) { activator.Activate(); } var deactivator = rootModel as IDeactivate; if (deactivator != null) { window.Closed += delegate { deactivator.Deactivate(true); }; } window.Show(durationInMilliseconds); }
static void ModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (View.InDesignMode || e.NewValue == null || e.NewValue == e.OldValue) { return; } var fe = d as FrameworkElement; if (fe == null) { return; } View.ExecuteOnLoad(fe, delegate { var target = e.NewValue; d.SetValue(View.IsScopeRootProperty, true); #if XFORMS var context = fe.Id.ToString("N"); #else var context = string.IsNullOrEmpty(fe.Name) ? fe.GetHashCode().ToString() : fe.Name; #endif ViewModelBinder.Bind(target, d, context); }); }
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); }
/// <summary> /// Creates the page. /// </summary> /// <param name="rootModel">The root model.</param> /// <param name="context">The context.</param> /// <param name="settings">The optional popup settings.</param> /// <returns>The page.</returns> public virtual async Task <Page> CreatePageAsync(object rootModel, object context, IDictionary <string, object> settings) { var view = EnsurePage(rootModel, ViewLocator.LocateForModel(rootModel, null, context)); ViewModelBinder.Bind(rootModel, view, context); var haveDisplayName = rootModel as IHaveDisplayName; if (string.IsNullOrEmpty(view.Title) && haveDisplayName != null && !ConventionManager.HasBinding(view, Page.TitleProperty)) { var binding = new Binding("DisplayName") { Mode = BindingMode.TwoWay }; view.SetBinding(Page.TitleProperty, binding); } ApplySettings(view, settings); if (rootModel is IActivate activator) { await activator.ActivateAsync(); } if (rootModel is IDeactivate deactivatable) { view.Unloaded += async(s, e) => await deactivatable.DeactivateAsync(true); } return(view); }
static void DataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (!View.InDesignMode) { return; } var enable = d.GetValue(AtDesignTimeProperty); if (enable == null || ((bool)enable) == false || e.NewValue == null) { return; } var fe = d as FrameworkElement; if (fe == null) { return; } #if XFORMS ViewModelBinder.Bind(e.NewValue, d, fe.Id.ToString("N")); #else ViewModelBinder.Bind(e.NewValue, d, string.IsNullOrEmpty(fe.Name) ? fe.GetHashCode().ToString() : fe.Name); #endif }
static void ModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (Execute.InDesignMode || e.NewValue == null || e.NewValue == e.OldValue) { return; } var fe = d as FrameworkElement; if (fe == null) { return; } View.ExecuteOnLoad(fe, delegate { var target = e.NewValue; var containerKey = e.NewValue as string; if (containerKey != null) { target = IoC.GetInstance(null, containerKey); } d.SetValue(View.IsScopeRootProperty, true); var context = string.IsNullOrEmpty(fe.Name) ? fe.GetHashCode().ToString() : fe.Name; ViewModelBinder.Bind(target, d, context); }); }
/// <summary> /// Creates the page. /// </summary> /// <param name="rootModel">The root model.</param> /// <param name="context">The context.</param> /// <param name="settings">The optional popup settings.</param> /// <returns>The page.</returns> public virtual Page CreatePage(object rootModel, object context, IDictionary <string, object> settings) { var view = EnsurePage(rootModel, ViewLocator.LocateForModel(rootModel, null, context)); ViewModelBinder.Bind(rootModel, view, context); var haveDisplayName = rootModel as IHaveDisplayName; if (haveDisplayName != null && !ConventionManager.HasBinding(view, Page.TitleProperty)) { var binding = new Binding("DisplayName") { Mode = BindingMode.TwoWay }; view.SetBinding(Page.TitleProperty, binding); } ApplySettings(view, settings); var activatable = rootModel as IActivate; if (activatable != null) { activatable.Activate(); } var deactivatable = rootModel as IDeactivate; if (deactivatable != null) { view.Unloaded += (s, e) => deactivatable.Deactivate(true); } return(view); }
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); ViewModelBinder.Bind(args.NewValue, view, context); 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); } } else { SetContentProperty(targetLocation, args.NewValue); } }
/// <summary> /// Creates a window. /// </summary> /// <param name="rootModel">The view model.</param> /// <param name="isDialog">Whethor or not the window is being shown as a dialog.</param> /// <param name="context">The view context.</param> /// <param name="settings">The optional popup settings.</param> /// <returns>The window.</returns> protected virtual async Task <Window> CreateWindowAsync(object rootModel, bool isDialog, object context, IDictionary <string, object> settings) { var view = EnsureWindow(rootModel, ViewLocator.LocateForModel(rootModel, null, context), isDialog); ViewModelBinder.Bind(rootModel, view, context); var haveDisplayName = rootModel as IHaveDisplayName; if (string.IsNullOrEmpty(view.Title) && haveDisplayName != null && !ConventionManager.HasBinding(view, Window.TitleProperty)) { var binding = new Binding("DisplayName") { Mode = BindingMode.TwoWay }; view.SetBinding(Window.TitleProperty, binding); } ApplySettings(view, settings); var conductor = new WindowConductor(rootModel, view); await conductor.InitialiseAsync(); return(view); }
/// <summary> /// Shows a modal dialog for the specified model. /// </summary> /// <param name = "rootModel">The root model.</param> /// <param name = "context">The context.</param> /// <param name = "settings">The optional dialog settings.</param> public virtual void ShowDialog(object rootModel, object context = null, IDictionary <string, object> settings = null) { var navigationSvc = IoC.Get <INavigationService>(); var host = new DialogHost(navigationSvc); var view = ViewLocator.LocateForModel(rootModel, host, context); host.Content = view; host.SetValue(View.IsGeneratedProperty, true); ViewModelBinder.Bind(rootModel, host, null); host.SetActionTarget(rootModel); ApplySettings(host, settings); var activatable = rootModel as IActivate; if (activatable != null) { activatable.Activate(); } var deactivator = rootModel as IDeactivate; if (deactivator != null) { host.Closed += delegate { deactivator.Deactivate(true); }; } host.Open(); }
static void ModelWithoutContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (View.InDesignMode || e.NewValue == null || e.NewValue == e.OldValue) { return; } var fe = d as FrameworkElement; if (fe == null) { return; } View.ExecuteOnLoad(fe, delegate { var target = e.NewValue; var containerKey = e.NewValue as string; if (containerKey != null) { LogManager.GetLog(typeof(Bind)).Info("Using IoC is deprecated and will be removed in v3.0"); target = IoC.GetInstance(null, containerKey); } d.SetValue(View.IsScopeRootProperty, true); var context = string.IsNullOrEmpty(fe.Name) ? fe.GetHashCode().ToString() : fe.Name; d.SetValue(NoContextProperty, true); ViewModelBinder.Bind(target, d, context); }); }
private Task PushAsync(Element view, object parameter, bool animated) { var page = view as Page; if (page == null && !(view is ContentView)) { throw new NotSupportedException(String.Format("{0} does not inherit from either {1} or {2}.", view.GetType(), typeof(Page), typeof(ContentView))); } var viewModel = ViewModelLocator.LocateForView(view); if (viewModel != null) { TryInjectParameters(viewModel, parameter); ViewModelBinder.Bind(viewModel, view, null); } var contentView = view as ContentView; if (contentView != null) { page = CreateContentPage(contentView, viewModel); } return(navigationPage.PushAsync(page, animated)); }
/// <summary> /// Shows a settings flyout panel for the specified model. /// </summary> /// <param name="viewModel">The settings view model.</param> /// <param name="commandLabel">The settings command label.</param> /// <param name="viewSettings">The optional dialog settings.</param> public async void ShowSettingsFlyout(object viewModel, string commandLabel, IDictionary <string, object> viewSettings = null) { var view = ViewLocator.LocateForModel(viewModel, null, null); ViewModelBinder.Bind(viewModel, view, null); viewSettings = viewSettings ?? new Dictionary <string, object>(); var width = viewSettings.ContainsKey("width") ? (SettingsFlyout.SettingsFlyoutWidth)viewSettings["width"] : SettingsFlyout.SettingsFlyoutWidth.Narrow; // extract the header color/logo from the appmanifest.xml var visualElements = await Callisto.Controls.Common.AppManifestHelper.GetManifestVisualElementsAsync(); // enable the overriding of these, but default to manifest var headerBackground = viewSettings.ContainsKey("headerbackground") ? (SolidColorBrush)viewSettings["headerbackground"] : new SolidColorBrush(visualElements.BackgroundColor); var smallLogoUri = viewSettings.ContainsKey("smalllogouri") ? (Uri)viewSettings["smalllogouri"] : visualElements.SmallLogoUri; var smallLogo = new BitmapImage(smallLogoUri); var settingsFlyout = new SettingsFlyout { FlyoutWidth = width, HeaderText = commandLabel, Content = view, IsOpen = true, HeaderBrush = headerBackground, SmallLogoImageSource = smallLogo }; var deactivator = viewModel as IDeactivate; if (deactivator != null) { EventHandler <object> closed = null; closed = (s, e) => { deactivator.Deactivate(true); settingsFlyout.Closed -= closed; }; settingsFlyout.Closed += closed; } 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? }
/// <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(); }
private static Tuple <T, UIElement> CreateViewModelAndView <T>(Action <T> onInitialize = null) where T : IScreen { var vm = IoC.Get <T>(); var view = ViewLocator.LocateForModel(vm, null, null); ViewModelBinder.Bind(vm, view, null); if (onInitialize != null) { onInitialize(vm); } ScreenExtensions.TryActivate(vm); return(new Tuple <T, UIElement>(vm, view)); }
/// <summary> /// Locates the view model, locates the associate view, binds them and shows it as the root view. /// </summary> /// <param name="application">The application.</param> /// <param name="viewModelType">The view model type.</param> protected static void DisplayRootViewFor(Application application, Type viewModelType) { var viewModel = IoC.GetInstance(viewModelType, null); var view = ViewLocator.LocateForModel(viewModel, null, null); ViewModelBinder.Bind(viewModel, view, null); var activator = viewModel as IActivate; if (activator != null) { activator.Activate(); } Mouse.Initialize(view); application.RootVisual = view; }
/// <summary> /// Locates the view model, locates the associate view, binds them and shows it as the root view. /// </summary> /// <param name="viewModelType">The view model type.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>A task that represents the asynchronous operation.</returns> protected async Task DisplayRootViewForAsync(Type viewModelType, CancellationToken cancellationToken) { Initialize(); var viewModel = IoC.GetInstance(viewModelType, null); var view = ViewLocator.LocateForModel(viewModel, null, null); ViewModelBinder.Bind(viewModel, view, null); if (viewModel is IActivate activator) { await activator.ActivateAsync(cancellationToken); } Window.Current.Content = view; Window.Current.Activate(); }
private static void UpdateViewModel(DependencyObject dependencyObject) { var model = GetModel(dependencyObject); var context = GetContext(dependencyObject); if (model == null) { SetContentProperty(dependencyObject, null); return; } var view = ViewLocator.LocateForModel(model, dependencyObject, context); SetContentProperty(dependencyObject, view); ViewModelBinder.Bind(model, view, context); }
/// <summary> /// Locates the view model, locates the associate view, binds them and shows it as the root view. /// </summary> /// <param name="viewModelType">The view model type.</param> protected async Task DisplayRootViewForAsync(Type viewModelType) { var viewModel = IoC.GetInstance(viewModelType, null); var view = ViewLocator.LocateForModel(viewModel, null, null); if (!(view is Page page)) { throw new NotSupportedException(String.Format("{0} does not inherit from {1}.", view.GetType(), typeof(Page))); } ViewModelBinder.Bind(viewModel, view, null); if (viewModel is IActivate activator) { await activator.ActivateAsync(); } MainPage = page; }
/// <summary> /// Locates the view model, locates the associate view, binds them and shows it as the root view. /// </summary> /// <param name="viewModelType">The view model type.</param> protected void DisplayRootViewFor(Type viewModelType) { Initialise(); var viewModel = IoC.GetInstance(viewModelType, null); var view = ViewLocator.LocateForModel(viewModel, null, null); ViewModelBinder.Bind(viewModel, view, null); var activator = viewModel as IActivate; if (activator != null) { activator.Activate(); } Window.Current.Content = view; Window.Current.Activate(); }
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); SetContentProperty(targetLocation, view); ViewModelBinder.Bind(model, view, e.NewValue); }
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); SetContentProperty(targetLocation, view); ViewModelBinder.Bind(args.NewValue, view, context); } else { SetContentProperty(targetLocation, args.NewValue); } }
/// <summary> /// Locates the view model, locates the associate view, binds them and shows it as the root view. /// </summary> /// <param name="viewModelType">The view model type.</param> protected void DisplayRootViewFor(Type viewModelType) { var rootElement = NoesisView.Content; var contentControl = (ContentControl)rootElement.FindName(RootContentControlName); object viewModel = IoC.GetInstance(viewModelType, null); UIElement view = ViewLocator.LocateForModel(viewModel, null, null); ViewModelBinder.Bind(viewModel, view, null); var activator = viewModel as IActivate; if (activator != null) { activator.Activate(); } contentControl.Content = view; }
/// <summary> /// Creates a window. /// </summary> /// <param name="rootModel">The view model.</param> /// <param name="isDialog">Whethor or not the window is being shown as a dialog.</param> /// <param name="context">The view context.</param> /// <returns>The window.</returns> protected virtual Window CreateWindow(object rootModel, bool isDialog, object context) { var view = EnsureWindow(rootModel, ViewLocator.LocateForModel(rootModel, null, context), isDialog); ViewModelBinder.Bind(rootModel, view, context); var haveDisplayName = rootModel as IHaveDisplayName; if (haveDisplayName != null && !ConventionManager.HasBinding(view, Window.TitleProperty)) { var binding = new Binding("DisplayName") { Mode = BindingMode.TwoWay }; view.SetBinding(Window.TitleProperty, binding); } new WindowConductor(rootModel, view); return(view); }
/// <summary> /// Shows a modal dialog for the specified model. /// </summary> /// <param name="rootModel">The root model.</param> /// <param name="context">The context.</param> public virtual void ShowDialog(object rootModel, object context = null) { var view = EnsureWindow(rootModel, ViewLocator.LocateForModel(rootModel, null, context)); ViewModelBinder.Bind(rootModel, view, context); var haveDisplayName = rootModel as IHaveDisplayName; if (haveDisplayName != null && !ConventionManager.HasBinding(view, ChildWindow.TitleProperty)) { var binding = new Binding("DisplayName") { Mode = BindingMode.TwoWay }; view.SetBinding(ChildWindow.TitleProperty, binding); } new WindowConductor(rootModel, view); view.Show(); }
/// <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(); }
private RadWindow PrepareRadWindow(object rootModel, object context, IDictionary <string, object> settings) { var view = EnsureWindow(rootModel, ViewLocator.LocateForModel(rootModel, null, context)); ViewModelBinder.Bind(rootModel, view, context); var haveDisplayName = rootModel as IHaveDisplayName; if (haveDisplayName != null && !ConventionManager.HasBinding(view, RadWindow.HeaderProperty)) { var binding = new Binding("DisplayName") { Mode = BindingMode.TwoWay }; view.SetBinding(RadWindow.HeaderProperty, binding); } new RadWindowConductor(rootModel, view); ApplySettings(view, settings); return(view); }