/// <summary> /// Evaluates the parameter. /// </summary> /// <param name = "expression">The expression.</param> /// <param name = "context">The context.</param> /// <param name = "resultType">Type of the result.</param> /// <returns>The evaluated parameter.</returns> private static object EvaluateParameter(string expression, ActionExecutionContext context, Type resultType) { try { var index = 0; var parameters = new ParameterExpression[MessageBinder.SpecialValues.Count]; var values = new object[MessageBinder.SpecialValues.Count]; foreach (var pair in MessageBinder.SpecialValues) { var name = "@" + index; expression = expression.Replace(pair.Key, name); var value = pair.Value(context); parameters[index] = Expression.Parameter(GetParameterType(value), name); values[index] = value; index++; } var exp = DynamicExpression.ParseLambda(parameters, resultType, expression); return(exp.Compile().DynamicInvoke(values)); } catch (Exception exc) { LogManager.GetLog(typeof(MessageBinder)).Error(exc); return(null); } }
/// <summary> /// Get the close action for the specified view model. /// </summary> /// <param name="viewModel">The view model to close.</param> /// <param name="views">The associated views.</param> /// <param name="dialogResult">The dialog result.</param> /// <returns> /// An <see cref="Action" /> to close the view model. /// </returns> /// <exception cref="System.NotImplementedException"></exception> public virtual Func <CancellationToken, Task> GetViewCloseAction(object viewModel, ICollection <object> views, bool?dialogResult) { foreach (var contextualView in views) { var viewType = contextualView.GetType(); #if WINDOWS_UWP var closeMethod = viewType.GetRuntimeMethod("Close", new Type[0]); #else var closeMethod = viewType.GetMethod("Close"); #endif if (closeMethod != null) { return ct => { #if !WINDOWS_UWP var isClosed = false; if (dialogResult != null) { var resultProperty = contextualView.GetType().GetProperty("DialogResult"); if (resultProperty != null) { resultProperty.SetValue(contextualView, dialogResult, null); isClosed = true; } } if (!isClosed) { closeMethod.Invoke(contextualView, null); } #else closeMethod.Invoke(contextualView, null); #endif return Task.FromResult(true); } } ; #if WINDOWS_UWP var isOpenProperty = viewType.GetRuntimeProperty("IsOpen"); #else var isOpenProperty = viewType.GetProperty("IsOpen"); #endif if (isOpenProperty != null) { return(ct => { isOpenProperty.SetValue(contextualView, false, null); return Task.FromResult(true); }); } } return(ct => { LogManager.GetLog(typeof(Screen)).Info("TryClose requires a parent IConductor or a view with a Close method or IsOpen property."); return Task.FromResult(true); }); } }
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; 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; ViewModelBinder.Bind(target, d, context); }); }
/// <summary> /// Get the close action for the specified view model. /// </summary> /// <param name="viewModel">The view model to close.</param> /// <param name="views">The associated views.</param> /// <param name="dialogResult">The dialog result.</param> /// <returns>An <see cref="Action"/> to close the view model.</returns> public virtual Func<CancellationToken, Task> GetViewCloseAction(object viewModel, ICollection<object> views, bool? dialogResult) { return ct => { LogManager.GetLog(typeof(Screen)).Info("TryClose requires a parent IConductor or a view with a Close method or IsOpen property."); return Task.FromResult(true); }; }
/// <summary> /// Get the close action for the specified view model. /// </summary> /// <param name="viewModel">The view model to close.</param> /// <param name="views">The associated views.</param> /// <param name="dialogResult">The dialog result.</param> /// <returns>An <see cref="Action"/> to close the view model.</returns> public virtual Action GetViewCloseAction(object viewModel, ICollection <object> views, bool?dialogResult) { var child = viewModel as IChild; if (child != null) { var conductor = child.Parent as IConductor; if (conductor != null) { return(() => conductor.CloseItem(viewModel)); } } return(() => LogManager.GetLog(typeof(Screen)).Info("TryClose requires a parent IConductor or a view with a Close method or IsOpen property.")); }
public MainViewModel(BarcodeLabel label, ApplicationPrinter printer, IEventAggregator eventAggregator) { this.Label = label; this.AppPrinter = printer; string printerName; if (BarcodePrinter.Properties.Settings.Default.DefaultPrinterName == null || BarcodePrinter.Properties.Settings.Default.DefaultPrinterName.Length == 0) { printerName = printer.DefaultZebraPrinter; } else { printerName = BarcodePrinter.Properties.Settings.Default.DefaultPrinterName; } this.SelectedPrinter = printerName; this.eventAggregator = eventAggregator; logger = LogManager.GetLog(GetType()); }
/// <summary> /// Get the close action for the specified view model. /// </summary> /// <param name="viewModel">The view model to close.</param> /// <param name="views">The associated views.</param> /// <param name="dialogResult">The dialog result.</param> /// <returns>An <see cref="Action"/> to close the view model.</returns> public virtual Func <CancellationToken, Task> GetViewCloseAction(object viewModel, ICollection <object> views, bool?dialogResult) { var child = viewModel as IChild; if (child != null) { var conductor = child.Parent as IConductor; if (conductor != null) { return(ct => conductor.CloseItemAsync(viewModel, ct)); } } return(ct => { LogManager.GetLog(typeof(Screen)).Info("TryClose requires a parent IConductor or a view with a Close method or IsOpen property."); return Task.FromResult(true); }); }
/// <summary> /// Get the close action for the specified view model. /// </summary> /// <param name="viewModel">The view model to close.</param> /// <param name="views">The associated views.</param> /// <param name="dialogResult">The dialog result.</param> /// <returns> /// An <see cref="Action" /> to close the view model. /// </returns> /// <exception cref="System.NotImplementedException"></exception> Action IPlatformProvider.GetViewCloseAction(object viewModel, ICollection <object> views, bool?dialogResult) { var child = viewModel as IChild; var conductor = child?.Parent as IConductor; if (conductor != null) { return(() => conductor.CloseItem(viewModel)); } foreach (var contextualView in views) { var viewType = contextualView.GetType(); var closeMethod = viewType.GetRuntimeMethod("Close", new Type[0]); if (closeMethod != null) { return () => { closeMethod.Invoke(contextualView, null); } } ; var isOpenProperty = viewType.GetRuntimeProperty("IsOpen"); if (isOpenProperty != null) { return(() => isOpenProperty.SetValue(contextualView, false, null)); } var parentPopup = (contextualView as FrameworkElement).FindAscendant <PopupOverlay>(); if (parentPopup != null) { return(() => parentPopup.Close()); } } return(() => LogManager.GetLog(typeof(Screen)).Info("TryClose requires a parent IConductor or a view with a Close method or IsOpen property.")); }
/// <summary> /// Allows action parameters to be specified using Xaml compact syntax and (optionally) parameters evaluation. /// </summary> /// <param name = "enableEvaluation">If set to <c>true</c> action parameters will be evaluated, if needed.</param> public static void AllowXamlSyntax(bool enableEvaluation = true) { if (enableEvaluation) { Parser.CreateParameter = (target, parameterText) => { //Check if the parameter is defined as a markup... if (parameterText.StartsWith("{") && parameterText.EndsWith("}")) { try { parameterText = string.Format(PARAMETER_XAML_FRAGMENT, XamlNamespaces, parameterText); var parsed = LoadXaml(parameterText); return((Parameter)parsed); } catch (Exception exc) { LogManager.GetLog(typeof(Parser)).Error(exc); } } //Pass the textual value and let it be evaluated afterwards... return(new Parameter { Value = parameterText }); }; MessageBinder.EvaluateParameter = (text, parameterType, context) => { var lookup = text.ToLower(CultureInfo.InvariantCulture); Func <ActionExecutionContext, object> resolver; return(MessageBinder.SpecialValues.TryGetValue(lookup, out resolver) ? resolver(context) : (typeof(string) == parameterType ? text : EvaluateParameter(text, context, parameterType))); }; } else { Parser.CreateParameter = (target, parameterText) => { //Check if the parameter is defined as a markup... if (parameterText.StartsWith("{") && parameterText.EndsWith("}")) { try { parameterText = string.Format(PARAMETER_XAML_FRAGMENT, XamlNamespaces, parameterText); var parsed = LoadXaml(parameterText); return((Parameter)parsed); } catch (Exception exc) { LogManager.GetLog(typeof(Parser)).Error(exc); } } //Use the default implementation if the parameter is not identified as a binding... return(m_BaseCreateParameter(target, parameterText)); }; MessageBinder.EvaluateParameter = m_BaseEvaluateParameter; } }
/// <summary> /// Get the close action for the specified view model. /// </summary> /// <param name="viewModel">The view model to close.</param> /// <param name="views">The associated views.</param> /// <param name="dialogResult">The dialog result.</param> /// <returns> /// An <see cref="Action" /> to close the view model. /// </returns> /// <exception cref="System.NotImplementedException"></exception> public System.Action GetViewCloseAction(object viewModel, ICollection <object> views, bool?dialogResult) { var child = viewModel as IChild; if (child != null) { var conductor = child.Parent as IConductor; if (conductor != null) { return(() => conductor.CloseItem(viewModel)); } } foreach (var contextualView in views) { var viewType = contextualView.GetType(); #if WinRT var closeMethod = viewType.GetRuntimeMethod("Close", new Type[0]); #else var closeMethod = viewType.GetMethod("Close"); #endif if (closeMethod != null) { return () => { #if !SILVERLIGHT && !WinRT var isClosed = false; if (dialogResult != null) { var resultProperty = contextualView.GetType().GetProperty("DialogResult"); if (resultProperty != null) { resultProperty.SetValue(contextualView, dialogResult, null); isClosed = true; } } if (!isClosed) { closeMethod.Invoke(contextualView, null); } #else closeMethod.Invoke(contextualView, null); #endif } } ; #if WinRT var isOpenProperty = viewType.GetRuntimeProperty("IsOpen"); #else var isOpenProperty = viewType.GetProperty("IsOpen"); #endif if (isOpenProperty != null) { return(() => isOpenProperty.SetValue(contextualView, false, null)); } } return(() => LogManager.GetLog(typeof(Screen)).Info("TryClose requires a parent IConductor or a view with a Close method or IsOpen property.")); } }
/// <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); // Do not use LocateForView because Frame control already has its // parent's DataContext. Use LocateForViewType instead. var viewModel = ViewModelLocator.LocateForViewType(e.Content.GetType()); //var viewModel = ViewModelLocator.LocateForView(e.Content); if (viewModel == null) { if (System.Diagnostics.Debugger.IsAttached) { var msg = "'" + e.Content.GetType().FullName + "' must be registered in the bootstrapper (or INavigationService might never get injected)"; var exc = new InvalidOperationException(msg); LogManager.GetLog(typeof(FrameAdapter)).Error(exc); throw exc; } return; } // Inject dependency properties IoC.BuildUp(viewModel); #if WINDOWS_PHONE var page = e.Content as PhoneApplicationPage; #else var page = e.Content as Page; #endif 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(); } #if WINDOWS_PHONE || WinRT var page = e.Content as PhoneApplicationPage; var viewAware = viewModel as IViewAware; if (viewAware != null) { View.ExecuteOnLayoutUpdated(page, (s, a) => viewAware.OnViewReady(page)); } #endif GC.Collect(); }