Пример #1
0
        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);
                // Trialing binding before setting content in Xamarin Forms
#if XFORMS
                ViewModelBinder.Bind(args.NewValue, view, context);
#endif
                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);
                }
#if !XFORMS
                ViewModelBinder.Bind(args.NewValue, view, context);
#endif
            }
            else
            {
                SetContentProperty(targetLocation, args.NewValue);
            }
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
            });
        }
Пример #4
0
        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
        }
Пример #5
0
        /// <summary>
        /// Parses the specified message text.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="text">The message text.</param>
        /// <returns>The triggers parsed from the text.</returns>
        public static IEnumerable <TriggerBase> Parse(DependencyObject target, string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(new TriggerBase[0]);
            }

            var triggers     = new List <TriggerBase>();
            var messageTexts = StringSplitter.Split(text, ';');

            foreach (var messageText in messageTexts)
            {
                var triggerPlusMessage = LongFormatRegularExpression.IsMatch(messageText)
                                             ? StringSplitter.Split(messageText, '=')
                                             : new[] { null, messageText };

                var messageDetail = triggerPlusMessage.Last()
                                    .Replace("[", string.Empty)
                                    .Replace("]", string.Empty)
                                    .Trim();

                var trigger = CreateTrigger(target, triggerPlusMessage.Length == 1 ? null : triggerPlusMessage[0]);
                var message = CreateMessage(target, messageDetail);

#if WINDOWS_UWP || XFORMS
                AddActionToTrigger(target, message, trigger);
#else
                trigger.Actions.Add(message);
#endif

                triggers.Add(trigger);
            }

            return(triggers);
        }
Пример #6
0
        static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var parameter = (Parameter)d;
            var owner     = parameter.Owner;

            if (owner != null)
            {
                owner.UpdateAvailability();
            }
        }
Пример #7
0
        void IAttachedObject.Attach(DependencyObject dependencyObject) {
#else
        void IAttachedObject.Attach(FrameworkElement dependencyObject)
        {
#endif
            associatedObject = dependencyObject;
        }

        void IAttachedObject.Detach()
        {
            associatedObject = null;
        }
Пример #8
0
        static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == e.OldValue)
            {
                return;
            }

            var messageTriggers = (TriggerBase[])d.GetValue(MessageTriggersProperty);

#if WinRT81
            var allTriggers = Interaction.GetBehaviors(d);

            if (messageTriggers != null)
            {
                messageTriggers.OfType <DependencyObject>().Apply(x => allTriggers.Remove(x));
            }

            var newTriggers = Parser.Parse(d, e.NewValue as string).ToArray();
            newTriggers.OfType <DependencyObject>().Apply(allTriggers.Add);
#elif XFORMS
            var visualElement = d as VisualElement;

            var allTriggers = visualElement != null ? visualElement.Triggers : new List <TriggerBase>();

            if (messageTriggers != null)
            {
                messageTriggers.Apply(x => allTriggers.Remove(x));
            }

            var newTriggers = Parser.Parse(d, e.NewValue as string).ToArray();
            newTriggers.Apply(allTriggers.Add);
#else
            var allTriggers = Interaction.GetTriggers(d);

            if (messageTriggers != null)
            {
                messageTriggers.Apply(x => allTriggers.Remove(x));
            }

            var newTriggers = Parser.Parse(d, e.NewValue as string).ToArray();
            newTriggers.Apply(allTriggers.Add);
#endif

            if (newTriggers.Length > 0)
            {
                d.SetValue(MessageTriggersProperty, newTriggers);
            }
            else
            {
                d.ClearValue(MessageTriggersProperty);
            }
        }
Пример #9
0
        void IAttachedObject.Attach(DependencyObject dependencyObject)
        {
#else
        void IAttachedObject.Attach(FrameworkElement dependencyObject)
        {
#endif
            associatedObject = dependencyObject;
        }

        void IAttachedObject.Detach()
        {
            associatedObject = null;
        }
Пример #10
0
        static void AtDesignTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!View.InDesignMode)
            {
                return;
            }

            var atDesignTime = (bool)e.NewValue;

            if (!atDesignTime)
            {
                return;
            }
#if XFORMS
            d.SetBinding(DataContextProperty, String.Empty);
#else
            BindingOperations.SetBinding(d, DataContextProperty, new Binding());
#endif
        }
Пример #11
0
        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);

#if XFORMS
                var context = fe.Id.ToString("N");
#else
                var context = string.IsNullOrEmpty(fe.Name)
                                  ? fe.GetHashCode().ToString()
                                  : fe.Name;
#endif

                d.SetValue(NoContextProperty, true);
                ViewModelBinder.Bind(target, d, context);
            });
        }
