get plugin system working

This commit is contained in:
TheXamlGuy
2024-01-04 20:53:49 +00:00
parent da8af59391
commit d45076f2a9
24 changed files with 219 additions and 55 deletions
+30 -9
View File
@@ -1,8 +1,10 @@
using Hyperbar.Desktop.Controls;
using Hyperbar.Desktop.Contextual;
using Hyperbar.Desktop.Controls;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;
using System;
using System.Collections.Generic;
namespace Hyperbar.Desktop;
@@ -17,18 +19,37 @@ public partial class App :
IHost? host = Host.CreateDefaultBuilder()
.UseContentRoot(AppContext.BaseDirectory)
.ConfigureServices(args =>
.ConfigureServices(services =>
{
args.AddTransient<IInitializer, AppInitializer>();
args.AddTransient<DesktopFlyout>();
services.AddHostedService<AppService>();
args.AddTransient<ITemplateFactory, TemplateFactory>();
args.AddTransient<ITemplateGeneratorFactory, TemplateGeneratorFactory>();
services.AddTransient<IInitializer, AppInitializer>();
services.AddTransient<DesktopFlyout>();
args.AddDataTemplate<CommandViewModel, CommandView>("Commands");
args.AddDataTemplate<ContextualCommandViewModel, ContextualCommandView>();
services.AddTransient<ITemplateFactory, TemplateFactory>();
services.AddTransient<ITemplateGeneratorFactory, TemplateGeneratorFactory>();
args.AddHostedService<AppService>();
services.AddDataTemplate<CommandViewModel, CommandView>("Commands");
// Commands
services.AddSomething<ContextualCommandBuilder>();
services.AddTransient(provider =>
{
IEnumerable<ICommandViewModel> Resolve(IServiceProvider services)
{
foreach (ICommandContext commandContext in services.GetServices<ICommandContext>())
{
if (commandContext.ServiceProvider.GetService<ICommandViewModel>() is
ICommandViewModel commandViewModel)
{
yield return commandViewModel;
}
}
}
return Resolve(provider);
});
})
.Build();
+1
View File
@@ -41,6 +41,7 @@
</Page>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Hyperbar.Desktop.Contextual\Hyperbar.Desktop.Contextual.csproj" />
<ProjectReference Include="..\Hyperbar.Desktop.Controls\Hyperbar.Desktop.Controls.csproj" />
<ProjectReference Include="..\Hyperbar\Hyperbar.csproj" />
</ItemGroup>
@@ -0,0 +1,26 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Hyperbar.Desktop
{
public static class IServiceCollectionExtensions
{
public static IServiceCollection AddSomething<TCommandBuilder>(this IServiceCollection services)
where TCommandBuilder :
ICommandBuilder, new()
{
TCommandBuilder builder = new();
IHost? host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddTransient<ITemplateFactory, TemplateFactory>();
services.AddTransient<ITemplateGeneratorFactory, TemplateGeneratorFactory>();
builder.Create(services);
}).Build();
services.AddTransient<ICommandContext>(provider => new CommandContext(host.Services));
return services;
}
}
}
@@ -2,6 +2,7 @@
namespace Hyperbar.Desktop
{
public interface ITemplateGeneratorFactory
{
DataTemplate Create();
@@ -9,10 +9,10 @@ public class TemplateGeneratorFactory :
public DataTemplate Create()
{
string xamlString = @"
<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:desktop='using:Hyperbar.Desktop'>
<desktop:TemplateGeneratorControl />
</DataTemplate>";
<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:desktop='using:Hyperbar.Desktop'>
<desktop:TemplateGeneratorControl />
</DataTemplate>";
return (DataTemplate)XamlReader.Load(xamlString);
}
+10 -9
View File
@@ -1,20 +1,21 @@
namespace Hyperbar.Desktop;
using System.Collections;
using System.Collections.Generic;
namespace Hyperbar.Desktop;
public partial class CommandViewModel :
ObservableCollectionViewModel,
ITemplatedViewModel
{
public CommandViewModel(ITemplateFactory templateFactory)
public CommandViewModel(ITemplateFactory templateFactory,
IEnumerable<ICommandViewModel> commands)
{
TemplateFactory = templateFactory;
this.Add(new ContextualCommandViewModel(templateFactory));
this.Add(new ContextualCommandViewModel(templateFactory));
this.Add(new ContextualCommandViewModel(templateFactory));
this.Add(new ContextualCommandViewModel(templateFactory));
this.Add(new ContextualCommandViewModel(templateFactory));
var d = Items;
foreach (var command in commands)
{
this.Add(command);
}
}
public ITemplateFactory TemplateFactory { get; }
@@ -1,15 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<Page
x:Class="Hyperbar.Desktop.ContextualCommandView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Hyperbar.Desktop"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Grid>
<Button />
</Grid>
</Page>
@@ -1,8 +0,0 @@
using Microsoft.UI.Xaml.Controls;
namespace Hyperbar.Desktop;
public sealed partial class ContextualCommandView : Page
{
public ContextualCommandView() => InitializeComponent();
}
@@ -1,12 +0,0 @@
namespace Hyperbar.Desktop;
public class ContextualCommandViewModel :
ITemplatedViewModel
{
public ContextualCommandViewModel(ITemplateFactory templateFactory)
{
TemplateFactory = templateFactory;
}
public ITemplateFactory TemplateFactory { get; }
}