public void RoutedEventSourceTest() { string text = @" <ResourceDictionary xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:test='clr-namespace:Granular.Presentation.Tests;assembly=Granular.Presentation.Tests' > <ControlTemplate x:Key='template1'> <StackPanel> <test:RoutedEventTestElement x:Name='child1' Width='100' Template='{DynamicResource template2}'/> </StackPanel> </ControlTemplate> <ControlTemplate x:Key='template2'> <StackPanel> <test:RoutedEventTestElement x:Name='child2' Width='200'/> </StackPanel> </ControlTemplate> </ResourceDictionary> "; ResourceDictionary resources = XamlLoader.Load(XamlParser.Parse(text)) as ResourceDictionary; RoutedEventTestElement root = new RoutedEventTestElement(); root.Resources = resources; root.Template = resources.GetValue("template1") as ControlTemplate; root.ApplyTemplate(); RoutedEventTestElement child1 = NameScope.GetTemplateNameScope(root).FindName("child1") as RoutedEventTestElement; child1.ApplyTemplate(); Assert.IsNotNull(child1); Assert.AreEqual(100, child1.Width); RoutedEventTestElement child2 = NameScope.GetTemplateNameScope(child1).FindName("child2") as RoutedEventTestElement; Assert.IsNotNull(child2); Assert.AreEqual(200, child2.Width); object source0 = null; object source1 = null; object source2 = null; root.Bubble += (sender, e) => source0 = e.Source; child1.Bubble += (sender, e) => source1 = e.Source; child2.Bubble += (sender, e) => source2 = e.Source; child2.RaiseEvent(new RoutedEventArgs(RoutedEventTestElement.BubbleEvent, child2)); Assert.AreEqual(root, source0); Assert.AreEqual(child1, source1); Assert.AreEqual(child2, source2); }
public void XamlLoadSharedTest() { string text = @" <ResourceDictionary xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:test='clr-namespace:Granular.Presentation.Tests.Markup;assembly=Granular.Presentation.Tests'> <test:LoaderTestElement x:Shared='false' x:Key='item1' Value1='1' Value2='2'/> <test:LoaderTestElement x:Shared='true' x:Key='item2' Value1='3' Value2='4'/> </ResourceDictionary>"; XamlElement rootElement = XamlParser.Parse(text); ResourceDictionary dictionary = XamlLoader.Load(rootElement) as ResourceDictionary; Assert.IsNotNull(dictionary); Assert.IsTrue(dictionary.Contains("item1")); Assert.IsTrue(dictionary.Contains("item2")); LoaderTestElement item1a = dictionary.GetValue("item1") as LoaderTestElement; Assert.IsNotNull(item1a); Assert.AreEqual(1, ((LoaderTestElement)item1a).Value1); Assert.AreEqual(2, ((LoaderTestElement)item1a).Value2); LoaderTestElement item1b = dictionary.GetValue("item1") as LoaderTestElement; Assert.IsFalse(ReferenceEquals(item1a, item1b)); Assert.IsNotNull(item1b); Assert.AreNotEqual(item1a, item1b); Assert.AreEqual(1, ((LoaderTestElement)item1b).Value1); Assert.AreEqual(2, ((LoaderTestElement)item1b).Value2); LoaderTestElement item2a = dictionary.GetValue("item2") as LoaderTestElement; Assert.IsNotNull(item2a); Assert.AreEqual(3, ((LoaderTestElement)item2a).Value1); Assert.AreEqual(4, ((LoaderTestElement)item2a).Value2); LoaderTestElement item2b = dictionary.GetValue("item2") as LoaderTestElement; Assert.IsTrue(ReferenceEquals(item2a, item2b)); }
public static Color GetColor(this ResourceDictionary resources, string key) => resources.GetValue(key) as Color? ?? DefaultColor;