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.Build.Tasks/SetPropertiesVisitor.cs | 6 ++-- Xamarin.Forms.Build.Tasks/SetResourcesVisitor.cs | 37 ++++++++++++++++++++-- Xamarin.Forms.Core/ResourceDictionary.cs | 29 +++++++++++++++-- .../SharedResourceDictionary.xaml | 14 ++++++++ .../SharedResourceDictionary.xaml.cs | 34 ++++++++++++++++++++ .../TestSharedResourceDictionary.xaml | 11 +++++++ .../TestSharedResourceDictionary.xaml.cs | 31 ++++++++++++++++++ .../Xamarin.Forms.Xaml.UnitTests.csproj | 12 +++++++ 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 ++++++---- .../Xamarin.Forms/ResourceDictionary.xml | 25 +++++++++++++-- 16 files changed, 236 insertions(+), 30 deletions(-) create mode 100644 Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary.xaml.cs create mode 100644 Xamarin.Forms.Xaml.UnitTests/TestSharedResourceDictionary.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/TestSharedResourceDictionary.xaml.cs diff --git a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs index 5470a3c8..692ef8b5 100644 --- a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs +++ b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs @@ -118,7 +118,7 @@ namespace Xamarin.Forms.Build.Tasks if (parentVar.VariableType.ImplementsInterface(Module.Import(typeof (IEnumerable)))) { var elementType = parentVar.VariableType; - if (elementType.FullName != "Xamarin.Forms.ResourceDictionary") + if (elementType.FullName != "Xamarin.Forms.ResourceDictionary" && elementType.Resolve().BaseType.FullName != "Xamarin.Forms.ResourceDictionary") { var adderTuple = elementType.GetMethods(md => md.Name == "Add" && md.Parameters.Count == 1, Module).First(); var adderRef = Module.Import(adderTuple.Item1); @@ -213,7 +213,7 @@ namespace Xamarin.Forms.Build.Tasks return parentList.CollectionItems.Contains(node); } - static string GetContentProperty(TypeReference typeRef) + internal static string GetContentProperty(TypeReference typeRef) { var typeDef = typeRef.Resolve(); var attributes = typeDef.CustomAttributes; @@ -461,7 +461,7 @@ namespace Xamarin.Forms.Build.Tasks context.IL.Emit(OpCodes.Ldloc, parent); context.IL.Append(vnode.PushConvertedValue(context, propertyType, - new ICustomAttributeProvider[] { propertyType.Resolve() }, + new ICustomAttributeProvider[] { property, propertyType.Resolve() }, valueNode.PushServiceProvider(context), false, true)); context.IL.Emit(OpCodes.Callvirt, propertySetterRef); diff --git a/Xamarin.Forms.Build.Tasks/SetResourcesVisitor.cs b/Xamarin.Forms.Build.Tasks/SetResourcesVisitor.cs index c054117c..d09c4b00 100644 --- a/Xamarin.Forms.Build.Tasks/SetResourcesVisitor.cs +++ b/Xamarin.Forms.Build.Tasks/SetResourcesVisitor.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Generic; using System.Linq; using Mono.Cecil; using Mono.Cecil.Cil; @@ -36,6 +37,33 @@ namespace Xamarin.Forms.Build.Tasks public void Visit(ValueNode node, INode parentNode) { + XmlName propertyName; + if (!SetPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName)) + { + if (!IsCollectionItem(node, parentNode)) + return; + string contentProperty; + if (!Context.Variables.ContainsKey((IElementNode)parentNode)) + return; + var parentVar = Context.Variables[(IElementNode)parentNode]; + if ((contentProperty = SetPropertiesVisitor.GetContentProperty(parentVar.VariableType)) != null) + propertyName = new XmlName(((IElementNode)parentNode).NamespaceURI, contentProperty); + else + return; + } + + if (node.SkipPrefix((node.NamespaceResolver ?? parentNode.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( + (node.Value as string).Split(',')); + return; + } + if (propertyName.LocalName != "MergedWith") + return; + SetPropertiesVisitor.SetPropertyValue(Context.Variables[(IElementNode)parentNode], propertyName, node, Context, node); } public void Visit(MarkupNode node, INode parentNode) @@ -52,7 +80,8 @@ namespace Xamarin.Forms.Build.Tasks var parentVar = Context.Variables[(IElementNode)parentNode]; if (parentVar.VariableType.ImplementsInterface(Module.Import(typeof (IEnumerable)))) { - if (parentVar.VariableType.FullName == "Xamarin.Forms.ResourceDictionary" && + if ((parentVar.VariableType.FullName == "Xamarin.Forms.ResourceDictionary" || + parentVar.VariableType.Resolve().BaseType.FullName == "Xamarin.Forms.ResourceDictionary") && !node.Properties.ContainsKey(XmlName.xKey)) { node.Accept(new SetPropertiesVisitor(Context), parentNode); @@ -79,7 +108,8 @@ namespace Xamarin.Forms.Build.Tasks .Resolve() .Methods.Single(md => md.Name == "Add" && md.Parameters.Count == 1))); } - else if (parentVar.VariableType.FullName == "Xamarin.Forms.ResourceDictionary" && + else if ((parentVar.VariableType.FullName == "Xamarin.Forms.ResourceDictionary" || + parentVar.VariableType.Resolve().BaseType.FullName == "Xamarin.Forms.ResourceDictionary") && node.Properties.ContainsKey(XmlName.xKey)) { node.Accept(new SetPropertiesVisitor(Context), parentNode); @@ -118,7 +148,8 @@ namespace Xamarin.Forms.Build.Tasks XmlName propertyName; if (SetPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName) && (propertyName.LocalName == "Resources" || propertyName.LocalName.EndsWith(".Resources", StringComparison.Ordinal)) && - Context.Variables[node].VariableType.FullName == "Xamarin.Forms.ResourceDictionary") + (Context.Variables[node].VariableType.FullName == "Xamarin.Forms.ResourceDictionary" || + Context.Variables[node].VariableType.Resolve().BaseType.FullName == "Xamarin.Forms.ResourceDictionary")) SetPropertiesVisitor.SetPropertyValue(Context.Variables[(IElementNode)parentNode], propertyName, node, Context, node); } diff --git a/Xamarin.Forms.Core/ResourceDictionary.cs b/Xamarin.Forms.Core/ResourceDictionary.cs index b19773ff..e5e08bc6 100644 --- a/Xamarin.Forms.Core/ResourceDictionary.cs +++ b/Xamarin.Forms.Core/ResourceDictionary.cs @@ -2,13 +2,38 @@ using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Reflection; +using System.Linq; namespace Xamarin.Forms { - public sealed class ResourceDictionary : IResourceDictionary, IDictionary + public class ResourceDictionary : IResourceDictionary, IDictionary { readonly Dictionary _innerDictionary = new Dictionary(); + Type _mergedWith; + [TypeConverter (typeof(TypeTypeConverter))] + public Type MergedWith { + get { return _mergedWith; } + set { + if (_mergedWith == value) + return; + _mergedWith = value; + if (_mergedWith == null) + return; + + _mergedInstance = _mergedWith.GetTypeInfo().BaseType.GetTypeInfo().DeclaredMethods.First(mi => mi.Name == "GetInstance").Invoke(null, new object[] {_mergedWith}) as ResourceDictionary; + } + } + + static ResourceDictionary _instance; + static ResourceDictionary GetInstance(Type type) + { + return _instance ?? (_instance = ((ResourceDictionary)Activator.CreateInstance (type))); + } + + ResourceDictionary _mergedInstance; + void ICollection>.Add(KeyValuePair item) { ((ICollection>)_innerDictionary).Add(item); @@ -94,7 +119,7 @@ namespace Xamarin.Forms public bool TryGetValue(string key, out object value) { - return _innerDictionary.TryGetValue(key, out value); + return _innerDictionary.TryGetValue(key, out value) || (_mergedInstance != null && _mergedInstance.TryGetValue(key, out value)); } event EventHandler IResourceDictionary.ValuesChanged diff --git a/Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary.xaml b/Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary.xaml new file mode 100644 index 00000000..8d86d57a --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary.xaml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary.xaml.cs new file mode 100644 index 00000000..f4f53cae --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary.xaml.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; + +using Xamarin.Forms; + +using NUnit.Framework; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public partial class SharedResourceDictionary : ResourceDictionary + { + public SharedResourceDictionary () + { + InitializeComponent (); + } + + public SharedResourceDictionary (bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + public class Tests + { + [TestCase (false)] + [TestCase (true)] + public void ResourcesDirectoriesCanBeXamlRoots (bool useCompiledXaml) + { + var layout = new SharedResourceDictionary (useCompiledXaml); + Assert.AreEqual (3, layout.Count); + } + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/TestSharedResourceDictionary.xaml b/Xamarin.Forms.Xaml.UnitTests/TestSharedResourceDictionary.xaml new file mode 100644 index 00000000..eb1eb7e4 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/TestSharedResourceDictionary.xaml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/TestSharedResourceDictionary.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/TestSharedResourceDictionary.xaml.cs new file mode 100644 index 00000000..dff3f893 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/TestSharedResourceDictionary.xaml.cs @@ -0,0 +1,31 @@ +using NUnit.Framework; + +using Xamarin.Forms; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public partial class TestSharedResourceDictionary : ContentPage + { + public TestSharedResourceDictionary () + { + InitializeComponent (); + } + + public TestSharedResourceDictionary (bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + public class Tests + { + [TestCase (false)] + [TestCase (true)] + public void MergedResourcesAreFound (bool useCompiledXaml) + { + var layout = new TestSharedResourceDictionary (useCompiledXaml); + Assert.AreEqual (Color.Pink, layout.label.TextColor); + } + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj index 961774f0..54433f56 100644 --- a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj +++ b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj @@ -335,6 +335,12 @@ McIgnorable.xaml + + SharedResourceDictionary.xaml + + + TestSharedResourceDictionary.xaml + @@ -596,6 +602,12 @@ MSBuild:UpdateDesignTimeXaml + + MSBuild:UpdateDesignTimeXaml + + + MSBuild:UpdateDesignTimeXaml + 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); } diff --git a/docs/Xamarin.Forms.Core/Xamarin.Forms/ResourceDictionary.xml b/docs/Xamarin.Forms.Core/Xamarin.Forms/ResourceDictionary.xml index 862bf40a..95690874 100644 --- a/docs/Xamarin.Forms.Core/Xamarin.Forms/ResourceDictionary.xml +++ b/docs/Xamarin.Forms.Core/Xamarin.Forms/ResourceDictionary.xml @@ -1,6 +1,6 @@ - - + + Xamarin.Forms.Core 1.0.0.0 @@ -243,6 +243,27 @@ To be added. + + + + Property + + 2.0.0.0 + + + + Xamarin.Forms.TypeConverter(typeof(Xamarin.Forms.TypeTypeConverter)) + + + + System.Type + + + To be added. + To be added. + To be added. + + -- cgit v1.2.3