Wrap FluentAvalonia controls within same named classes allowing us to declare the xmlns namespace in our assembly

This commit is contained in:
Daniel Clark
2022-12-10 15:52:58 +00:00
parent 3d9a7e4438
commit 4f243eba2e
94 changed files with 121 additions and 85 deletions
@@ -0,0 +1,24 @@
using Avalonia.Data.Converters;
using System.Globalization;
namespace Toolkit.Foundation.Avalonia
{
public static class EventArgsExtensions
{
public static dynamic? GetEventArguments(this EventArgs args, string? path, IValueConverter? converter, object? converterParameter)
{
return !string.IsNullOrWhiteSpace(path) ? GetEventArgsPropertyPathValue(args, path) : converter is not null ? converter.Convert(args, typeof(object), converterParameter, CultureInfo.CurrentCulture) : (dynamic)args;
}
private static object GetEventArgsPropertyPathValue(object args, string path)
{
object? value = args;
if (path is { })
{
value = PropertyPathHelper.GetValue(args, path);
}
return value;
}
}
}
@@ -0,0 +1,35 @@
using Mediator;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
namespace Toolkit.Foundation.Avalonia
{
public static class IHostBuilderExtensions
{
public static IHostBuilder ConfigureTemplates(this IHostBuilder hostBuilder, Action<ITemplateBuilder> builderDelegate)
{
hostBuilder.ConfigureServices((hostBuilderContext, serviceCollection) =>
{
TemplateBuilder? builder = new();
builderDelegate?.Invoke(builder);
serviceCollection.TryAddSingleton(builder.Descriptors);
serviceCollection.TryAddSingleton<ITemplateDescriptorProvider, TemplateDescriptorProvider>();
serviceCollection.TryAddSingleton<ITemplateFactory, TemplateFactory>();
serviceCollection.TryAddSingleton<INamedTemplateFactory, NamedTemplateFactory>();
serviceCollection.TryAddSingleton<ITypedDataTemplateFactory, TypedDataTemplateFactory>();
serviceCollection.TryAddSingleton<INamedDataTemplateFactory, NamedDataTemplateFactory>();
serviceCollection.TryAddSingleton<ITemplateSelector, TemplateSelector>();
foreach (ITemplateDescriptor? descriptor in builder.Descriptors)
{
serviceCollection.Add(new ServiceDescriptor(descriptor.TemplateType, descriptor.TemplateType, descriptor.Lifetime));
serviceCollection.Add(new ServiceDescriptor(descriptor.ContentType, descriptor.ContentType, descriptor.Lifetime));
}
});
return hostBuilder;
}
}
}
@@ -0,0 +1,21 @@
using Avalonia.Controls;
using FluentAvalonia.UI.Controls;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Toolkit.Foundation.Avalonia
{
public static class IServiceCollectionExtensions
{
public static IServiceCollection AddNavigation(this IServiceCollection serviceCollection)
{
serviceCollection.TryAddSingleton<INavigationRouteDescriptorCollection, NavigationRouteDescriptorCollection>();
serviceCollection.TryAddTransient<Navigation<Frame>, FrameNavigation>();
serviceCollection.TryAddTransient<Navigation<ContentDialog>, ContentDialogNavigation>();
serviceCollection.TryAddTransient<Navigation<ContentControl>, ContentControlNavigation>();
return serviceCollection;
}
}
}
@@ -0,0 +1,17 @@
using Avalonia.Data;
namespace Toolkit.Foundation.Avalonia
{
public static class MarkupExtensions
{
public static Binding? ToBinding(this object value)
{
if (value is Binding)
{
return value as Binding;
}
return new Binding { Mode = BindingMode.OneWay, Source = value };
}
}
}
@@ -0,0 +1,28 @@
using Avalonia;
using Avalonia.Data;
namespace Toolkit.Foundation.Avalonia
{
public static class PropertyPathHelper
{
private static readonly Dummy dummy = new();
public static object GetValue(object args, string path)
{
Binding binding = new(path)
{
Mode = BindingMode.OneTime,
Source = args
};
dummy.Bind(Dummy.ValueProperty, binding);
return dummy.GetValue(Dummy.ValueProperty);
}
private class Dummy : AvaloniaObject
{
public static readonly StyledProperty<object> ValueProperty =
AvaloniaProperty.Register<Dummy, object>("Value");
}
}
}