Add Content lookup markupextension

This commit is contained in:
Daniel Clark
2022-12-14 20:07:45 +00:00
parent b77ee21d56
commit f7e682e0db
32 changed files with 783 additions and 229 deletions
@@ -0,0 +1,40 @@
namespace Toolkit.Framework.Foundation;
public class NamedContentTemplateFactory : INamedContentTemplateFactory
{
private readonly Dictionary<string, object> cache = new();
private readonly IContentTemplateDescriptorProvider provider;
private readonly IServiceFactory serviceFactory;
public NamedContentTemplateFactory(IContentTemplateDescriptorProvider provider,
IServiceFactory serviceFactory)
{
this.provider = provider;
this.serviceFactory = serviceFactory;
}
public virtual object? Create(string name)
{
if (cache.TryGetValue(name, out object? view))
{
return view;
}
if (provider.Get(name) is IContentTemplateDescriptor descriptor)
{
view = serviceFactory.Create(descriptor.TemplateType);
if (view is ICache cache)
{
this.cache[name] = cache;
}
if (descriptor.GetType().GenericTypeArguments is { Length: 2 })
{
(descriptor as dynamic).ViewInvoker?.Invoke(view);
}
}
return view;
}
}