/* * private object GetResource(DrawingElement drawingElement, string resourceKey) * { * if (drawingElement.Resources.ContainsKey(resourceKey)) * return drawingElement.Resources[resourceKey]; * else if (drawingElement.Parent != null) * return GetResource(drawingElement.Parent, resourceKey); * else * return null; * }*/ private string ResolveName(XamlElement rootElement) { if (string.IsNullOrEmpty(rootElement.Namespace)) { return(string.Format("{0}.{1}", DefaultNamespace, rootElement.Name)); } else if (rootElement.Namespace == @"http://mobileui.codeplex.com/v1") { var assembly = System.Reflection.Assembly.GetExecutingAssembly(); var attributes = assembly.GetCustomAttributes(typeof(XamlNamespaceMappingAttribute), true); foreach (XamlNamespaceMappingAttribute attribute in attributes) { var fullTypeName = string.Format("{0}.{1}", attribute.ClrNamespace, rootElement.Name, false); var resolvedType = assembly.GetType(fullTypeName); if (resolvedType != null) { return(resolvedType.FullName); } } } var splitNamespace = rootElement.Namespace.Split(','); return(string.Format("{0}.{1},{2}", splitNamespace[0], rootElement.Name, splitNamespace.Length == 1 ? DefaultAssemblyName : splitNamespace[1])); }
public void DockPanelLayoutParseTest() { string text = @" <DockPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <FrameworkElement Width='100' DockPanel.Dock='Left'/> <FrameworkElement Height='100' DockPanel.Dock='Top'/> <FrameworkElement Width='100' DockPanel.Dock='Right'/> <FrameworkElement Height='100' DockPanel.Dock='Bottom'/> <FrameworkElement/> </DockPanel>"; XamlElement rootElement = XamlParser.Parse(text); DockPanel panel = XamlLoader.Load(rootElement) as DockPanel; panel.Measure(new Size(1000, 1000)); Assert.AreEqual(new Size(200, 200), panel.DesiredSize); panel.Arrange(new Rect(1000, 1000)); Assert.AreEqual(new Size(1000, 1000), panel.VisualSize); Assert.AreEqual(new Size(100, 1000), panel.Children[0].VisualSize); Assert.AreEqual(new Size(900, 100), panel.Children[1].VisualSize); Assert.AreEqual(new Size(100, 900), panel.Children[2].VisualSize); Assert.AreEqual(new Size(800, 100), panel.Children[3].VisualSize); Assert.AreEqual(new Size(800, 800), panel.Children[4].VisualSize); Assert.AreEqual(new Point(0, 0), panel.Children[0].VisualOffset); Assert.AreEqual(new Point(100, 0), panel.Children[1].VisualOffset); Assert.AreEqual(new Point(900, 100), panel.Children[2].VisualOffset); Assert.AreEqual(new Point(100, 900), panel.Children[3].VisualOffset); Assert.AreEqual(new Point(100, 100), panel.Children[4].VisualOffset); }
public void XamlLoadKeyElementTest() { 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 Value1='1' Value2='2'> <x:Key> <test:LoaderTestElement Value1='3' Value2='4'/> </x:Key> </test:LoaderTestElement> </ResourceDictionary>"; XamlElement rootElement = XamlParser.Parse(text); ResourceDictionary dictionary = XamlLoader.Load(rootElement) as ResourceDictionary; Assert.IsNotNull(dictionary); Assert.AreEqual(1, dictionary.Count); LoaderTestElement key = (LoaderTestElement)dictionary.Keys.First(); LoaderTestElement value = (LoaderTestElement)dictionary[key]; Assert.AreEqual(1, value.Value1); Assert.AreEqual(2, value.Value2); Assert.AreEqual(3, key.Value1); Assert.AreEqual(4, key.Value2); }
public void XamlLoadCollectionReplacePropertyTest() { string text = @" <test:LoaderTestCollectionElement xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:test='clr-namespace:Granular.Presentation.Tests.Markup;assembly=Granular.Presentation.Tests'> <test:LoaderTestCollectionElement.Items> <test:LoaderTestCollection> <test:LoaderTestElement Value1='1'/> <test:LoaderTestElement Value1='2'/> <test:LoaderTestElement Value1='3'/> </test:LoaderTestCollection> </test:LoaderTestCollectionElement.Items> </test:LoaderTestCollectionElement>"; XamlElement rootElement = XamlParser.Parse(text); object root1 = XamlLoader.Load(rootElement); Assert.IsTrue(root1 is LoaderTestCollectionElement); Assert.AreEqual(3, (root1 as LoaderTestCollectionElement).Items.Count); Assert.IsTrue((root1 as LoaderTestCollectionElement).Items[0] is LoaderTestElement); Assert.IsTrue((root1 as LoaderTestCollectionElement).Items[1] is LoaderTestElement); Assert.IsTrue((root1 as LoaderTestCollectionElement).Items[2] is LoaderTestElement); Assert.AreEqual(1, ((root1 as LoaderTestCollectionElement).Items[0] as LoaderTestElement).Value1); Assert.AreEqual(2, ((root1 as LoaderTestCollectionElement).Items[1] as LoaderTestElement).Value1); Assert.AreEqual(3, ((root1 as LoaderTestCollectionElement).Items[2] as LoaderTestElement).Value1); }
public void CanvasLayoutParseTest() { string text = @" <Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <FrameworkElement Width='200' Height='100' Canvas.Left='20' Canvas.Top='10'/> <FrameworkElement Width='200' Height='100' Canvas.Right='20' Canvas.Top='10'/> <FrameworkElement Width='200' Height='100' Canvas.Right='20' Canvas.Bottom='10'/> <FrameworkElement Width='200' Height='100' Canvas.Left='20' Canvas.Bottom='10'/> </Canvas>"; XamlElement rootElement = XamlParser.Parse(text); Canvas panel = XamlLoader.Load(rootElement) as Canvas; panel.Measure(new Size(1000, 1000)); Assert.AreEqual(Size.Zero, panel.DesiredSize); panel.Arrange(new Rect(1000, 1000)); Assert.AreEqual(new Size(1000, 1000), panel.VisualSize); Assert.AreEqual(new Size(200, 100), panel.Children[0].VisualSize); Assert.AreEqual(new Size(200, 100), panel.Children[1].VisualSize); Assert.AreEqual(new Size(200, 100), panel.Children[2].VisualSize); Assert.AreEqual(new Size(200, 100), panel.Children[3].VisualSize); Assert.AreEqual(new Point(20, 10), panel.Children[0].VisualOffset); Assert.AreEqual(new Point(780, 10), panel.Children[1].VisualOffset); Assert.AreEqual(new Point(780, 890), panel.Children[2].VisualOffset); Assert.AreEqual(new Point(20, 890), panel.Children[3].VisualOffset); }
public void MarkupCompatibilityNestedIgnoreProperties() { XamlElement root = XamlParser.Parse(@" <root xmlns='namespace' xmlns:n1='namepsace1' xmlns:n2='namepsace2' xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' property='valuevalue' n1:property='value1' n2:property='value2'> <element mc:Ignorable='n1' property='valuevalue' n1:property='value1' n2:property='value2'> <element mc:Ignorable='n2' property='valuevalue' n1:property='value1' n2:property='value2'/> </element> </root>"); Assert.AreEqual(3, root.Members.Count()); Assert.AreEqual(2, ((XamlElement)root.Values.First()).Members.Count()); Assert.AreEqual(1, ((XamlElement)((XamlElement)root.Values.First()).Values.First()).Members.Count()); }
public void ParseTest() { XamlElement root1 = XamlParser.Parse(@" <root1 xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:ns1='namespace1' xmlns:ns2='namespace2' member1='value1' ns2:member2='value2'> <!-- comment --> <ns1:child1/> <child2> value3 <child3> </child3> value4 </child2> </root1>"); Assert.AreEqual("root1", root1.Name.LocalName); Assert.AreEqual(2, root1.Members.Count()); Assert.IsTrue(root1.Members.Any(member => member.Name.Equals(new XamlName("member1", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")) && (string)member.Values.Single() == "value1")); Assert.IsTrue(root1.Members.Any(member => member.Name.Equals(new XamlName("member2", "namespace2")) && (string)member.Values.Single() == "value2")); Assert.AreEqual(2, root1.Values.Count()); Assert.AreEqual("child1", ((XamlElement)root1.Values.First()).Name.LocalName); Assert.AreEqual("namespace1", ((XamlElement)root1.Values.First()).Name.NamespaceName); Assert.AreEqual("value3", ((XamlElement)root1.Values.Last()).Values.ElementAt(0)); Assert.AreEqual("child3", ((XamlElement)((XamlElement)root1.Values.Last()).Values.ElementAt(1)).Name.LocalName); Assert.AreEqual("value4", ((XamlElement)root1.Values.Last()).Values.ElementAt(2)); }
public void XamlClassParserBasicTest() { string text = @" <ContentControl x:Class='Granular.BuildTasks.Tests.TestClass' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:test='clr-namespace:Granular.BuildTasks.Tests'> <Control.Background> <SolidColorBrush x:Name='brush1' Color='Red'/> </Control.Background> <ContentControl.Content> <test:TestElement x:Name='content1'> <FrameworkElement x:Name='content2'/> </test:TestElement> </ContentControl.Content> </ContentControl>"; XamlElement root = XamlParser.Parse(text); ClassDefinition classDefinition = XamlClassParser.Parse(root, new TestTypeParser()); MemberDefinition[] expectedMembers = new[] { new MemberDefinition("brush1", "System.Windows.Media.SolidColorBrush"), new MemberDefinition("content1", "Granular.BuildTasks.Tests.TestElement"), new MemberDefinition("content2", "System.Windows.FrameworkElement") }; Assert.AreEqual("System.Windows.Controls.ContentControl", classDefinition.BaseTypeName); Assert.AreEqual("TestClass", classDefinition.Name); Assert.AreEqual("Granular.BuildTasks.Tests", classDefinition.Namespace); CollectionAssert.AreEqual(expectedMembers, classDefinition.Members.ToArray()); }
private object CreateInstance(object parentInstance, XamlElement rootElement) { var className = ResolveName(rootElement); var type = Type.GetType(className, true, false); var constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.CreateInstance | BindingFlags.Instance); var hostConstructor = (from c in constructors where c.GetParameters().Count() == 0 /*.Any(p => p.ParameterType == typeof(IDrawingHost))*/ select c).FirstOrDefault(); if (hostConstructor != null) { return(hostConstructor.Invoke(new object[] { /*_host*/ })); } object[] args; var bestConstructor = GetBestMatchingConstructor(constructors, parentInstance, rootElement, out args); if (bestConstructor != null) { return(bestConstructor.Invoke(args)); } else { return(Activator.CreateInstance(type)); } }
public object Render(object parentInstance, XamlElement rootElement) { var drawingElement = GetInstanceForMarkupElement(parentInstance, rootElement); //var drawingContainer = drawingElement as DrawingContainer; foreach (var childElement in rootElement.Children) { if (childElement.IsMember) { // Must be a property or event var propertyName = childElement.MemberName; foreach (var subChildElement in childElement.Children) { var propertyValue = Render(drawingElement, subChildElement); SetProperty(drawingElement, propertyName, propertyValue, subChildElement.ResourceKey); } } /* * else if (drawingContainer != null) * { * // Must be a child element * var childDrawingElement = (DrawingElement)Render(drawingElement, childElement); * }*/ } return(drawingElement); }
public void StackPanelLayoutParseTest() { string text = @" <StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <FrameworkElement Height='100'/> <FrameworkElement Height='100'/> <FrameworkElement Height='100'/> </StackPanel>"; XamlElement rootElement = XamlParser.Parse(text); StackPanel panel = XamlLoader.Load(rootElement) as StackPanel; panel.Measure(new Size(1000, 1000)); Assert.AreEqual(new Size(0, 300), panel.DesiredSize); panel.Arrange(new Rect(1000, 300)); Assert.AreEqual(new Size(1000, 300), panel.VisualSize); Assert.AreEqual(new Size(1000, 100), panel.Children[0].VisualSize); Assert.AreEqual(new Size(1000, 100), panel.Children[1].VisualSize); Assert.AreEqual(new Size(1000, 100), panel.Children[2].VisualSize); Assert.AreEqual(new Point(0, 0), panel.Children[0].VisualOffset); Assert.AreEqual(new Point(0, 100), panel.Children[1].VisualOffset); Assert.AreEqual(new Point(0, 200), panel.Children[2].VisualOffset); }
private void SetProperties(object parentInstance, object instance, XamlElement rootElement) { //var drawingElement = parentInstance as DrawingElement; foreach (var property in rootElement.Attributes) { SetProperty(instance, property); } }
public void MarkupCompatibilityIgnoreDeclaringElement() { XamlElement root = XamlParser.Parse(@" <root xmlns='namespace' xmlns:n1='namepsace1' xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'> <n1:element mc:Ignorable='n1'/> <n1:element /> </root>"); Assert.AreEqual(1, root.Values.Count()); }
public void XamlMarkupExtensionTest() { string text = @"<test:LoaderTestElement xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:test='clr-namespace:Granular.Presentation.Tests.Markup;assembly=Granular.Presentation.Tests' Value3='{test:LoaderTestBinding Path=A.B.C}'/>"; XamlElement rootElement = XamlParser.Parse(text); object root1 = XamlLoader.Load(rootElement); Assert.IsTrue((root1 as LoaderTestElement).Value3 is LoaderTestBindingExpression); Assert.AreEqual("A.B.C", ((root1 as LoaderTestElement).Value3 as LoaderTestBindingExpression).Path); }
public void ParseExplicitPropertiesTest() { XamlNamespaces namespaces = new XamlNamespaces(String.Empty, "default-namespace"); XamlElement root1 = (XamlElement)MarkupExtensionParser.Parse("{root1 property1=value1, property2=value2}", namespaces); Assert.AreEqual("root1", root1.Name.LocalName); Assert.AreEqual(2, root1.Members.Count()); Assert.AreEqual("property1", root1.Members.ElementAt(0).Name.LocalName); Assert.AreEqual("value1", root1.Members.ElementAt(0).Values.Single()); Assert.AreEqual("property2", root1.Members.ElementAt(1).Name.LocalName); Assert.AreEqual("value2", root1.Members.ElementAt(1).Values.Single()); }
public void ParseNamespaceTest() { XamlElement root1 = XamlParser.Parse(@" <root1 xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:ns1='namespace1'> <element1 xmlns:ns2='namespace2'/> </root1>"); Assert.AreEqual("http://schemas.microsoft.com/winfx/2006/xaml/presentation", root1.Namespaces.GetNamespace(String.Empty)); Assert.AreEqual("namespace1", root1.Namespaces.GetNamespace("ns1")); Assert.AreEqual("namespace2", ((XamlElement)root1.Values.Single()).Namespaces.GetNamespace("ns2")); }
private XamlElement FindXamlElement(XamlElement parent, int row, int column) { var start = parent.Start; var end = parent.End; var startRow = start.Row + 1; var endRow = end.Row + 1; if (startRow <= row && start.Column <= column && endRow >= row && end.Column >= column) { var node = parent as XamlNode; if (node != null) { foreach (var childNode in node.ChildNodes) { var childResult = FindXamlElement(childNode, row, column); if (childResult != null) { return(childResult); } } if (node.Value != null) { var valueResult = FindXamlElement(node.Value, row, column); if (valueResult != null) { return(valueResult); } } foreach (var attribute in node.Attributes) { var attributeResult = FindXamlElement(attribute, row, column); if (attributeResult != null) { return(attributeResult); } } return(node); } return(parent); } return(null); }
public static ClassDefinition Parse(XamlElement element, ITypeParser typeParser) { string fullName = GetClassFullName(element); if (fullName.IsNullOrEmpty()) { return(null); } string baseTypeName = typeParser.ParseTypeName(element.Name); IEnumerable <MemberDefinition> members = GetMembers(element, baseTypeName, typeParser).ToArray(); return(new ClassDefinition(GetTypeName(fullName), GetTypeNamespace(fullName), baseTypeName, members)); }
public void ParseNamespaceTest() { XamlNamespaces namespaces = new XamlNamespaces(new[] { new NamespaceDeclaration("ns1", "namespace1"), new NamespaceDeclaration("ns2", "namespace2"), }); XamlElement root1 = (XamlElement)MarkupExtensionParser.Parse("{ns1:root1 value1={ns2:child2}}", namespaces); Assert.AreEqual("root1", root1.Name.LocalName); Assert.AreEqual("namespace1", root1.Name.NamespaceName); Assert.AreEqual("child2", (root1.Members.First().Values.Single() as XamlElement).Name.LocalName); Assert.AreEqual("namespace2", (root1.Members.First().Values.Single() as XamlElement).Name.NamespaceName); }
public void MarkupCompatibilityIgnoreMultipleNamespaces() { XamlElement root = XamlParser.Parse(@" <root xmlns='namespace' xmlns:n1='namepsace1' xmlns:n2='namepsace2' xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' mc:Ignorable='n1 n2' property='value' n1:property='value1' n2:property='value2'> </root>"); Assert.AreEqual(1, root.Members.Count()); }
private object GetInstanceForMarkupElement(object parentInstance, XamlElement rootElement) { var instance = CreateInstance(parentInstance, rootElement); /* * if (instance is DrawingElement && parentInstance is DrawingContainer) * ((DrawingContainer)parentInstance).Children.Add((DrawingElement)instance); */ if (rootElement.IsNamed) { _resourceDictionary.Add(rootElement.ResourceName, instance); } SetProperties(parentInstance, instance, rootElement); return(instance); }
private void LoadStartupUri() { if (StartupUri.IsNullOrEmpty()) { return; } XamlElement rootElement = XamlParser.Parse(Granular.Compatibility.String.FromByteArray(EmbeddedResourceLoader.LoadResourceData(StartupUri))); XamlMember classDirective = rootElement.Directives.FirstOrDefault(directive => directive.Name == XamlLanguage.ClassDirective); Window window = Activator.CreateInstance(Type.GetType(String.Format("{0}, {1}", classDirective.GetSingleValue(), GetType().Assembly.GetName().Name))) as Window; if (window != null) { window.Show(); } }
public void ContentPresenterBasicTest() { string text = @" <ContentPresenter 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.Controls;assembly=Granular.Presentation.Tests'> <ContentPresenter.Resources> <DataTemplate DataType='{x:Type test:ContentPresenterTestData}'> <FrameworkElement Width='{Binding Value}'/> </DataTemplate> </ContentPresenter.Resources> </ContentPresenter>"; XamlElement rootElement = XamlParser.Parse(text); ContentPresenter contentPresenter = XamlLoader.Load(rootElement) as ContentPresenter; ContentPresenterTestData data = new ContentPresenterTestData { Value = 100 }; Assert.AreEqual(0, contentPresenter.VisualChildren.Count()); contentPresenter.Content = data; Assert.AreEqual(1, contentPresenter.VisualChildren.Count()); FrameworkElement templateChild = contentPresenter.VisualChildren.First() as FrameworkElement; Assert.IsNotNull(templateChild); Assert.AreEqual(data, templateChild.DataContext); Assert.AreEqual(100, templateChild.Width); contentPresenter.Content = null; Assert.AreEqual(0, contentPresenter.VisualChildren.Count()); object content = new object(); contentPresenter.Content = content; Assert.AreEqual(1, contentPresenter.VisualChildren.Count()); TextBlock templateChild2 = contentPresenter.VisualChildren.First() as TextBlock; Assert.IsNotNull(templateChild2); Assert.AreEqual(content.ToString(), templateChild2.Text); }
public object GetValueKey(XamlElement element) { XamlMember keyMember = element.Members.SingleOrDefault(member => member.Name.LocalName == "Key"); if (keyMember != null) { return(ElementFactory.FromValue(keyMember.Values.Single(), typeof(object), element.Namespaces, element.SourceUri).CreateElement(new InitializeContext())); } XamlMember targetTypeMember = element.Members.SingleOrDefault(member => member.Name.LocalName == "TargetType"); if (targetTypeMember != null) { return(new StyleKey((Type)ElementFactory.FromValue(targetTypeMember.Values.Single(), typeof(Type), element.Namespaces, element.SourceUri).CreateElement(new InitializeContext()))); } throw new Granular.Exception($"Can't create value key from \"{element.Name}\""); }
public void GridLayoutParseTest() { string text = @" <Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <Grid.ColumnDefinitions> <ColumnDefinition Width='*'/> <ColumnDefinition Width='2*'/> <ColumnDefinition Width='3*'/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <FrameworkElement Height='100'/> <FrameworkElement Height='100' Grid.Column='1'/> <FrameworkElement Height='100' Grid.Column='2'/> <FrameworkElement Height='100' Grid.Row='1' Grid.ColumnSpan='2'/> <FrameworkElement Height='100' Grid.Row='2' Grid.Column='1' Grid.ColumnSpan='2'/> </Grid>"; XamlElement rootElement = XamlParser.Parse(text); Grid panel = XamlLoader.Load(rootElement) as Grid; panel.Measure(new Size(600, 600)); Assert.AreEqual(new Size(0, 300), panel.DesiredSize); panel.Arrange(new Rect(600, 300)); Assert.AreEqual(new Size(600, 300), panel.VisualSize); Assert.AreEqual(new Size(100, 100), panel.Children[0].VisualSize); Assert.AreEqual(new Size(200, 100), panel.Children[1].VisualSize); Assert.AreEqual(new Size(300, 100), panel.Children[2].VisualSize); Assert.AreEqual(new Size(300, 100), panel.Children[3].VisualSize); Assert.AreEqual(new Size(500, 100), panel.Children[4].VisualSize); Assert.AreEqual(new Point(0, 0), panel.Children[0].VisualOffset); Assert.AreEqual(new Point(100, 0), panel.Children[1].VisualOffset); Assert.AreEqual(new Point(300, 0), panel.Children[2].VisualOffset); Assert.AreEqual(new Point(0, 100), panel.Children[3].VisualOffset); Assert.AreEqual(new Point(100, 200), panel.Children[4].VisualOffset); }
private ITaskItem GenerateCodeFile(ITaskItem item, ITypeParser typeParser, XamlItemType itemType) { XamlElement xamlElement = XamlParser.Parse(File.ReadAllText(item.GetMetadata("FullPath"))); ClassDefinition classDefinition = XamlClassParser.Parse(xamlElement, typeParser); if (classDefinition == null) { return(null); } CodeTypeDeclaration classDeclaration = classDefinition.CreateClassDeclaration(); classDeclaration.CustomAttributes.Add(new CodeAttributeDeclaration("Bridge.Reflectable", new CodeAttributeArgument(new CodeSnippetExpression("Bridge.MemberAccessibility.InstanceMethod")), new CodeAttributeArgument(new CodeSnippetExpression("Bridge.MemberAccessibility.InstanceField")))); string itemRelativePath = item.GetRelativePath().Replace("\\", "/"); string resourceUri = String.Format("pack://application:,,,/{0};component/{1}", TargetName, itemRelativePath); classDeclaration.Members.Add(CreateInitializeComponentMethod(resourceUri)); if (itemType == XamlItemType.XamlApplicationDefinition) { classDeclaration.Members.Add(CreateEntryPointMethod(classDefinition.Name)); } CodeNamespace classNamespace = new CodeNamespace(classDefinition.Namespace); classNamespace.Types.Add(classDeclaration); CodeCompileUnit classCompileUnit = new CodeCompileUnit(); classCompileUnit.Namespaces.Add(classNamespace); string targetDirectory = Path.GetDirectoryName(Path.Combine(IntermediateOutputPath, item.GetMetadata("Link").DefaultIfNullOrEmpty(item.GetMetadata("Identity")))); Directory.CreateDirectory(targetDirectory); string targetFileName = String.Format("{0}.g{1}{2}", item.GetMetadata("Filename"), item.GetMetadata("Extension"), LanguageSourceExtension); return(GenerateCodeFile(classCompileUnit, Path.Combine(targetDirectory, targetFileName))); }
public object GetValueKey(XamlElement element) { XamlMember keyMember = element.Members.SingleOrDefault(member => member.Name.LocalName == "Key"); if (keyMember != null) { object value = keyMember.Values.Single(); return(value is XamlElement?XamlLoader.Load((XamlElement)value) : value); } XamlMember partialKeyMember = element.Members.SingleOrDefault(member => member.Name.LocalName == "PartialKey"); if (partialKeyMember != null) { return(TestDictionaryValue.CreateKey(partialKeyMember.Values.Single().ToString())); } throw new Granular.Exception($"Can't create value key from \"{element.Name}\""); }
public void XamlLoadFrameworkElementFactoryTest() { string text = @" <test:LoaderTestFactoryElement xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:test='clr-namespace:Granular.Presentation.Tests.Markup;assembly=Granular.Presentation.Tests'> <test:LoaderTestElement Value1='1' Value2='2'/> </test:LoaderTestFactoryElement>"; XamlElement rootElement = XamlParser.Parse(text); object root1 = XamlLoader.Load(rootElement); Assert.IsTrue(root1 is LoaderTestFactoryElement); Assert.IsTrue(((LoaderTestFactoryElement)root1).Factory != null); LoaderTestElement contentElement = ((LoaderTestFactoryElement)root1).Factory.CreateElement(null) as LoaderTestElement; Assert.IsNotNull(contentElement); Assert.AreEqual(1, contentElement.Value1); Assert.AreEqual(2, contentElement.Value2); }
public void WrapPanelLayoutParseTest() { string text = @" <WrapPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <FrameworkElement Width='200' Height='100'/> <FrameworkElement Width='200' Height='100'/> <FrameworkElement Width='200' Height='60' Margin='0,10'/> <FrameworkElement Width='200' Height='60' Margin='0,10'/> <FrameworkElement Width='200' Height='60'/> </WrapPanel>"; XamlElement rootElement = XamlParser.Parse(text); WrapPanel panel = XamlLoader.Load(rootElement) as WrapPanel; panel.IsRootElement = true; panel.Measure(new Size(1000, 1000)); Assert.AreEqual(new Size(1000, 100), panel.DesiredSize); panel.Arrange(new Rect(1000, 100)); Assert.AreEqual(new Size(1000, 100), panel.RenderSize); Assert.AreEqual(new Point(0, 0), panel.Children[0].VisualOffset); Assert.AreEqual(new Point(200, 0), panel.Children[1].VisualOffset); Assert.AreEqual(new Point(400, 20), panel.Children[2].VisualOffset); Assert.AreEqual(new Point(600, 20), panel.Children[3].VisualOffset); Assert.AreEqual(new Point(800, 20), panel.Children[4].VisualOffset); panel.Measure(new Size(500, 1000)); Assert.AreEqual(new Size(400, 240), panel.DesiredSize); panel.Arrange(new Rect(500, 240)); Assert.AreEqual(new Point(0, 0), panel.Children[0].VisualOffset); Assert.AreEqual(new Point(200, 0), panel.Children[1].VisualOffset); Assert.AreEqual(new Point(0, 110), panel.Children[2].VisualOffset); Assert.AreEqual(new Point(200, 110), panel.Children[3].VisualOffset); Assert.AreEqual(new Point(0, 180), panel.Children[4].VisualOffset); }
public void ParseChildrenTest() { XamlNamespaces namespaces = new XamlNamespaces(String.Empty, "default-namespace"); XamlElement root1 = (XamlElement)MarkupExtensionParser.Parse("{root1 property1={child1 value1, property2=value2}, property3={child3 property4=value4}}", namespaces); Assert.AreEqual("root1", root1.Name.LocalName); Assert.AreEqual(2, root1.Members.Count()); Assert.AreEqual("property1", root1.Members.ElementAt(0).Name.LocalName); Assert.IsTrue(root1.Members.ElementAt(0).Values.Single() is XamlElement); Assert.AreEqual("child1", (root1.Members.ElementAt(0).Values.Single() as XamlElement).Name.LocalName); Assert.IsTrue((root1.Members.ElementAt(0).Values.Single() as XamlElement).Members.Count() == 2); Assert.AreEqual("value1", (root1.Members.ElementAt(0).Values.Single() as XamlElement).Members.ElementAt(0).Values.Single()); Assert.AreEqual("value2", (root1.Members.ElementAt(0).Values.Single() as XamlElement).Members.ElementAt(1).Values.Single()); Assert.IsTrue(root1.Members.ElementAt(1).Values.Single() is XamlElement); Assert.AreEqual("child3", (root1.Members.ElementAt(1).Values.Single() as XamlElement).Name.LocalName); Assert.IsTrue((root1.Members.ElementAt(1).Values.Single() as XamlElement).Members.Count() == 1); Assert.AreEqual("property4", (root1.Members.ElementAt(1).Values.Single() as XamlElement).Members.ElementAt(0).Name.LocalName); Assert.AreEqual("value4", (root1.Members.ElementAt(1).Values.Single() as XamlElement).Members.ElementAt(0).Values.Single()); }