Add Content lookup markupextension
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using Avalonia;
|
||||
using Mediator;
|
||||
using System.Diagnostics;
|
||||
using Toolkit.Framework.Foundation;
|
||||
|
||||
namespace Toolkit.Framework.Avalonia;
|
||||
|
||||
public class ContentHandler : IRequestHandler<Content, object?>
|
||||
{
|
||||
private readonly INamedContentTemplateFactory namedContentTemplateFactory;
|
||||
private readonly INamedContentFactory namedContentFactory;
|
||||
private readonly IContentTemplateFactory contentTemplateFactory;
|
||||
private readonly ITypedContentFactory typedContentFactory;
|
||||
|
||||
public ContentHandler(IContentTemplateFactory contentTemplateFactory,
|
||||
INamedContentFactory namedContentFactory,
|
||||
INamedContentTemplateFactory namedContentTemplateFactory,
|
||||
ITypedContentFactory typedContentFactory)
|
||||
{
|
||||
this.contentTemplateFactory = contentTemplateFactory;
|
||||
this.namedContentFactory = namedContentFactory;
|
||||
this.namedContentTemplateFactory = namedContentTemplateFactory;
|
||||
this.typedContentFactory = typedContentFactory;
|
||||
}
|
||||
|
||||
public ValueTask<object?> Handle(Content request, CancellationToken cancellationToken)
|
||||
{
|
||||
object? content = null;
|
||||
object? template = null;
|
||||
|
||||
if (request.Name is { Length: > 0 } name)
|
||||
{
|
||||
content = namedContentFactory.Create(name, request.Parameters);
|
||||
template = namedContentTemplateFactory.Create(name);
|
||||
}
|
||||
|
||||
if (request.Type is Type type)
|
||||
{
|
||||
content = typedContentFactory.Create(type, request.Parameters);
|
||||
template = contentTemplateFactory.Create(content);
|
||||
}
|
||||
|
||||
if (template is Visual visual)
|
||||
{
|
||||
visual.DataContext = content;
|
||||
return new ValueTask<object?>(visual);
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Toolkit.Framework.Foundation;
|
||||
|
||||
namespace Toolkit.Framework.Avalonia;
|
||||
|
||||
public class ContentTemplateSelector : IDataTemplate, IContentTemplateSelector
|
||||
{
|
||||
private readonly Dictionary<object, IControl> dataTracking = new();
|
||||
|
||||
private readonly IContentTemplateFactory templateFactory;
|
||||
|
||||
public ContentTemplateSelector(IContentTemplateFactory templateFactory)
|
||||
{
|
||||
this.templateFactory = templateFactory;
|
||||
}
|
||||
|
||||
public IControl? Build(object? item)
|
||||
{
|
||||
if (item is not null)
|
||||
{
|
||||
if (dataTracking.TryGetValue(item, out IControl? control))
|
||||
{
|
||||
return control;
|
||||
}
|
||||
|
||||
return (IControl?)templateFactory.Create(item);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool Match(object? data)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user