Add project files.

This commit is contained in:
Daniel Clark
2022-11-01 15:26:08 +00:00
parent daa7b59f22
commit 7e4f880821
408 changed files with 16863 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace TheXamlGuy.Framework.WPF
{
public interface IRoute
{
void AddRoute(string name, object route);
}
}
+11
View File
@@ -0,0 +1,11 @@
using System.Collections.Generic;
namespace TheXamlGuy.Framework.WPF
{
public interface IRouteDescriptor
{
object Route { get; }
string? Name { get; }
}
}
@@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace TheXamlGuy.Framework.WPF
{
public interface IRouteDescriptorCollection : IList<IRouteDescriptor>
{
}
}
+20
View File
@@ -0,0 +1,20 @@
using TheXamlGuy.Framework.Core;
using TheXamlGuy.Framework.Core;
namespace TheXamlGuy.Framework.WPF
{
public class NavigateHandler : IMediatorHandler<Navigate>
{
private readonly IEventAggregator eventAggregator;
public NavigateHandler(IEventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;
}
public void Handle(Navigate request)
{
eventAggregator.Publish(request);
}
}
}
+33
View File
@@ -0,0 +1,33 @@
using System.Collections.Generic;
namespace TheXamlGuy.Framework.WPF
{
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);
}
}
}
+38
View File
@@ -0,0 +1,38 @@
using System.Linq;
using System.Windows;
namespace TheXamlGuy.Framework.WPF
{
public class Route : IRoute
{
private readonly IRouteDescriptorCollection routes;
public Route(IRouteDescriptorCollection routes)
{
this.routes = routes;
}
public void AddRoute(string name, object route)
{
if (route is FrameworkElement frameworkElement)
{
void HandleUnloaded(object sender, RoutedEventArgs args)
{
if (routes.FirstOrDefault(x => x.Route == sender) is IRouteDescriptor descriptor)
{
routes.Remove(descriptor);
}
}
frameworkElement.Unloaded += HandleUnloaded;
}
if (routes.FirstOrDefault(x => x.Name == name) is IRouteDescriptor descriptor)
{
routes.Remove(descriptor);
}
routes.Add(new RouteDescriptor(name, route));
}
}
}
+15
View File
@@ -0,0 +1,15 @@
namespace TheXamlGuy.Framework.WPF
{
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,12 @@
using System.Collections.Generic;
namespace TheXamlGuy.Framework.WPF
{
public class RouteDescriptorCollection : List<IRouteDescriptor>, IRouteDescriptorCollection
{
public RouteDescriptorCollection(IEnumerable<IRouteDescriptor> collection) : base(collection)
{
}
}
}
+124
View File
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using TheXamlGuy.Framework.Core;
namespace TheXamlGuy.Framework.WPF
{
public class Router : IRouterContext
{
private readonly IRouteDescriptorCollection descriptors;
private readonly IDisposer disposer;
private readonly IEventAggregator eventAggregator;
private readonly INamedDataTemplateFactory namedDataTemplateFactory;
private readonly INamedTemplateFactory namedTemplateFactory;
private readonly ITemplateFactory templateFactory;
private readonly ITypedDataTemplateFactory typedDataTemplateFactory;
public Router(ITemplateFactory templateFactory,
INamedTemplateFactory namedTemplateFactory,
INamedDataTemplateFactory namedDataTemplateFactory,
ITypedDataTemplateFactory typedDataTemplateFactory,
IEventAggregator eventAggregator,
IDisposer disposer,
IRouteDescriptorCollection descriptors)
{
this.templateFactory = templateFactory;
this.namedTemplateFactory = namedTemplateFactory;
this.namedDataTemplateFactory = namedDataTemplateFactory;
this.typedDataTemplateFactory = typedDataTemplateFactory;
this.eventAggregator = eventAggregator;
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? dataTemplate = 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)
{
dataTemplate = namedDataTemplateFactory.Create(name, parameters.ToArray());
template = namedTemplateFactory.Create(name);
}
if (args.Type is Type type)
{
dataTemplate = typedDataTemplateFactory.Create(type, parameters.ToArray());
template = templateFactory.Create(dataTemplate);
}
if (template is FrameworkElement content)
{
content.DataContext = dataTemplate;
if (descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is RouteDescriptor descriptor)
{
if (descriptor.Route is Frame frame)
{
frame.Navigate(content);
}
if (descriptor.Route is ContentControl contentControl)
{
contentControl.Content = content;
}
}
eventAggregator.Publish(Navigated.Create((dynamic?)content, (dynamic?)dataTemplate, keyedParameters));
}
else
{
if (descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is { Route: 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: FrameworkElement content })
{
if (content.DataContext is IDisposable disposable)
{
disposable.Dispose();
}
}
if (descriptor.Route is Frame frame)
{
frame.GoBack();
}
}
}
}
}