threading fixes
This commit is contained in:
@@ -4,6 +4,7 @@ using Hyperbar.Windows.Primary;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.UI.Dispatching;
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
namespace Hyperbar.Windows;
|
||||
@@ -17,6 +18,10 @@ public partial class App :
|
||||
{
|
||||
base.OnLaunched(args);
|
||||
|
||||
var context = new DispatcherQueueSynchronizationContext(
|
||||
DispatcherQueue.GetForCurrentThread());
|
||||
SynchronizationContext.SetSynchronizationContext(context);
|
||||
|
||||
IHost? host = Host.CreateDefaultBuilder()
|
||||
.UseContentRoot(AppContext.BaseDirectory)
|
||||
.ConfigureAppConfiguration(config =>
|
||||
@@ -31,10 +36,11 @@ public partial class App :
|
||||
services.AddSingleton<IServiceFactory>(provider =>
|
||||
new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!)));
|
||||
|
||||
services.AddSingleton<IMediator, Mediator>();
|
||||
services.AddHostedService<AppService>();
|
||||
|
||||
services.AddTransient<IInitializer, AppInitializer>();
|
||||
services.AddTransient<ITemplateFactory, TemplateFactory>();
|
||||
services.AddTransient<ITemplateGeneratorFactory, TemplateGeneratorFactory>();
|
||||
|
||||
services.AddTransient<DesktopFlyout>();
|
||||
services.AddContentTemplate<CommandViewModel, CommandView>();
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.UI.Dispatching;
|
||||
|
||||
namespace Hyperbar.Windows
|
||||
{
|
||||
@@ -11,6 +12,9 @@ namespace Hyperbar.Windows
|
||||
where TWidgetProvider :
|
||||
IWidgetProvider, new()
|
||||
{
|
||||
DispatcherQueueSynchronizationContext context = new(DispatcherQueue.GetForCurrentThread());
|
||||
SynchronizationContext.SetSynchronizationContext(context);
|
||||
|
||||
TWidgetProvider builder = new();
|
||||
IHost? host = new HostBuilder()
|
||||
.UseContentRoot(AppContext.BaseDirectory)
|
||||
@@ -37,7 +41,6 @@ namespace Hyperbar.Windows
|
||||
isolatedServices.AddContentTemplate<WidgetButtonViewModel, WidgetButtonView>();
|
||||
|
||||
isolatedServices.AddTransient<ITemplateFactory, TemplateFactory>();
|
||||
isolatedServices.AddTransient<ITemplateGeneratorFactory, TemplateGeneratorFactory>();
|
||||
|
||||
builder.Create(context, isolatedServices);
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
namespace Hyperbar.Windows;
|
||||
|
||||
public class DataTemplateConverter : ValueConverter<object, DataTemplateSelector>
|
||||
{
|
||||
protected override DataTemplateSelector? ConvertTo(object value, Type? targetType, object? parameter, string? language)
|
||||
{
|
||||
return new TemplateGenerator();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
namespace Hyperbar.Windows
|
||||
{
|
||||
public interface ITemplateGeneratorFactory
|
||||
{
|
||||
DataTemplate Create();
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,11 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
namespace Hyperbar.Windows;
|
||||
|
||||
public class TemplateFactory(ITemplateGeneratorFactory factory,
|
||||
IEnumerable<IContentTemplateDescriptor> descriptors,
|
||||
public class TemplateFactory(IEnumerable<IContentTemplateDescriptor> descriptors,
|
||||
IServiceProvider provider) :
|
||||
DataTemplateSelector,
|
||||
ITemplateFactory
|
||||
{
|
||||
protected override DataTemplate SelectTemplateCore(object item) => factory.Create();
|
||||
|
||||
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) => factory.Create();
|
||||
|
||||
public object? Create(object key)
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => x.Key == key) is IContentTemplateDescriptor descriptor)
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Markup;
|
||||
|
||||
namespace Hyperbar.Windows;
|
||||
|
||||
public class TemplateGenerator : DataTemplateSelector
|
||||
{
|
||||
protected override DataTemplate SelectTemplateCore(object item)
|
||||
{
|
||||
string xamlString = @"
|
||||
<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
|
||||
xmlns:desktop='using:Hyperbar.Windows'>
|
||||
<desktop:TemplateGeneratorControl />
|
||||
</DataTemplate>";
|
||||
|
||||
return (DataTemplate)XamlReader.Load(xamlString);
|
||||
}
|
||||
|
||||
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
|
||||
{
|
||||
string xamlString = @"
|
||||
<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
|
||||
xmlns:desktop='using:Hyperbar.Windows'>
|
||||
<desktop:TemplateGeneratorControl />
|
||||
</DataTemplate>";
|
||||
|
||||
return (DataTemplate)XamlReader.Load(xamlString);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Markup;
|
||||
|
||||
namespace Hyperbar.Windows;
|
||||
|
||||
public class TemplateGeneratorFactory :
|
||||
ITemplateGeneratorFactory
|
||||
{
|
||||
public DataTemplate Create()
|
||||
{
|
||||
string xamlString = @"
|
||||
<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
|
||||
xmlns:desktop='using:Hyperbar.Windows'>
|
||||
<desktop:TemplateGeneratorControl />
|
||||
</DataTemplate>";
|
||||
|
||||
return (DataTemplate)XamlReader.Load(xamlString);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Data;
|
||||
using Microsoft.UI.Xaml.Markup;
|
||||
|
||||
namespace Hyperbar.Windows;
|
||||
|
||||
public abstract class ValueConverter<TSource, TTarget> :
|
||||
MarkupExtension,
|
||||
IValueConverter
|
||||
{
|
||||
protected override object ProvideValue(IXamlServiceProvider serviceProvider)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public object? Convert(object value, Type targetType, object parameter, string language)
|
||||
{
|
||||
return ConvertTo((TSource)value, targetType, parameter, language);
|
||||
}
|
||||
|
||||
public object? ConvertBack(object value, Type targetType, object parameter, string language)
|
||||
{
|
||||
return ConvertBackTo((TTarget)value, targetType, parameter, language);
|
||||
}
|
||||
|
||||
public TTarget? Convert(TSource value)
|
||||
{
|
||||
return ConvertTo(value, null, null, null);
|
||||
}
|
||||
|
||||
public TSource? ConvertBack(TTarget value)
|
||||
{
|
||||
return ConvertBackTo(value, null, null, null);
|
||||
}
|
||||
|
||||
protected virtual TTarget? ConvertTo(TSource value, Type? targetType, object? parameter, string? language)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
protected virtual TSource? ConvertBackTo(TTarget value, Type? targetType, object? parameter, string? language)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,9 @@
|
||||
<UserControl
|
||||
x:Class="Hyperbar.Windows.CommandView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<ItemsControl ItemTemplateSelector="{Binding TemplateFactory}" ItemsSource="{Binding}">
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:windows="using:Hyperbar.Windows">
|
||||
<ItemsControl ItemTemplateSelector="{Binding Mode=TwoWay, Converter={windows:DataTemplateConverter}}" ItemsSource="{Binding}">
|
||||
<ItemsControl.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<StackPanel Orientation="Horizontal" />
|
||||
|
||||
@@ -6,8 +6,9 @@ public partial class CommandViewModel :
|
||||
ITemplatedViewModel
|
||||
{
|
||||
public CommandViewModel(ITemplateFactory templateFactory,
|
||||
IServiceFactory serviceFactory,
|
||||
IEnumerable<IWidgetViewModel> items) : base(serviceFactory, items)
|
||||
IServiceFactory serviceFactory,
|
||||
IMediator mediator,
|
||||
IEnumerable<IWidgetViewModel> items) : base(serviceFactory, mediator, items)
|
||||
{
|
||||
TemplateFactory = templateFactory;
|
||||
}
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
<UserControl
|
||||
x:Class="Hyperbar.Windows.WidgetView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<ItemsControl ItemTemplateSelector="{Binding TemplateFactory}" ItemsSource="{Binding}">
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:windows="using:Hyperbar.Windows">
|
||||
<ItemsControl ItemTemplateSelector="{Binding Mode=TwoWay, Converter={windows:DataTemplateConverter}}" ItemsSource="{Binding}">
|
||||
<ItemsControl.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<StackPanel Orientation="Horizontal" Spacing="8" />
|
||||
|
||||
Reference in New Issue
Block a user