Add project files.
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using FluentAvalonia.UI.Controls;
|
||||
|
||||
namespace TheXamlGuy.Framework.Avalonia;
|
||||
|
||||
public class ContentDialogRouteHandler : RouteHandler<ContentDialog>
|
||||
{
|
||||
public override async void Handle(Route<ContentDialog> request)
|
||||
{
|
||||
if (request.Template is ContentDialog contentDialog)
|
||||
{
|
||||
contentDialog.DataContext = request.Data;
|
||||
await contentDialog.ShowAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class ContentControlRouteHandler : RouteHandler<ContentControl>
|
||||
{
|
||||
public override void Handle(Route<ContentControl> request)
|
||||
{
|
||||
if (request.Template is TemplatedControl control)
|
||||
{
|
||||
control.DataContext = request.Data;
|
||||
request.Target.Content = control;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using FluentAvalonia.UI.Controls;
|
||||
using FluentAvalonia.UI.Navigation;
|
||||
|
||||
namespace TheXamlGuy.Framework.Avalonia;
|
||||
|
||||
public class FrameRouteHandler : RouteHandler<Frame>
|
||||
{
|
||||
public override void Handle(Route<Frame> request)
|
||||
{
|
||||
if (request.Template is Type type)
|
||||
{
|
||||
void HandleNavigated(object sender, NavigationEventArgs args)
|
||||
{
|
||||
request.Target.Navigated -= HandleNavigated;
|
||||
if (request.Target.Content is TemplatedControl control)
|
||||
{
|
||||
control.DataContext = request.Data;
|
||||
}
|
||||
}
|
||||
|
||||
request.Target.Navigated += HandleNavigated;
|
||||
request.Target.Navigate(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace TheXamlGuy.Framework.Avalonia;
|
||||
|
||||
public interface IRouteDescriptor
|
||||
{
|
||||
object Route { get; }
|
||||
|
||||
string? Name { get; }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace TheXamlGuy.Framework.Avalonia;
|
||||
|
||||
public interface IRouteDescriptorCollection : IList<IRouteDescriptor>
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace TheXamlGuy.Framework.Avalonia;
|
||||
|
||||
public interface IRouter
|
||||
{
|
||||
void Add(string name, object route);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using TheXamlGuy.Framework.Core;
|
||||
|
||||
namespace TheXamlGuy.Framework.Avalonia
|
||||
{
|
||||
public class NavigateHandler : IMediatorHandler<Navigate>
|
||||
{
|
||||
private readonly IEventAggregator eventAggregator;
|
||||
|
||||
public NavigateHandler(IEventAggregator eventAggregator)
|
||||
{
|
||||
this.eventAggregator = eventAggregator;
|
||||
}
|
||||
|
||||
public void Handle(Navigate request)
|
||||
{
|
||||
eventAggregator.Publish(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace TheXamlGuy.Framework.Avalonia;
|
||||
|
||||
public class Navigated<TContent, TDataContext> where TContent : class where TDataContext : class
|
||||
{
|
||||
public Navigated()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Navigated(TContent content, TDataContext dataContext, IDictionary<string, object>? parameters = null)
|
||||
{
|
||||
Content = content;
|
||||
DataContext = dataContext;
|
||||
Parameters = parameters;
|
||||
}
|
||||
|
||||
public TContent? Content { get; }
|
||||
|
||||
public TDataContext? DataContext { get; }
|
||||
|
||||
public IDictionary<string, object>? Parameters { get; }
|
||||
}
|
||||
|
||||
public class Navigated
|
||||
{
|
||||
public static Navigated<TContent, TDataTemplate> Create<TContent, TDataTemplate>(TContent content, TDataTemplate dataContext, IDictionary<string, object>? parameters = null) where TContent : class where TDataTemplate : class
|
||||
{
|
||||
return new Navigated<TContent, TDataTemplate>(content, dataContext, parameters);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
|
||||
namespace TheXamlGuy.Framework.Avalonia;
|
||||
|
||||
public record Route<TTarget>(TTarget Target, object? Data, object? Template) where TTarget : TemplatedControl;
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace TheXamlGuy.Framework.Avalonia;
|
||||
|
||||
public record RouteDescriptor : IRouteDescriptor
|
||||
{
|
||||
public RouteDescriptor(string name, object route)
|
||||
{
|
||||
Name = name;
|
||||
Route = route;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public object Route { get; }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace TheXamlGuy.Framework.Avalonia;
|
||||
|
||||
public class RouteDescriptorCollection : List<IRouteDescriptor>, IRouteDescriptorCollection
|
||||
{
|
||||
public RouteDescriptorCollection(IEnumerable<IRouteDescriptor> collection) : base(collection)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using TheXamlGuy.Framework.Core;
|
||||
|
||||
namespace TheXamlGuy.Framework.Avalonia;
|
||||
|
||||
public abstract class RouteHandler<TTarget> : IMediatorHandler<Route<TTarget>> where TTarget : TemplatedControl
|
||||
{
|
||||
public abstract void Handle(Route<TTarget> request);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
|
||||
namespace TheXamlGuy.Framework.Avalonia;
|
||||
|
||||
public class Router : IRouter
|
||||
{
|
||||
private readonly IRouteDescriptorCollection routes;
|
||||
|
||||
public Router(IRouteDescriptorCollection routes)
|
||||
{
|
||||
this.routes = routes;
|
||||
}
|
||||
|
||||
public void Add(string name, object route)
|
||||
{
|
||||
if (route is TemplatedControl control)
|
||||
{
|
||||
void HandleUnloaded(object? sender, RoutedEventArgs args)
|
||||
{
|
||||
if (routes.FirstOrDefault(x => x.Route == sender) is IRouteDescriptor descriptor)
|
||||
{
|
||||
routes.Remove(descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
control.Unloaded += HandleUnloaded;
|
||||
}
|
||||
|
||||
if (routes.FirstOrDefault(x => x.Name == name) is IRouteDescriptor descriptor)
|
||||
{
|
||||
routes.Remove(descriptor);
|
||||
}
|
||||
|
||||
routes.Add(new RouteDescriptor(name, route));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using FluentAvalonia.UI.Controls;
|
||||
using TheXamlGuy.Framework.Core;
|
||||
|
||||
namespace TheXamlGuy.Framework.Avalonia;
|
||||
|
||||
public class RouterContext : IRouterContext
|
||||
{
|
||||
private readonly IRouteDescriptorCollection descriptors;
|
||||
private readonly IDisposer disposer;
|
||||
private readonly IEventAggregator eventAggregator;
|
||||
private readonly IMediator mediator;
|
||||
private readonly INamedDataTemplateFactory namedDataTemplateFactory;
|
||||
private readonly INamedTemplateFactory namedTemplateFactory;
|
||||
private readonly ITemplateDescriptorProvider templateDescriptorProvider;
|
||||
private readonly ITemplateFactory templateFactory;
|
||||
private readonly ITypedDataTemplateFactory typedDataTemplateFactory;
|
||||
|
||||
public RouterContext(ITemplateDescriptorProvider templateDescriptorProvider,
|
||||
ITemplateFactory templateFactory,
|
||||
INamedTemplateFactory namedTemplateFactory,
|
||||
INamedDataTemplateFactory namedDataTemplateFactory,
|
||||
ITypedDataTemplateFactory typedDataTemplateFactory,
|
||||
IEventAggregator eventAggregator,
|
||||
IMediator mediator,
|
||||
IDisposer disposer,
|
||||
IRouteDescriptorCollection descriptors)
|
||||
{
|
||||
this.templateDescriptorProvider = templateDescriptorProvider;
|
||||
this.templateFactory = templateFactory;
|
||||
this.namedTemplateFactory = namedTemplateFactory;
|
||||
this.namedDataTemplateFactory = namedDataTemplateFactory;
|
||||
this.typedDataTemplateFactory = typedDataTemplateFactory;
|
||||
this.eventAggregator = eventAggregator;
|
||||
this.mediator = mediator;
|
||||
this.disposer = disposer;
|
||||
this.descriptors = descriptors;
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
disposer.Add(this, eventAggregator.Current.SubscribeUI<Navigate>(OnNavigate));
|
||||
disposer.Add(this, eventAggregator.Current.SubscribeUI<NavigateBack>(OnNavigateBack));
|
||||
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void OnNavigate(Navigate args)
|
||||
{
|
||||
object? data = null;
|
||||
object? template = null;
|
||||
|
||||
Dictionary<string, object> keyedParameters = new();
|
||||
List<object> parameters = new();
|
||||
|
||||
foreach (object? parameter in args.Parameters)
|
||||
{
|
||||
if (parameter is KeyValuePair<string, object> keyed)
|
||||
{
|
||||
keyedParameters.Add(keyed.Key, keyed.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
parameters.Add(parameter);
|
||||
}
|
||||
}
|
||||
|
||||
if (args.Name is { Length: > 0 } name)
|
||||
{
|
||||
data = namedDataTemplateFactory.Create(name, parameters.ToArray());
|
||||
template = descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is { Route: Frame } ? templateDescriptorProvider.Get(name)?.TemplateType : namedTemplateFactory.Create(name);
|
||||
}
|
||||
|
||||
if (args.Type is Type type)
|
||||
{
|
||||
data = typedDataTemplateFactory.Create(type, parameters.ToArray());
|
||||
template = descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is { Route: Frame } ? templateDescriptorProvider.Get(type)?.TemplateType : templateFactory.Create(data);
|
||||
}
|
||||
|
||||
if (template is not null)
|
||||
{
|
||||
if (template is ContentDialog contentDialog)
|
||||
{
|
||||
mediator.Handle(new Route<ContentDialog>(contentDialog, data, template));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is RouteDescriptor descriptor)
|
||||
{
|
||||
switch (descriptor.Route)
|
||||
{
|
||||
case Frame frame:
|
||||
mediator.Handle(new Route<Frame>(frame, data, template));
|
||||
break;
|
||||
case ContentControl contentControl:
|
||||
mediator.Handle(new Route<ContentControl>(contentControl, data, template));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is RouteDescriptor descriptor)
|
||||
{
|
||||
if (descriptor.Route is ContentControl contentControl)
|
||||
{
|
||||
contentControl.Content = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnNavigateBack(NavigateBack args)
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is RouteDescriptor descriptor)
|
||||
{
|
||||
if (descriptor.Route is ContentControl { Content: TemplatedControl content })
|
||||
{
|
||||
if (content.DataContext is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
if (descriptor.Route is Frame frame)
|
||||
{
|
||||
frame.GoBack();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user