Used to configure the conventions used by the framework to apply bindings and create actions.
Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates the page.
        /// </summary>
        /// <param name="rootModel">The root model.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public virtual Page CreatePage(object rootModel, object context)
        {
            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);
            }

            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);
        }
Ejemplo n.º 3
0
        /// <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.InitialiseAysnc();

            return(view);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a binding on a <see cref="Parameter"/>.
        /// </summary>
        /// <param name="target">The target to which the message is applied.</param>
        /// <param name="parameter">The parameter object.</param>
        /// <param name="elementName">The name of the element to bind to.</param>
        /// <param name="path">The path of the element to bind to.</param>
        /// <param name="bindingMode">The binding mode to use.</param>
        public static void BindParameter(FrameworkElement target, Parameter parameter, string elementName, string path, BindingMode bindingMode)
        {
#if XFORMS
            var element = elementName == "$this" ? target : null;

            if (element == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(path))
            {
                path = ConventionManager.GetElementConvention(element.GetType()).ParameterProperty;
            }

            var binding = new Binding(path)
            {
                Source = element,
                Mode   = bindingMode
            };

            parameter.SetBinding(Parameter.ValueProperty, binding);
#else
            var element = elementName == "$this"
                ? target
                : BindingScope.GetNamedElements(target).FindName(elementName);
            if (element == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(path))
            {
                path = ConventionManager.GetElementConvention(element.GetType()).ParameterProperty;
            }
#if WINDOWS_UWP
            var binding = new Binding
            {
                Path   = new PropertyPath(path),
                Source = element,
                Mode   = bindingMode
            };
#else
            var binding = new Binding(path)
            {
                Source = element,
                Mode   = bindingMode
            };
#endif

#if !WINDOWS_UWP
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
#endif

            BindingOperations.SetBinding(parameter, Parameter.ValueProperty, binding);
#endif
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a binding on a <see cref="Parameter"/>.
        /// </summary>
        /// <param name="target">The target to which the message is applied.</param>
        /// <param name="parameter">The parameter object.</param>
        /// <param name="elementName">The name of the element to bind to.</param>
        /// <param name="path">The path of the element to bind to.</param>
        /// <param name="bindingMode">The binding mode to use.</param>
        public static void BindParameter(FrameworkElement target, Parameter parameter, string elementName, string path, BindingMode bindingMode)
        {
            var element = elementName == "$this"
                ? target
                : BindingScope.GetNamedElements(target).FindName(elementName);

            if (element == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(path))
            {
                path = ConventionManager.GetElementConvention(element.GetType()).ParameterProperty;
            }
#if WinRT
            var binding = new Binding
            {
                Path   = new PropertyPath(path),
                Source = element,
                Mode   = bindingMode
            };
#else
            var binding = new Binding(path)
            {
                Source = element,
                Mode   = bindingMode
            };
#endif

#if (SILVERLIGHT && !SL5)
            var expression = (BindingExpression)BindingOperations.SetBinding(parameter, Parameter.ValueProperty, binding);

            var field = element.GetType().GetField(path + "Property", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
            if (field == null)
            {
                return;
            }

            ConventionManager.ApplySilverlightTriggers(element, (DependencyProperty)field.GetValue(null), x => expression, null, null);
#else
#if !WinRT
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
#endif
            BindingOperations.SetBinding(parameter, Parameter.ValueProperty, binding);
#endif
        }
Ejemplo n.º 6
0
        ///<summary>
        ///  Checks if the <see cref="ActionMessage" /> -Target was set.
        ///</summary>
        ///<param name="element"> DependencyObject to check </param>
        ///<returns> True if Target or TargetWithoutContext was set on <paramref name="element" /> </returns>
        public static bool HasTargetSet(DependencyObject element)
        {
            if (GetTarget(element) != null || GetTargetWithoutContext(element) != null)
            {
                return(true);
            }

            var frameworkElement = element as FrameworkElement;

            if (frameworkElement == null)
            {
                return(false);
            }

            return(ConventionManager.HasBinding(frameworkElement, TargetProperty) ||
                   ConventionManager.HasBinding(frameworkElement, TargetWithoutContextProperty));
        }
Ejemplo n.º 7
0
        public static void Install()
        {
            ConventionManager.AddElementConvention <RadBusyIndicator>(RadBusyIndicator.IsRunningProperty,
                                                                      "IsRunning",
                                                                      "Loaded");
            ConventionManager.AddElementConvention <RadDataBoundListBox>(DataControlBase.ItemsSourceProperty,
                                                                         "SelectedItem",
                                                                         "SelectionChanged")
            .ApplyBinding = (viewModelType, path, property, element, convention) =>
            {
                if (!ConventionManager.SetBindingWithoutBindingOrValueOverwrite(viewModelType,
                                                                                path,
                                                                                property, element, convention,
                                                                                DataControlBase.ItemsSourceProperty))
                {
                    return(false);
                }

                if (ConventionManager.HasBinding(element, RadDataBoundListBox.SelectedItemProperty))
                {
                    return(true);
                }

                var index = path.LastIndexOf('.');
                index = index == -1 ? 0 : index + 1;

                var baseName = path.Substring(index);
                foreach (var selectionPath in
                         from potentialName in ConventionManager.DerivePotentialSelectionNames(baseName)
                         where
                         viewModelType.GetProperty(potentialName,
                                                   BindingFlags.IgnoreCase | BindingFlags.Public |
                                                   BindingFlags.Instance) != null
                         select path.Replace(baseName, potentialName))
                {
                    var binding = new Binding(selectionPath)
                    {
                        Mode = BindingMode.TwoWay
                    };
                    BindingOperations.SetBinding(element, RadDataBoundListBox.SelectedItemProperty, binding);
                }

                return(true);
            };
        }
        static TriggerBase CreateTrigger(Type targetType, string[] triggerPlusMessage)
        {
            if (triggerPlusMessage.Length == 1)
            {
                var defaults = ConventionManager.GetElementConvention(targetType);
                return(defaults.CreateTrigger());
            }

            var triggerDetail = triggerPlusMessage[0]
                                .Replace("[", string.Empty)
                                .Replace("]", string.Empty)
                                .Replace("Event", string.Empty)
                                .Trim();

            return(new EventTrigger {
                EventName = triggerDetail
            });
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Allow Xamarin to navigate to a ViewModel backed by a view which is of type <see cref="T:Xamarin.Forms.ContentView"/> by adapting the result
        /// to a <see cref="T:Xamarin.Forms.ContentPage"/>.
        /// </summary>
        /// <param name="view">The view to be adapted</param>
        /// <param name="viewModel">The view model which is bound to the view</param>
        /// <returns>The adapted ContentPage</returns>
        protected virtual ContentPage CreateContentPage(ContentView view, object viewModel)
        {
            var page = new ContentPage {
                Content = view
            };

            var hasDiplayName = viewModel as IHaveDisplayName;

            if (hasDiplayName != null)
            {
                var path     = "DisplayName";
                var property = typeof(IHaveDisplayName).GetRuntimeProperty(path);
                ConventionManager.SetBinding(viewModel.GetType(), path, property, page, null, Page.TitleProperty);

                page.BindingContext = viewModel;
            }

            return(page);
        }
Ejemplo n.º 10
0
        /// <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);
        }
Ejemplo n.º 11
0
        /// <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();
        }
Ejemplo n.º 12
0
        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);
        }
