/// <summary> /// Gets the <see cref="BehaviorCollection"/> associated with a specified object. /// </summary> /// <param name="obj">The object from which to retrieve the <see cref="BehaviorCollection"/>.</param> /// <returns>A <see cref="BehaviorCollection"/> containing the behaviors associated with the specified object.</returns> public static BehaviorCollection GetBehaviors(DependencyObject obj) { var behaviorCollection = (BehaviorCollection)obj.GetValue(BehaviorsProperty); if (behaviorCollection == null) { #if WINDOWS_PHONE behaviorCollection = (BehaviorCollection)typeof(BehaviorCollection) .GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null) .Invoke(null); #else behaviorCollection = new BehaviorCollection(); #endif obj.SetValue(BehaviorsProperty, behaviorCollection); obj.SetValue(Interaction.BehaviorsProperty, behaviorCollection); var frameworkElement = obj as FrameworkElement; if (frameworkElement != null) { frameworkElement.Loaded -= FrameworkElement_Loaded; frameworkElement.Loaded += FrameworkElement_Loaded; frameworkElement.Unloaded -= FrameworkElement_Unloaded; frameworkElement.Unloaded += FrameworkElement_Unloaded; } } return behaviorCollection; }
/// <summary> /// Gets the <see cref="BehaviorCollection"/> associated with a specified object. /// </summary> /// <param name="obj">The <see cref="Windows.UI.Xaml.DependencyObject"/> from which to retrieve the <see cref="BehaviorCollection"/>.</param> /// <returns>A <see cref="BehaviorCollection"/> containing the behaviors associated with the specified object.</returns> public static BehaviorCollection GetBehaviors(DependencyObject obj) { if (obj == null) { throw new ArgumentNullException("obj"); } BehaviorCollection behaviorCollection = (BehaviorCollection)obj.GetValue(Interaction.BehaviorsProperty); if (behaviorCollection == null) { behaviorCollection = new BehaviorCollection(); obj.SetValue(Interaction.BehaviorsProperty, behaviorCollection); var frameworkElement = obj as FrameworkElement; if (frameworkElement != null) { frameworkElement.Loaded -= FrameworkElement_Loaded; frameworkElement.Loaded += FrameworkElement_Loaded; frameworkElement.Unloaded -= FrameworkElement_Unloaded; frameworkElement.Unloaded += FrameworkElement_Unloaded; } } return behaviorCollection; }
public void VectorChanged_BehaviorChangedToNonBehavior_ExceptionThrown() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); TestUtilities.AssertThrowsException(() => behaviorCollection[0] = new ToggleSwitch()); }
private void ConfigureOrCreateSearchContract( BehaviorCollection behaviors ) { Contract.Requires( behaviors != null ); if ( !searchOptions.IsValueCreated ) return; var options = searchOptions.Value; var searchContract = behaviors.OfType<SearchContractBehavior>().FirstOrDefault(); if ( searchContract == null ) { searchContract = new SearchContractBehavior(); behaviors.Add( searchContract ); } searchContract.SearchHistoryEnabled = options.SearchHistoryEnabled; searchContract.ShowOnKeyboardInput = options.ShowOnKeyboardInput; if ( !string.IsNullOrEmpty( options.PlaceholderText ) ) searchContract.PlaceholderText = options.PlaceholderText; if ( !string.IsNullOrEmpty( options.SearchHistoryContext ) ) searchContract.SearchHistoryContext = options.SearchHistoryContext; }
private void ConfigureOrCreateSettingsContract( BehaviorCollection behaviors ) { Contract.Requires( behaviors != null ); var settingsContract = behaviors.OfType<SettingsContractBehavior>().FirstOrDefault(); if ( settingsContract == null ) { settingsContract = new SettingsContractBehavior(); behaviors.Add( settingsContract ); } foreach ( var appSetting in appSettings ) { var setting = new ApplicationSetting() { Name = appSetting.Item2, ViewTypeName = appSetting.Item3 }; if ( !string.IsNullOrEmpty( appSetting.Item1 ) ) setting.Id = appSetting.Item1; settingsContract.ApplicationSettings.Add( setting ); } }
/// <summary> /// Gets the <see cref="BehaviorCollection"/> associated with a specified object. /// </summary> /// <param name="obj">The <see cref="Windows.UI.Xaml.DependencyObject"/> from which to retrieve the <see cref="BehaviorCollection"/>.</param> /// <returns>A <see cref="BehaviorCollection"/> containing the behaviors associated with the specified object.</returns> public static BehaviorCollection GetBehaviors(DependencyObject obj) { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } BehaviorCollection behaviorCollection = (BehaviorCollection)obj.GetValue(Interaction.BehaviorsProperty); if (behaviorCollection == null) { behaviorCollection = new BehaviorCollection(); obj.SetValue(Interaction.BehaviorsProperty, behaviorCollection); var frameworkElement = obj as FrameworkElement; if (frameworkElement != null) { frameworkElement.Loaded -= FrameworkElement_Loaded; frameworkElement.Loaded += FrameworkElement_Loaded; frameworkElement.Unloaded -= FrameworkElement_Unloaded; frameworkElement.Unloaded += FrameworkElement_Unloaded; } } return(behaviorCollection); }
public void VectorChanged_NonBehaviorAdded_ExceptionThrown() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); TestUtilities.AssertThrowsException(() => behaviorCollection.Add(new TextBlock())); }
/// <summary> /// Sets the <see cref="BehaviorCollection"/> associated with a specified object. /// </summary> /// <param name="obj">The <see cref="Windows.UI.Xaml.DependencyObject"/> on which to set the <see cref="BehaviorCollection"/>.</param> /// <param name="value">The <see cref="BehaviorCollection"/> associated with the object.</param> public static void SetBehaviors(DependencyObject obj, BehaviorCollection value) { if (obj == null) { throw new ArgumentNullException("obj"); } obj.SetValue(Interaction.BehaviorsProperty, value); }
/// <summary> /// Sets the <see cref="BehaviorCollection"/> associated with a specified object. /// </summary> /// <param name="obj">The <see cref="Windows.UI.Xaml.DependencyObject"/> on which to set the <see cref="BehaviorCollection"/>.</param> /// <param name="value">The <see cref="BehaviorCollection"/> associated with the object.</param> public static void SetBehaviors(DependencyObject obj, BehaviorCollection value) { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } obj.SetValue(Interaction.BehaviorsProperty, value); }
public void VectorChanged_AddWhileNotAttached_AttachNotCalled() { BehaviorCollection behaviorCollection = new BehaviorCollection(); StubBehavior stub = new StubBehavior(); behaviorCollection.Add(stub); TestUtilities.AssertNotAttached(stub); }
public void VectorChanged_DuplicateAdd_ExceptionThrown() { BehaviorCollection behaviorCollection = new BehaviorCollection(); StubBehavior stub = new StubBehavior(); behaviorCollection.Add(stub); TestUtilities.AssertThrowsException(() => behaviorCollection.Add(stub)); }
public void SetBehaviors_MultipleSets_DoesNotReattach() { BehaviorCollection behaviorCollection = new BehaviorCollection() { new StubBehavior() }; Button button = new Button(); Interaction.SetBehaviors(button, behaviorCollection); Interaction.SetBehaviors(button, behaviorCollection); foreach (StubBehavior behavior in behaviorCollection) { Assert.AreEqual(1, behavior.AttachCount, "Should only have called Attach once."); } }
public void VectorChanged_AddWhileAttached_AllAttached() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Attach(new Button()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); foreach (StubBehavior stub in behaviorCollection) { TestUtilities.AssertAttached(stub, behaviorCollection.AssociatedObject); } }
public void SetBehaviors_CollectionThenNull_DeatchCollection() { BehaviorCollection behaviorCollection = new BehaviorCollection() { new StubBehavior() }; Button button = new Button(); Interaction.SetBehaviors(button, behaviorCollection); Interaction.SetBehaviors(button, null); foreach (StubBehavior behavior in behaviorCollection) { Assert.AreEqual(1, behavior.DetachCount, "Should only have called Detach once."); Assert.IsNull(behavior.AssociatedObject, "AssociatedObject should be null after Detach."); } }
public void VectorChanged_ReplaceWhileAttached_OldDetachedNewAttached() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Attach(new Button()); StubBehavior first = new StubBehavior(); behaviorCollection.Add(first); StubBehavior second = new StubBehavior(); behaviorCollection[0] = second; TestUtilities.AssertDetached(first); TestUtilities.AssertAttached(second, behaviorCollection.AssociatedObject); }
public void SetBehaviors_MultipleBehaviors_AllAttached() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); Button button = new Button(); Interaction.SetBehaviors(button, behaviorCollection); foreach (StubBehavior behavior in behaviorCollection) { Assert.AreEqual(1, behavior.AttachCount, "Should only have called Attach once."); Assert.AreEqual(0, behavior.DetachCount, "Should not have called Detach."); Assert.AreEqual(button, behavior.AssociatedObject, "Should be attached to the host of the BehaviorCollection."); } }
private static void OnBehaviorsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { BehaviorCollection oldCollection = (BehaviorCollection)args.OldValue; BehaviorCollection newCollection = (BehaviorCollection)args.NewValue; if (oldCollection == newCollection) { return; } if (oldCollection != null && oldCollection.AssociatedObject != null) { oldCollection.Detach(); } if (newCollection != null && sender != null) { newCollection.Attach(sender); } }
public void SetBehaviors_ManualDetachThenNull_DoesNotDoubleDetach() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); Button button = new Button(); Interaction.SetBehaviors(button, behaviorCollection); foreach (StubBehavior behavior in behaviorCollection) { behavior.Detach(); } Interaction.SetBehaviors(button, null); foreach (StubBehavior behavior in behaviorCollection) { Assert.AreEqual(1, behavior.DetachCount, "Setting BehaviorCollection to null should not call Detach on already Detached Behaviors."); Assert.IsNull(behavior.AssociatedObject, "AssociatedObject should be null after Detach."); } }
public void VectorChanged_RemoveWhileAttached_Detached() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Attach(new ToggleSwitch()); StubBehavior behavior = new StubBehavior(); behaviorCollection.Add(behavior); behaviorCollection.Remove(behavior); TestUtilities.AssertDetached(behavior); }
public void Attach_MultipleBehaviors_AllAttached() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); Button button = new Button(); behaviorCollection.Attach(button); Assert.AreEqual(button, behaviorCollection.AssociatedObject, "Attach should set the AssociatedObject to the given parameter."); foreach (StubBehavior stub in behaviorCollection) { TestUtilities.AssertAttached(stub, button); } }
public void Attach_Null_AttachNotCalledOnItems() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Attach(null); foreach (StubBehavior stub in behaviorCollection) { TestUtilities.AssertNotAttached(stub); } }
public void Attach_MultipleObjects_ExceptionThrown() { BehaviorCollection behaviorCollection = new BehaviorCollection(); StubBehavior stub = new StubBehavior(); behaviorCollection.Attach(new Button()); TestUtilities.AssertThrowsException(() => behaviorCollection.Attach(new StackPanel())); }
public void Attach_NonNullThenNull_ExceptionThrown() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Attach(new Button()); TestUtilities.AssertThrowsException(() => behaviorCollection.Attach(null)); }
public void Attach_MultipleTimeSameObject_AttachCalledOnce() { BehaviorCollection behaviorCollection = new BehaviorCollection() { new StubBehavior() }; Button button = new Button(); behaviorCollection.Attach(button); behaviorCollection.Attach(button); // This method hard codes AttachCount == 1. TestUtilities.AssertAttached((StubBehavior)behaviorCollection[0], button); }
public void VectorChanged_RemoveWhileNotAttached_DetachNotCalled() { BehaviorCollection behaviorCollection = new BehaviorCollection(); StubBehavior behavior = new StubBehavior(); behaviorCollection.Add(behavior); behaviorCollection.Remove(behavior); TestUtilities.AssertNotDetached(behavior); }
public void VectorChanged_ResetWhileAttached_AllDetached() { StubBehavior[] behaviorArray = { new StubBehavior(), new StubBehavior(), new StubBehavior() }; BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Attach(new Button()); foreach (StubBehavior behavior in behaviorArray) { behaviorCollection.Add(behavior); } behaviorCollection.Clear(); foreach (StubBehavior behavior in behaviorArray) { TestUtilities.AssertDetached(behavior); } }
public void Detach_NotAttached_DetachNotCalledOnItems() { BehaviorCollection behaviorCollection = new BehaviorCollection() { new StubBehavior() }; behaviorCollection.Detach(); TestUtilities.AssertNotDetached((StubBehavior)behaviorCollection[0]); }
public void Detach_Attached_AllItemsDetached() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Attach(new Button()); behaviorCollection.Detach(); Assert.IsNull(behaviorCollection.AssociatedObject, "The AssociatedObject should be null after Detach."); foreach (StubBehavior behavior in behaviorCollection) { TestUtilities.AssertDetached(behavior); } }