Resource loading #815
Resource loading #815
Changes from 4 commits
b8ccc60
a249d64
827fa1b
2d6e78d
c7f2c59
33ca284
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
namespace Xamarin.Forms.Internals | ||
{ | ||
public static class ResourceLoader | ||
{ | ||
public static Func<string, string> ResourceProvider { get; internal set; } | ||
internal static Action<Exception> ExceptionHandler { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Mono.Cecil" version="0.10.0-beta4" targetFramework="net451" /> | ||
</packages> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,9 +32,11 @@ | |
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 @@ public static class XamlLoader | |
|
||
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 @@ public static void Load(object view, string 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 @@ public static object Create (string xaml, bool doNotThrow = false) | |
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 @@ static void Visit (RootNode rootnode, HydratationContext visitorContext) | |
|
||
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; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you checking that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you've just hit the nail on the head - if you set both, and XamlFileProvider does give valid data, then ResourceProvider will not prevail. Was that expected? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤦♂️ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a case when I should rebase to hide my embarrassment |
||
var assembly = type.GetTypeInfo().Assembly; | ||
|
@@ -189,7 +197,8 @@ static string GetXamlForType(Type type) | |
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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<Type Name="ResourceLoader" FullName="Xamarin.Forms.Internals.ResourceLoader"> | ||
<TypeSignature Language="C#" Value="public static class ResourceLoader" /> | ||
<TypeSignature Language="ILAsm" Value=".class public auto ansi abstract sealed beforefieldinit ResourceLoader extends System.Object" /> | ||
<AssemblyInfo> | ||
<AssemblyName>Xamarin.Forms.Core</AssemblyName> | ||
<AssemblyVersion>2.0.0.0</AssemblyVersion> | ||
</AssemblyInfo> | ||
<Base> | ||
<BaseTypeName>System.Object</BaseTypeName> | ||
</Base> | ||
<Interfaces /> | ||
<Docs> | ||
<summary>To be added.</summary> | ||
<remarks>To be added.</remarks> | ||
</Docs> | ||
<Members> | ||
<Member MemberName="ResourceProvider"> | ||
<MemberSignature Language="C#" Value="public static Func<string,string> ResourceProvider { get; }" /> | ||
<MemberSignature Language="ILAsm" Value=".property class System.Func`2<string, string> ResourceProvider" /> | ||
<MemberType>Property</MemberType> | ||
<AssemblyInfo> | ||
<AssemblyVersion>2.0.0.0</AssemblyVersion> | ||
</AssemblyInfo> | ||
<ReturnValue> | ||
<ReturnType>System.Func<System.String,System.String></ReturnType> | ||
</ReturnValue> | ||
<Docs> | ||
<summary>To be added.</summary> | ||
<value>To be added.</value> | ||
<remarks>To be added.</remarks> | ||
</Docs> | ||
</Member> | ||
</Members> | ||
</Type> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a debugging helper?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, it's not. adding that white line makes the build output clearer