diff options
author | Stephane Delcroix <stephane@delcroix.org> | 2017-03-16 20:09:22 +0100 |
---|---|---|
committer | Samantha Houts <samantha@teamredwall.com> | 2017-03-16 12:09:22 -0700 |
commit | 467c1befa1379c2644a3607d87db9a6178575306 (patch) | |
tree | dd5153c7d38ca310177f56a24587ad69d56f0498 /Xamarin.Forms.Xaml | |
parent | eb8b4d19450e3264fdd20e964f48facca6521070 (diff) | |
download | xamarin-forms-467c1befa1379c2644a3607d87db9a6178575306.tar.gz xamarin-forms-467c1befa1379c2644a3607d87db9a6178575306.tar.bz2 xamarin-forms-467c1befa1379c2644a3607d87db9a6178575306.zip |
Resource loading (#815)
* Resource Loader
* Replace XamlLoader API by ResourceLoader for the Previewer
* instruct generated IL to use the resourceLoader
* [docs] update docs
* oops
* [docs] fix docs
Diffstat (limited to 'Xamarin.Forms.Xaml')
-rw-r--r-- | Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs | 4 | ||||
-rw-r--r-- | Xamarin.Forms.Xaml/HydratationContext.cs | 6 | ||||
-rw-r--r-- | Xamarin.Forms.Xaml/XamlFilePathAttribute.cs | 2 | ||||
-rw-r--r-- | Xamarin.Forms.Xaml/XamlLoader.cs | 23 |
4 files changed, 22 insertions, 13 deletions
diff --git a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs index 5b8a8403..337b2ace 100644 --- a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs +++ b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs @@ -305,8 +305,8 @@ namespace Xamarin.Forms.Xaml return; xpe = xpe ?? new XamlParseException($"Cannot assign property \"{localName}\": Property does not exists, or is not assignable, or mismatching type between value and property", lineInfo); - if (context.DoNotThrowOnExceptions) - System.Diagnostics.Debug.WriteLine(xpe.Message); + if (context.ExceptionHandler != null) + context.ExceptionHandler(xpe); else throw xpe; } diff --git a/Xamarin.Forms.Xaml/HydratationContext.cs b/Xamarin.Forms.Xaml/HydratationContext.cs index 7273a2cc..172bce17 100644 --- a/Xamarin.Forms.Xaml/HydratationContext.cs +++ b/Xamarin.Forms.Xaml/HydratationContext.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; namespace Xamarin.Forms.Xaml { - internal class HydratationContext + class HydratationContext { public HydratationContext() { @@ -17,8 +17,8 @@ namespace Xamarin.Forms.Xaml public HydratationContext ParentContext { get; set; } - public bool DoNotThrowOnExceptions { get; set; } + public Action<Exception> ExceptionHandler { get; set; } public object RootElement { get; set; } } -} +}
\ No newline at end of file diff --git a/Xamarin.Forms.Xaml/XamlFilePathAttribute.cs b/Xamarin.Forms.Xaml/XamlFilePathAttribute.cs index 615f290b..7a6d665b 100644 --- a/Xamarin.Forms.Xaml/XamlFilePathAttribute.cs +++ b/Xamarin.Forms.Xaml/XamlFilePathAttribute.cs @@ -3,7 +3,7 @@ using System.Runtime.CompilerServices; namespace Xamarin.Forms.Xaml { - [AttributeUsage(AttributeTargets.Class, Inherited = false)] + [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] public sealed class XamlFilePathAttribute : Attribute { public XamlFilePathAttribute([CallerFilePath] string filePath = "") diff --git a/Xamarin.Forms.Xaml/XamlLoader.cs b/Xamarin.Forms.Xaml/XamlLoader.cs index 7fee40da..cdf73bdd 100644 --- a/Xamarin.Forms.Xaml/XamlLoader.cs +++ b/Xamarin.Forms.Xaml/XamlLoader.cs @@ -32,9 +32,11 @@ using System.IO; using System.Reflection; using System.Text.RegularExpressions; using System.Xml; +using Xamarin.Forms.Internals; namespace Xamarin.Forms.Xaml.Internals { + [Obsolete ("Replaced by ResourceLoader")] public static class XamlLoader { public static Func<Type, string> XamlFileProvider { get; internal set; } @@ -44,7 +46,7 @@ namespace Xamarin.Forms.Xaml.Internals namespace Xamarin.Forms.Xaml { - internal static class XamlLoader + static class XamlLoader { static readonly Dictionary<Type, string> XamlResources = new Dictionary<Type, string>(); @@ -75,7 +77,9 @@ namespace Xamarin.Forms.Xaml XamlParser.ParseXaml (rootnode, reader); Visit (rootnode, new HydratationContext { RootElement = view, - DoNotThrowOnExceptions = Xamarin.Forms.Xaml.Internals.XamlLoader.DoNotThrowOnExceptions +#pragma warning disable 0618 + ExceptionHandler = ResourceLoader.ExceptionHandler ?? (Internals.XamlLoader.DoNotThrowOnExceptions ? e => { }: (Action<Exception>)null) +#pragma warning restore 0618 }); break; } @@ -99,7 +103,7 @@ namespace Xamarin.Forms.Xaml var rootnode = new RuntimeRootNode (new XmlType (reader.NamespaceURI, reader.Name, null), null, (IXmlNamespaceResolver)reader); XamlParser.ParseXaml (rootnode, reader); var visitorContext = new HydratationContext { - DoNotThrowOnExceptions = doNotThrow, + ExceptionHandler = doNotThrow ? e => { } : (Action<Exception>)null, }; var cvv = new CreateValuesVisitor (visitorContext); cvv.Visit ((ElementNode)rootnode, null); @@ -127,10 +131,14 @@ namespace Xamarin.Forms.Xaml static string GetXamlForType(Type type) { - string xaml = null; - //the Previewer might want to provide it's own xaml for this... let them do that - if (Xamarin.Forms.Xaml.Internals.XamlLoader.XamlFileProvider != null && (xaml = Xamarin.Forms.Xaml.Internals.XamlLoader.XamlFileProvider(type)) != null) + //the check at the end is preferred (using ResourceLoader). keep this until all the previewers are updated + +#pragma warning disable 0618 + var xaml = Internals.XamlLoader.XamlFileProvider?.Invoke(type); +#pragma warning restore 0618 + + if (xaml != null && ResourceLoader.ResourceProvider == null) return xaml; var assembly = type.GetTypeInfo().Assembly; @@ -189,7 +197,8 @@ namespace Xamarin.Forms.Xaml return null; XamlResources[type] = resourceName; - return xaml; + var alternateXaml = ResourceLoader.ResourceProvider?.Invoke(resourceName); + return alternateXaml ?? xaml; } static bool ResourceMatchesFilename(Assembly assembly, string resource, string filename) |