Ejemplo n.º 13
0
 public static void Install()
 {
     ConventionManager.AddElementConvention <MenuItem>(ItemsControl.ItemsSourceProperty, "DataContext", "Click");
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Shows a modal dialog for the specified model.
        /// </summary>
        /// <param name="rootModel">The root model.</param>
        /// <param name="context">The context.</param>
        public 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);
            }

            var activatable = rootModel as IActivate;

            if (activatable != null)
            {
                activatable.Activate();
            }

            var deactivatable = rootModel as IDeactivate;

            if (deactivatable != null)
            {
                bool deactivatingFromView = false;
                bool deactivateFromVM     = false;

                view.Closed += (s, e) => {
                    if (deactivateFromVM)
                    {
                        return;
                    }

                    deactivatingFromView = true;
                    deactivatable.Deactivate(true);
                    deactivatingFromView = false;
                };

                deactivatable.Deactivated += (s, e) => {
                    if (e.WasClosed && !deactivatingFromView)
                    {
                        deactivateFromVM = true;
                        actuallyClosing  = true;
                        view.Close();
                        actuallyClosing  = false;
                        deactivateFromVM = false;
                    }
                };
            }

            var guard = rootModel as IGuardClose;

            if (guard != null)
            {
                view.Closing += (s, e) => OnShutdownAttempted(guard, view, e);
            }

            view.Show();
        }