Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring back ResourceDictionary.SetAndLoadSource #23683

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Controls/src/Core/ResourceDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using System.Runtime.CompilerServices;

using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Xaml;

namespace Microsoft.Maui.Controls
{
Expand All @@ -25,6 +24,9 @@ public class ResourceDictionary : IResourceDictionary, IDictionary<string, objec
ResourceDictionary _mergedInstance;
Uri _source;

// This action is instantiated in a module initializer in ResourceDictionaryHotReloadHelper
internal static Action<ResourceDictionary, Uri, string, Assembly, System.Xml.IXmlLineInfo> s_setAndLoadSource;

/// <include file="../../docs/Microsoft.Maui.Controls/ResourceDictionary.xml" path="//Member[@MemberName='Source']/Docs/*" />
[System.ComponentModel.TypeConverter(typeof(RDSourceTypeConverter))]
public Uri Source
Expand All @@ -48,6 +50,19 @@ public void SetAndCreateSource<T>(Uri value)
SetSource(value, instance);
}

// Used by hot reload
/// <include file="../../docs/Microsoft.Maui.Controls/ResourceDictionary.xml" path="//Member[@MemberName='SetAndLoadSource']/Docs/*" />
[RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)]
internal void SetAndLoadSource(Uri value, string resourcePath, Assembly assembly, global::System.Xml.IXmlLineInfo lineInfo)
{
if (s_setAndLoadSource is null)
{
throw new InvalidOperationException("ResourceDictionary.SetAndLoadSource was not initialized");
}

s_setAndLoadSource(this, value, resourcePath, assembly, lineInfo);
}

internal static ResourceDictionary GetOrCreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type type)
{
return s_instances.GetValue(type, _ => (ResourceDictionary)Activator.CreateInstance(type));
Expand Down
31 changes: 29 additions & 2 deletions src/Controls/src/Xaml/ResourceDictionaryHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Xml;

namespace Microsoft.Maui.Controls.Xaml
Expand All @@ -14,14 +15,15 @@ namespace Microsoft.Maui.Controls.Xaml
#endif
public static class ResourceDictionaryHelpers
{
// Used from XamlC generated code
// Called from XamlC generated code when the ResourceDictionary is not compiled
[EditorBrowsable(EditorBrowsableState.Never)]
public static void LoadFromSource(ResourceDictionary rd, Uri source, string resourcePath, Assembly assembly, IXmlLineInfo lineInfo)
{
var sourceInstance = CreateFromResource(resourcePath, assembly, lineInfo);
rd.SetSource(source, sourceInstance);
}

// Called from XamlParser
internal static void LoadFromSource(ResourceDictionary rd, string value, Type rootType, IXmlLineInfo lineInfo)
{
(value, var assembly) = ResourceDictionary.RDSourceTypeConverter.SplitUriAndAssembly(value, rootType.Assembly);
Expand All @@ -30,12 +32,18 @@ internal static void LoadFromSource(ResourceDictionary rd, string value, Type ro
var resourcePath = ResourceDictionary.RDSourceTypeConverter.GetResourcePath(new Uri(value, UriKind.Relative), rootTargetPath);
var sourceUri = ResourceDictionary.RDSourceTypeConverter.CombineUriAndAssembly(value, assembly);

SetAndLoadSource(rd, sourceUri, resourcePath, assembly, lineInfo);
}

// Called from Hot Reload
internal static void SetAndLoadSource(ResourceDictionary rd, Uri value, string resourcePath, Assembly assembly, IXmlLineInfo lineInfo)
{
var type = XamlResourceIdAttribute.GetTypeForPath(assembly, resourcePath);
var sourceInstance = type is not null
? ResourceDictionary.GetOrCreateInstance(type)
: CreateFromResource(resourcePath, assembly, lineInfo);

rd.SetSource(sourceUri, sourceInstance);
rd.SetSource(value, sourceInstance);
}

static ResourceDictionary CreateFromResource(string resourcePath, Assembly assembly, IXmlLineInfo lineInfo)
Expand Down Expand Up @@ -72,4 +80,23 @@ static ResourceDictionary CreateFromResource(string resourcePath, Assembly assem
}
}
}

#if !NETSTANDARD
internal static class ResourceDictionaryHotReloadHelper
{
#pragma warning disable CA2255 // The 'ModuleInitializer' attribute should not be used in libraries
[ModuleInitializer]
#pragma warning restore CA2255
internal static void Init()
{
// This code will be trimmed in production builds
if (HotReload.MauiHotReloadHelper.IsSupported)
{
#pragma warning disable IL2026, IL3050
ResourceDictionary.s_setAndLoadSource = ResourceDictionaryHelpers.SetAndLoadSource;
#pragma warning restore IL2026, IL3050
}
}
}
#endif
}
Loading