コード例 #1
0
        /// <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;
        }
コード例 #2
0
ファイル: Interaction.cs プロジェクト: blinds52/XamlBehaviors
        /// <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;
        }
コード例 #3
0
        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;
        }
コード例 #4
0
        public void VectorChanged_BehaviorChangedToNonBehavior_ExceptionThrown()
        {
            BehaviorCollection behaviorCollection = new BehaviorCollection();
            behaviorCollection.Add(new StubBehavior());

            TestUtilities.AssertThrowsException(() => behaviorCollection[0] = new ToggleSwitch());
        }
コード例 #5
0
        public void VectorChanged_NonBehaviorAdded_ExceptionThrown()
        {
            BehaviorCollection behaviorCollection = new BehaviorCollection();
            behaviorCollection.Add(new StubBehavior());

            TestUtilities.AssertThrowsException(() => behaviorCollection.Add(new TextBlock()));
        }
コード例 #6
0
        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 );
            }
        }
コード例 #7
0
ファイル: Interaction.cs プロジェクト: blinds52/XamlBehaviors
 /// <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);
 }
コード例 #8
0
        public void VectorChanged_AddWhileNotAttached_AttachNotCalled()
        {
            BehaviorCollection behaviorCollection = new BehaviorCollection();
            StubBehavior stub = new StubBehavior();
            behaviorCollection.Add(stub);

            TestUtilities.AssertNotAttached(stub);
        }
コード例 #9
0
        public void VectorChanged_DuplicateAdd_ExceptionThrown()
        {
            BehaviorCollection behaviorCollection = new BehaviorCollection();
            StubBehavior stub = new StubBehavior();
            behaviorCollection.Add(stub);

            TestUtilities.AssertThrowsException(() => behaviorCollection.Add(stub));

        }
コード例 #10
0
        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.");
            }
        }
コード例 #11
0
        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.");
            }
        }
コード例 #12
0
        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);
            }
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        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.");
            }
        }
コード例 #15
0
        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.");
            }
        }
コード例 #16
0
        public void VectorChanged_RemoveWhileNotAttached_DetachNotCalled()
        {
            BehaviorCollection behaviorCollection = new BehaviorCollection();

            StubBehavior behavior = new StubBehavior();
            behaviorCollection.Add(behavior);
            behaviorCollection.Remove(behavior);

            TestUtilities.AssertNotDetached(behavior);
        }
コード例 #17
0
        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);
            }
        }
コード例 #18
0
        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);
            }
        }
コード例 #19
0
        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);
            }
        }
コード例 #20
0
        public void Detach_NotAttached_DetachNotCalledOnItems()
        {
            BehaviorCollection behaviorCollection = new BehaviorCollection() { new StubBehavior() };

            behaviorCollection.Detach();

            TestUtilities.AssertNotDetached((StubBehavior)behaviorCollection[0]);
        }
コード例 #21
0
        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);
        }
コード例 #22
0
        public void Attach_NonNullThenNull_ExceptionThrown()
        {
            BehaviorCollection behaviorCollection = new BehaviorCollection();
            behaviorCollection.Add(new StubBehavior());

            behaviorCollection.Attach(new Button());

            TestUtilities.AssertThrowsException(() => behaviorCollection.Attach(null));
        }
コード例 #23
0
        public void Attach_MultipleObjects_ExceptionThrown()
        {
            BehaviorCollection behaviorCollection = new BehaviorCollection();
            StubBehavior stub = new StubBehavior();
            behaviorCollection.Attach(new Button());

            TestUtilities.AssertThrowsException(() => behaviorCollection.Attach(new StackPanel()));
        }
コード例 #24
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);
            }
        }
コード例 #25
0
        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);
        }