Пример #12
0
        private static void AddActionToTrigger(DependencyObject target, TriggerAction message, TriggerBase trigger)
        {
            if (trigger is EventTrigger)
            {
                var eventTrigger = (EventTrigger)trigger;

                eventTrigger.Actions.Add(message);
            }

            trigger.EnterActions.Add(message);

            // TriggerAction doesn't have an associated object property so we have
            // to create it ourselves, could be potential issues here with leaking the associated
            // object and not correctly detaching, this may depend if the trigger implements it's
            // AssociatedObject as a DependencyProperty.

            var actionMessage = message as ActionMessage;
            var targetElement = target as FrameworkElement;

            if (actionMessage != null && targetElement != null)
            {
                actionMessage.AssociatedObject = targetElement;
            }
        }
Пример #13
0
        /// <summary>
        /// Creates an instance of <see cref="ActionMessage"/> by parsing out the textual dsl.
        /// </summary>
        /// <param name="target">The target of the message.</param>
        /// <param name="messageText">The textual message dsl.</param>
        /// <returns>The created message.</returns>
        public static TriggerAction CreateMessage(DependencyObject target, string messageText)
        {
            var openingParenthesisIndex = messageText.IndexOf('(');

            if (openingParenthesisIndex < 0)
            {
                openingParenthesisIndex = messageText.Length;
            }

            var closingParenthesisIndex = messageText.LastIndexOf(')');

            if (closingParenthesisIndex < 0)
            {
                closingParenthesisIndex = messageText.Length;
            }

            var core           = messageText.Substring(0, openingParenthesisIndex).Trim();
            var message        = InterpretMessageText(target, core);
            var withParameters = message as IHaveParameters;

            if (withParameters != null)
            {
                if (closingParenthesisIndex - openingParenthesisIndex > 1)
                {
                    var paramString = messageText.Substring(openingParenthesisIndex + 1, closingParenthesisIndex - openingParenthesisIndex - 1);
                    var parameters  = StringSplitter.SplitParameters(paramString);

                    foreach (var parameter in parameters)
                    {
                        withParameters.Parameters.Add(CreateParameter(target, parameter.Trim()));
                    }
                }
            }

            return(message);
        }
Пример #14
0
 /// <summary>
 ///   Gets the message handler for this element.
 /// </summary>
 /// <param name="d"> The element. </param>
 /// <returns> The message handler. </returns>
 public static object GetHandler(DependencyObject d) {
     return d.GetValue(HandlerProperty);
 }
Пример #15
0
 /// <summary>
 ///   Gets the message handler for this element.
 /// </summary>
 /// <param name="d"> The element. </param>
 /// <returns> The message handler. </returns>
 public static object GetHandler(DependencyObject d) {
     return d.GetValue(HandlerProperty);
 }
Пример #16
0
 /// <summary>
 ///   Sets the attached triggers and messages.
 /// </summary>
 /// <param name="d"> The element to attach to. </param>
 /// <param name="attachText"> The parsable attachment text. </param>
 public static void SetAttach(DependencyObject d, string attachText) {
     d.SetValue(AttachProperty, attachText);
 }
Пример #17
0
 /// <summary>
 ///   Gets the attached triggers and messages.
 /// </summary>
 /// <param name="d"> The element that was attached to. </param>
 /// <returns> The parsable attachment text. </returns>
 public static string GetAttach(DependencyObject d) {
     return d.GetValue(AttachProperty) as string;
 }
Пример #18
0
        static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var parameter = (Parameter)d;
            var owner = parameter.Owner;

            if (owner != null)
            {
                owner.UpdateAvailability();
            }
        }
Пример #19
0
 /// <summary>
 ///   Places a message handler on this element.
 /// </summary>
 /// <param name="d"> The element. </param>
 /// <param name="value"> The message handler. </param>
 public static void SetHandler(DependencyObject d, object value) {
     d.SetValue(HandlerProperty, value);
 }
Пример #20
0
 /// <summary>
 ///   Gets the model to bind to.
 /// </summary>
 /// <param name = "dependencyObject">The dependency object to bind to.</param>
 /// <returns>The model.</returns>
 public static object GetModel(DependencyObject dependencyObject)
 {
     return(dependencyObject.GetValue(ModelProperty));
 }
Пример #21
0
 /// <summary>
 ///   Gets the model to bind to.
 /// </summary>
 /// <param name = "dependencyObject">The dependency object to bind to.</param>
 /// <returns>The model.</returns>
 public static object GetModelWithoutContext(DependencyObject dependencyObject)
 {
     return(dependencyObject.GetValue(ModelWithoutContextProperty));
 }
Пример #22
0
 /// <summary>
 ///   Sets the attached triggers and messages.
 /// </summary>
 /// <param name="d"> The element to attach to. </param>
 /// <param name="attachText"> The parsable attachment text. </param>
 public static void SetAttach(DependencyObject d, string attachText) {
     d.SetValue(AttachProperty, attachText);
 }
