Esempio n. 1
0
        public void FreezableBasicTest()
        {
            int resourcesChangedCount = 0;
            object resourceValue;

            Freezable freezable = new Freezable();
            freezable.ResourcesChanged += (sender, e) => resourcesChangedCount++;

            FrameworkElement element = new FrameworkElement();
            element.DataContext = "data-context";
            element.Resources = new ResourceDictionary();
            element.Resources.Add("key1", "value1");

            Assert.AreNotEqual("data-context", freezable.GetValue(FrameworkElement.DataContextProperty));
            Assert.IsFalse(freezable.TryGetResource("key1", out resourceValue));

            element.SetValue(ValueProperty, freezable);
            Assert.AreEqual("data-context", freezable.GetValue(FrameworkElement.DataContextProperty));
            Assert.AreEqual(1, resourcesChangedCount);
            Assert.IsTrue(freezable.TryGetResource("key1", out resourceValue));
            Assert.AreEqual("value1", resourceValue);

            element.Resources.Add("key2", "value2");
            Assert.AreEqual(2, resourcesChangedCount);
            Assert.IsTrue(freezable.TryGetResource("key2", out resourceValue));
            Assert.AreEqual("value2", resourceValue);
        }
Esempio n. 2
0
        public void FreezableCollectionBasicTest()
        {
            int resources1ChangedCount = 0;
            object resourceValue;

            Freezable freezable1 = new Freezable();
            freezable1.ResourcesChanged += (sender, e) => resources1ChangedCount++;

            FreezableCollection<Freezable> freezableCollection = new FreezableCollection<Freezable> { freezable1 };

            FrameworkElement element = new FrameworkElement();
            element.DataContext = "data-context";
            element.Resources = new ResourceDictionary();
            element.Resources.Add("key1", "value1");

            Assert.IsNull(freezable1.GetValue(FrameworkElement.DataContextProperty));
            Assert.IsFalse(freezable1.TryGetResource("key1", out resourceValue));

            element.SetValue(ValueProperty, freezableCollection);
            Assert.AreEqual("data-context", freezable1.GetValue(FrameworkElement.DataContextProperty));
            Assert.AreEqual(2, resources1ChangedCount);
            Assert.IsTrue(freezable1.TryGetResource("key1", out resourceValue));
            Assert.AreEqual("value1", resourceValue);

            Freezable freezable2 = new Freezable();
            freezableCollection.Add(freezable2);
            Assert.AreEqual("data-context", freezable2.GetValue(FrameworkElement.DataContextProperty));
            Assert.IsTrue(freezable2.TryGetResource("key1", out resourceValue));
            Assert.AreEqual("value1", resourceValue);

            freezableCollection.Clear();
            Assert.AreNotEqual("data-context", freezable1.GetValue(FrameworkElement.DataContextProperty));
            Assert.AreNotEqual("data-context", freezable2.GetValue(FrameworkElement.DataContextProperty));
            Assert.AreEqual(3, resources1ChangedCount);
            Assert.IsFalse(freezable1.TryGetResource("key1", out resourceValue));
            Assert.IsFalse(freezable2.TryGetResource("key1", out resourceValue));
        }