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); } }
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); }
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 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 }
/// <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); }
static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var parameter = (Parameter)d; var owner = parameter.Owner; if (owner != null) { owner.UpdateAvailability(); } }
void IAttachedObject.Attach(DependencyObject dependencyObject) { #else void IAttachedObject.Attach(FrameworkElement dependencyObject) { #endif associatedObject = dependencyObject; } void IAttachedObject.Detach() { associatedObject = null; }
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); } }
void IAttachedObject.Attach(DependencyObject dependencyObject) { #else void IAttachedObject.Attach(FrameworkElement dependencyObject) { #endif associatedObject = dependencyObject; } void IAttachedObject.Detach() { associatedObject = null; }
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 }
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); }); }
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; } }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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; }
static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var parameter = (Parameter)d; var owner = parameter.Owner; if (owner != null) { owner.UpdateAvailability(); } }
/// <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); }
/// <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)); }
/// <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)); }
/// <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); }
public static bool GetAtDesignTime(DependencyObject dependencyObject) { return((bool)dependencyObject.GetValue(AtDesignTimeProperty)); }
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); } }
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; } }
/// <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; }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
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); } }
/// <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); }
/// <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)); }
/// <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); }
/// <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); }
/// <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); }
/// <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)); }
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> /// 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); }