Пример #23
0
 public static bool GetAtDesignTime(DependencyObject dependencyObject)
 {
     return((bool)dependencyObject.GetValue(AtDesignTimeProperty));
 }
Пример #24
0
        static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            if (e.NewValue == e.OldValue) {
                return;
            }

            var messageTriggers = (TriggerBase[])d.GetValue(MessageTriggersProperty);

#if WinRT81
            var allTriggers = Interaction.GetBehaviors(d);

            if (messageTriggers != null)
            {
                messageTriggers.OfType<DependencyObject>().Apply(x => allTriggers.Remove(x));
            }

            var newTriggers = Parser.Parse(d, e.NewValue as string).ToArray();
            newTriggers.OfType<DependencyObject>().Apply(allTriggers.Add);
#elif XFORMS
            var visualElement = d as VisualElement;

            var allTriggers = visualElement != null ? visualElement.Triggers : new List<TriggerBase>();

            if (messageTriggers != null) {
                messageTriggers.Apply(x => allTriggers.Remove(x));
            }

            var newTriggers = Parser.Parse(d, e.NewValue as string).ToArray();
            newTriggers.Apply(allTriggers.Add);

#else
            var allTriggers = Interaction.GetTriggers(d);

             if (messageTriggers != null) {
                messageTriggers.Apply(x => allTriggers.Remove(x));
            }

            var newTriggers = Parser.Parse(d, e.NewValue as string).ToArray();
            newTriggers.Apply(allTriggers.Add);
#endif

            if (newTriggers.Length > 0) {
                d.SetValue(MessageTriggersProperty, newTriggers);
            }
            else {
                d.ClearValue(MessageTriggersProperty);
            }
        }
Пример #25
0
        private static void AddActionToTrigger(DependencyObject target, TriggerAction message, TriggerBase trigger)
        {
            // This is stupid, but there a number of limitiations in the 8.1 Behaviours SDK

            // The first is that there is no base class for a Trigger, just IBehaviour. Which
            // means there's no strongly typed way to add an action to a trigger. Every trigger
            // in the SDK implements the same pattern but no interface, we're going to have to
            // use reflection to set it.

            // More stupidity, ActionCollection doesn't care about IAction, but DependencyObject
            // and there's no actual implementation of

            var messageDependencyObject = message as DependencyObject;

            if (messageDependencyObject == null)
            {
                Log.Warn("{0} doesn't inherit DependencyObject and can't be added to ActionCollection", trigger.GetType().FullName);
                return;
            }

            // 95% of the time the trigger will be an EventTrigger, let's optimise for that case

            if (trigger is EventTrigger)
            {
                var eventTrigger = (EventTrigger)trigger;

                eventTrigger.Actions.Add(messageDependencyObject);
            }
            else
            {
                var actionsProperty = trigger.GetType().GetRuntimeProperty("Actions");

                if (actionsProperty == null)
                {
                    Log.Warn("Could not find Actions collection on trigger {0}.", trigger.GetType().FullName);
                    return;
                }

                var actionCollection = actionsProperty.GetValue(trigger) as ActionCollection;

                if (actionCollection == null)
                {
                    Log.Warn("{0}.Actions is either not an ActionCollection or is null.", trigger.GetType().FullName);
                    return;
                }

                actionCollection.Add(messageDependencyObject);
            }

            // The second is the IAction doesn't have an associated object property so we have
            // to create it ourselves, could be potential issues here with leaking the associated
            // object and not correctly detaching, this may depend if the trigger implements it's
            // AssociatedObject as a DependencyProperty.

            // Turns out trying to a binding won't work because the trigger doesn't notify the
            // binding of changes, so we just need to set it, yay.

            var actionMessage = message as ActionMessage;
            var targetElement = target as FrameworkElement;

            if (actionMessage != null && targetElement != null)
            {
                //var binding = new Binding { Source = trigger, Path = new PropertyPath("AssociatedObject") };

                //BindingOperations.SetBinding(actionMessage, ActionMessage.AssociatedObjectProperty, binding);

                actionMessage.AssociatedObject = targetElement;
            }
        }
Пример #26
0
 /// <summary>
 ///   Gets the attached triggers and messages.
 /// </summary>
 /// <param name="d"> The element that was attached to. </param>
 /// <returns> The parsable attachment text. </returns>
 public static string GetAttach(DependencyObject d) {
     return d.GetValue(AttachProperty) as string;
 }
Пример #27
0
 /// <summary>
 /// Sets whether or not do bind conventions at design-time.
 /// </summary>
 /// <param name="dependencyObject">The ui to apply conventions to.</param>
 /// <param name="value">Whether or not to apply conventions.</param>
 public static void SetAtDesignTime(DependencyObject dependencyObject, bool value)
 {
     dependencyObject.SetValue(AtDesignTimeProperty, value);
 }
