Add some WinUI work
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Markup;
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Toolkit.WinUI;
|
||||
|
||||
public class ContentTemplate :
|
||||
DataTemplateSelector,
|
||||
IContentTemplate
|
||||
{
|
||||
protected override DataTemplate? SelectTemplateCore(object item)
|
||||
{
|
||||
if (item is IObservableViewModel observableViewModel)
|
||||
{
|
||||
if (observableViewModel.Provider is IServiceProvider provider)
|
||||
{
|
||||
Type itemType = item.GetType();
|
||||
if (provider.GetRequiredKeyedService<IContentTemplateDescriptor>(itemType.Name.Replace("ViewModel", ""))
|
||||
is IContentTemplateDescriptor descriptor)
|
||||
{
|
||||
return CreateDataTemplate(descriptor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
protected override DataTemplate? SelectTemplateCore(object item,
|
||||
DependencyObject container) => SelectTemplateCore(item);
|
||||
|
||||
private static DataTemplate CreateDataTemplate(IContentTemplateDescriptor descriptor)
|
||||
{
|
||||
string xamlString = @$"
|
||||
<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
|
||||
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
|
||||
xmlns:local=""using:Toolkit.WinUI"">
|
||||
<local:TemplateControl />
|
||||
</DataTemplate>";
|
||||
|
||||
return (DataTemplate)XamlReader.Load(xamlString);
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ public static class IServiceCollectionExtensions
|
||||
services.AddTransient<IDispatcherTimerFactory, DispatcherTimerFactory>();
|
||||
services.AddSingleton<IWindowRegistry, WindowRegistry>();
|
||||
|
||||
services.AddTransient<IContentTemplate, ContentTemplate>();
|
||||
|
||||
services.AddTransient((Func<IServiceProvider, IProxyServiceCollection<IComponentBuilder>>)(provider =>
|
||||
new ProxyServiceCollection<IComponentBuilder>(services =>
|
||||
{
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Data;
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Toolkit.WinUI;
|
||||
|
||||
[Bindable]
|
||||
public class TemplateControl :
|
||||
ContentControl
|
||||
{
|
||||
public TemplateControl()
|
||||
{
|
||||
DefaultStyleKey = typeof(TemplateControl);
|
||||
Loaded += OnLoaded;
|
||||
}
|
||||
|
||||
private void OnLoaded(object sender,
|
||||
RoutedEventArgs args)
|
||||
{
|
||||
Loaded -= OnLoaded;
|
||||
|
||||
if (DataContext is IObservableViewModel observableViewModel)
|
||||
{
|
||||
if (observableViewModel.Provider is IServiceProvider provider)
|
||||
{
|
||||
if (provider.GetRequiredKeyedService<IContentTemplateDescriptor>(DataContext.GetType().Name.Replace("ViewModel", ""))
|
||||
is IContentTemplateDescriptor descriptor)
|
||||
{
|
||||
if (provider.GetRequiredKeyedService(descriptor.TemplateType, descriptor.Key)
|
||||
is FrameworkElement control)
|
||||
{
|
||||
void HandleLoaded(object? sender, RoutedEventArgs args)
|
||||
{
|
||||
control.Loaded -= HandleLoaded;
|
||||
if (control.DataContext is object content)
|
||||
{
|
||||
if (content is IActivation activation)
|
||||
{
|
||||
activation.IsActive = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HandleDataContextChanged(FrameworkElement? sender, DataContextChangedEventArgs args)
|
||||
{
|
||||
if (control.DataContext is object content)
|
||||
{
|
||||
if (content is IActivation activation)
|
||||
{
|
||||
activation.IsActive = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HandleUnloaded(object? sender, RoutedEventArgs args)
|
||||
{
|
||||
control.Unloaded -= HandleUnloaded;
|
||||
if (control.DataContext is object content)
|
||||
{
|
||||
if (content is IActivation activation)
|
||||
{
|
||||
activation.IsActive = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
control.Loaded += HandleLoaded;
|
||||
control.Unloaded += HandleUnloaded;
|
||||
control.DataContextChanged += HandleDataContextChanged;
|
||||
|
||||
Content = control;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,14 +15,13 @@
|
||||
<UseWinUI>true</UseWinUI>
|
||||
<WindowsSdkPackageVersion>10.0.19041.41</WindowsSdkPackageVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
|
||||
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240923002" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Toolkit.Foundation\Toolkit.Foundation.csproj" />
|
||||
<ProjectReference Include="..\Toolkit.Windows\Toolkit.Windows.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user