From cdd6d4defc844b605890e7ad731e27e373b09bf4 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Sun, 17 Apr 2016 23:59:44 +0200 Subject: Merged ResourceDictionary (#97) * [X] Support Merged RD * [X] Support RD as xaml roots * [XamlC] I have no idea how that used to work before * [C] Remove debugging statements * fix docs --- Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs | 4 +-- Xamarin.Forms.Xaml/CreateValuesVisitor.cs | 4 ++- .../FillResourceDictionariesVisitor.cs | 29 +++++++++++++++++++--- Xamarin.Forms.Xaml/HydratationContext.cs | 6 ++--- Xamarin.Forms.Xaml/ViewExtensions.cs | 4 +-- Xamarin.Forms.Xaml/XamlLoader.cs | 4 +-- Xamarin.Forms.Xaml/XamlNode.cs | 16 +++++++----- 7 files changed, 47 insertions(+), 20 deletions(-) (limited to 'Xamarin.Forms.Xaml') diff --git a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs index 8978f4fd..0a0651a7 100644 --- a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs +++ b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs @@ -133,7 +133,7 @@ namespace Xamarin.Forms.Xaml if (typeof (IEnumerable).GetTypeInfo().IsAssignableFrom(Context.Types[parentElement].GetTypeInfo())) { var source = Values[parentNode]; - if (Context.Types[parentElement] != typeof (ResourceDictionary)) + if (!(typeof (ResourceDictionary).IsAssignableFrom(Context.Types[parentElement]))) { var addMethod = Context.Types[parentElement].GetRuntimeMethods().First(mi => mi.Name == "Add" && mi.GetParameters().Length == 1); @@ -291,7 +291,7 @@ namespace Xamarin.Forms.Xaml return null; } - public static void SetPropertyValue(object xamlelement, XmlName propertyName, object value, BindableObject rootElement, + public static void SetPropertyValue(object xamlelement, XmlName propertyName, object value, object rootElement, INode node, HydratationContext context, IXmlLineInfo lineInfo) { var elementType = xamlelement.GetType(); diff --git a/Xamarin.Forms.Xaml/CreateValuesVisitor.cs b/Xamarin.Forms.Xaml/CreateValuesVisitor.cs index d54a36d5..612b32f1 100644 --- a/Xamarin.Forms.Xaml/CreateValuesVisitor.cs +++ b/Xamarin.Forms.Xaml/CreateValuesVisitor.cs @@ -149,7 +149,9 @@ namespace Xamarin.Forms.Xaml var rnode = (XamlLoader.RuntimeRootNode)node; Values[node] = rnode.Root; Context.Types[node] = rnode.Root.GetType(); - NameScope.SetNameScope(rnode.Root as BindableObject, node.Namescope); + var bindableRoot = rnode.Root as BindableObject; + if (bindableRoot != null) + NameScope.SetNameScope(bindableRoot, node.Namescope); } public void Visit(ListNode node, INode parentNode) diff --git a/Xamarin.Forms.Xaml/FillResourceDictionariesVisitor.cs b/Xamarin.Forms.Xaml/FillResourceDictionariesVisitor.cs index 5eb47887..c140084a 100644 --- a/Xamarin.Forms.Xaml/FillResourceDictionariesVisitor.cs +++ b/Xamarin.Forms.Xaml/FillResourceDictionariesVisitor.cs @@ -37,6 +37,27 @@ namespace Xamarin.Forms.Xaml public void Visit(ValueNode node, INode parentNode) { + var parentElement = parentNode as IElementNode; + var value = Values [node]; + var source = Values [parentNode]; + + XmlName propertyName; + if (ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName)) { + if (parentElement.SkipProperties.Contains(propertyName)) + return; + if (parentElement.SkipPrefix(node.NamespaceResolver.LookupPrefix(propertyName.NamespaceURI))) + return; + if (propertyName.NamespaceURI == "http://schemas.openxmlformats.org/markup-compatibility/2006" && + propertyName.LocalName == "Ignorable") { + (parentNode.IgnorablePrefixes ?? (parentNode.IgnorablePrefixes = new List ())).AddRange ( + (value as string).Split (',')); + return; + } + if (propertyName.LocalName != "MergedWith") + return; + ApplyPropertiesVisitor.SetPropertyValue(source, propertyName, value, Context.RootElement, node, Context, node); + } + } public void Visit(MarkupNode node, INode parentNode) @@ -56,10 +77,10 @@ namespace Xamarin.Forms.Xaml //Set Resources in ResourcesDictionaries if (IsCollectionItem(node, parentNode) && parentNode is IElementNode) { - if (typeof (IEnumerable).GetTypeInfo().IsAssignableFrom(Context.Types[parentElement].GetTypeInfo())) + if (typeof (IEnumerable).IsAssignableFrom(Context.Types[parentElement])) { var source = Values[parentNode]; - if (Context.Types[parentElement] == typeof (ResourceDictionary) && value is Style && + if (typeof (ResourceDictionary).IsAssignableFrom(Context.Types[parentElement]) && value is Style && !node.Properties.ContainsKey(XmlName.xKey)) { node.Accept(new ApplyPropertiesVisitor(Context), parentNode); @@ -75,9 +96,9 @@ namespace Xamarin.Forms.Xaml } ((ResourceDictionary)source).Add(value as Style); } - else if (Context.Types[parentElement] == typeof (ResourceDictionary) && !node.Properties.ContainsKey(XmlName.xKey)) + else if (typeof (ResourceDictionary).IsAssignableFrom(Context.Types[parentElement]) && !node.Properties.ContainsKey(XmlName.xKey)) throw new XamlParseException("resources in ResourceDictionary require a x:Key attribute", node); - else if (Context.Types[parentElement] == typeof (ResourceDictionary) && node.Properties.ContainsKey(XmlName.xKey)) + else if (typeof (ResourceDictionary).IsAssignableFrom(Context.Types[parentElement]) && node.Properties.ContainsKey(XmlName.xKey)) { node.Accept(new ApplyPropertiesVisitor(Context), parentNode); if (markupExtension != null) diff --git a/Xamarin.Forms.Xaml/HydratationContext.cs b/Xamarin.Forms.Xaml/HydratationContext.cs index 7c1356d9..7273a2cc 100644 --- a/Xamarin.Forms.Xaml/HydratationContext.cs +++ b/Xamarin.Forms.Xaml/HydratationContext.cs @@ -17,8 +17,8 @@ namespace Xamarin.Forms.Xaml public HydratationContext ParentContext { get; set; } - public BindableObject RootElement { get; set; } - public bool DoNotThrowOnExceptions { get; set; } + + public object RootElement { get; set; } } -} \ No newline at end of file +} diff --git a/Xamarin.Forms.Xaml/ViewExtensions.cs b/Xamarin.Forms.Xaml/ViewExtensions.cs index fb8a612a..38d5becf 100644 --- a/Xamarin.Forms.Xaml/ViewExtensions.cs +++ b/Xamarin.Forms.Xaml/ViewExtensions.cs @@ -31,13 +31,13 @@ namespace Xamarin.Forms.Xaml { public static class Extensions { - public static TView LoadFromXaml(this TView view, Type callingType) where TView : BindableObject + public static TXaml LoadFromXaml(this TXaml view, Type callingType) { XamlLoader.Load(view, callingType); return view; } - internal static TView LoadFromXaml(this TView view, string xaml) where TView : BindableObject + internal static TXaml LoadFromXaml(this TXaml view, string xaml) { XamlLoader.Load(view, xaml); return view; diff --git a/Xamarin.Forms.Xaml/XamlLoader.cs b/Xamarin.Forms.Xaml/XamlLoader.cs index 575f2c7d..37bea4e4 100644 --- a/Xamarin.Forms.Xaml/XamlLoader.cs +++ b/Xamarin.Forms.Xaml/XamlLoader.cs @@ -39,7 +39,7 @@ namespace Xamarin.Forms.Xaml { static readonly Dictionary XamlResources = new Dictionary(); - public static void Load(BindableObject view, Type callingType) + public static void Load(object view, Type callingType) { var xaml = GetXamlForType(callingType); if (string.IsNullOrEmpty(xaml)) @@ -47,7 +47,7 @@ namespace Xamarin.Forms.Xaml Load(view, xaml); } - public static void Load(BindableObject view, string xaml) + public static void Load(object view, string xaml) { using (var reader = XmlReader.Create(new StringReader(xaml))) { diff --git a/Xamarin.Forms.Xaml/XamlNode.cs b/Xamarin.Forms.Xaml/XamlNode.cs index aa8e6c82..6d84c67d 100644 --- a/Xamarin.Forms.Xaml/XamlNode.cs +++ b/Xamarin.Forms.Xaml/XamlNode.cs @@ -159,7 +159,7 @@ namespace Xamarin.Forms.Xaml visitor.Visit(this, parentNode); } - static bool IsDataTemplate(INode node, INode parentNode) + internal static bool IsDataTemplate(INode node, INode parentNode) { var parentElement = parentNode as IElementNode; INode createContent; @@ -169,7 +169,7 @@ namespace Xamarin.Forms.Xaml return false; } - static bool IsResourceDictionary(INode node, INode parentNode) + internal static bool IsResourceDictionary(INode node, INode parentNode) { var enode = node as ElementNode; return enode.XmlType.Name == "ResourceDictionary"; @@ -186,10 +186,14 @@ namespace Xamarin.Forms.Xaml { if (!visitor.VisitChildrenFirst) visitor.Visit(this, parentNode); - foreach (var node in Properties.Values.ToList()) - node.Accept(visitor, this); - foreach (var node in CollectionItems) - node.Accept(visitor, this); + if ((!visitor.StopOnDataTemplate || !IsDataTemplate(this, parentNode)) && + (!visitor.StopOnResourceDictionary || !IsResourceDictionary(this, parentNode))) + { + foreach (var node in Properties.Values.ToList()) + node.Accept(visitor, this); + foreach (var node in CollectionItems) + node.Accept(visitor, this); + } if (visitor.VisitChildrenFirst) visitor.Visit(this, parentNode); } -- cgit v1.2.3