Пример #28
0
 /// <summary>
 /// Gets the convention application behavior.
 /// </summary>
 /// <param name="d">The element the property is attached to.</param>
 /// <returns>Whether or not to apply conventions.</returns>
 public static bool? GetApplyConventions(DependencyObject d) {
     return (bool?)d.GetValue(ApplyConventionsProperty);
 }
Пример #29
0
 /// <summary>
 ///   Places a message handler on this element.
 /// </summary>
 /// <param name="d"> The element. </param>
 /// <param name="value"> The message handler. </param>
 public static void SetHandler(DependencyObject d, object value) {
     d.SetValue(HandlerProperty, value);
 }
Пример #30
0
 /// <summary>
 /// Sets the model.
 /// </summary>
 /// <param name="d">The element to attach the model to.</param>
 /// <param name="value">The model.</param>
 public static void SetModel(DependencyObject d, object value) {
     d.SetValue(ModelProperty, value);
 }
Пример #31
0
 /// <summary>
 ///   Sets the model to bind to.
 /// </summary>
 /// <param name = "dependencyObject">The dependency object to bind to.</param>
 /// <param name = "value">The model.</param>
 public static void SetModelWithoutContext(DependencyObject dependencyObject, object value)
 {
     dependencyObject.SetValue(ModelWithoutContextProperty, value);
 }
Пример #32
0
 /// <summary>
 /// Gets the context.
 /// </summary>
 /// <param name="d">The element the context is attached to.</param>
 /// <returns>The context.</returns>
 public static object GetContext(DependencyObject d) {
     return d.GetValue(ContextProperty);
 }
Пример #33
0
 /// <summary>
 ///   Sets the model to bind to.
 /// </summary>
 /// <param name = "dependencyObject">The dependency object to bind to.</param>
 /// <param name = "value">The model.</param>
 public static void SetModel(DependencyObject dependencyObject, object value)
 {
     dependencyObject.SetValue(ModelProperty, value);
 }
Пример #34
0
        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);
                // Trialing binding before setting content in Xamarin Forms
#if XFORMS
                ViewModelBinder.Bind(args.NewValue, view, context);
#endif
                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);
                }
#if !XFORMS
                ViewModelBinder.Bind(args.NewValue, view, context);
#endif
            }
            else {
                SetContentProperty(targetLocation, args.NewValue);
            }
        }
Пример #35
0
 /// <summary>
 /// Sets the convention application behavior.
 /// </summary>
 /// <param name="d">The element to attach the property to.</param>
 /// <param name="value">Whether or not to apply conventions.</param>
 public static void SetApplyConventions(DependencyObject d, bool? value) {
     d.SetValue(ApplyConventionsProperty, value);
 }
Пример #36
0
 /// <summary>
 /// Gets the convention application behavior.
 /// </summary>
 /// <param name="d">The element the property is attached to.</param>
 /// <returns>Whether or not to apply conventions.</returns>
 public static bool?GetApplyConventions(DependencyObject d)
 {
     return((bool?)d.GetValue(ApplyConventionsProperty));
 }
Пример #37
0
 /// <summary>
 /// Gets the model.
 /// </summary>
 /// <param name="d">The element the model is attached to.</param>
 /// <returns>The model.</returns>
 public static object GetModel(DependencyObject d) {
     return d.GetValue(ModelProperty);
 }
Пример #38
0
 /// <summary>
 /// Sets the convention application behavior.
 /// </summary>
 /// <param name="d">The element to attach the property to.</param>
 /// <param name="value">Whether or not to apply conventions.</param>
 public static void SetApplyConventions(DependencyObject d, bool?value)
 {
     d.SetValue(ApplyConventionsProperty, value);
 }
Пример #39
0
 /// <summary>
 /// Sets the context.
 /// </summary>
 /// <param name="d">The element to attach the context to.</param>
 /// <param name="value">The context.</param>
 public static void SetContext(DependencyObject d, object value) {
     d.SetValue(ContextProperty, value);
 }
Пример #40
0
 /// <summary>
 /// Gets the context.
 /// </summary>
 /// <param name="d">The element the context is attached to.</param>
 /// <returns>The context.</returns>
 public static object GetContext(DependencyObject d)
 {
     return(d.GetValue(ContextProperty));
 }
Пример #41
0
        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);
        }
Пример #42
0
 /// <summary>
 /// Sets the context.
 /// </summary>
 /// <param name="d">The element to attach the context to.</param>
 /// <param name="value">The context.</param>
 public static void SetContext(DependencyObject d, object value)
 {
     d.SetValue(ContextProperty, value);
 }