restructure project for part 2

This commit is contained in:
TheXamlGuy
2024-01-27 10:55:53 +00:00
parent a322893166
commit 48925b89ff
96 changed files with 383 additions and 351 deletions
@@ -0,0 +1,15 @@
using Microsoft.UI.Xaml.Controls;
namespace Hyperbar.UI.Windows;
public class DataTemplateConverter :
ValueConverter<object, DataTemplateSelector>
{
protected override DataTemplateSelector? ConvertTo(object value,
Type? targetType,
object? parameter,
string? language)
{
return new TemplateGenerator();
}
}
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<UseWinUI>true</UseWinUI>
<UseRidGraph>true</UseRidGraph>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableMsixTooling>true</EnableMsixTooling>
</PropertyGroup>
<ItemGroup>
<None Remove="bin/Debug/net8.0-windows10.0.19041.0/Hyperbar.Windows.UI.pri" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.231202003-experimental1" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26031-preview" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Hyperbar.Interop.Windows\Hyperbar.Interop.Windows.csproj" />
<ProjectReference Include="..\Hyperbar\Hyperbar.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,24 @@
using System.Runtime.CompilerServices;
using WinRT;
namespace Hyperbar.UI.Windows;
public static class IWinRTObjectExtensions
{
public static void InitializeComponent<TComponent>(this TComponent component,
ref bool loaded,
[CallerFilePath] string path = "")
where TComponent :
IWinRTObject
{
if (loaded)
{
return;
}
loaded = true;
//Uri resourceLocator = ApplicationExtensionHost.Current.LocateResource(component, callerFilePath);
//Application.LoadComponent(component, resourceLocator, ComponentResourceLocation.Nested);
}
}
+21
View File
@@ -0,0 +1,21 @@
using Microsoft.Extensions.DependencyInjection;
namespace Hyperbar.UI.Windows;
public class TemplateFactory(IEnumerable<IContentTemplateDescriptor> descriptors,
IServiceProvider provider) :
ITemplateFactory
{
public object? Create(object key)
{
if (descriptors.FirstOrDefault(x => x.Key == key) is IContentTemplateDescriptor descriptor)
{
if (provider.GetRequiredKeyedService(descriptor.TemplateType, descriptor.Key) is { } template)
{
return template;
}
}
return default;
}
}
+30
View File
@@ -0,0 +1,30 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Markup;
namespace Hyperbar.UI.Windows;
public class TemplateGenerator : DataTemplateSelector
{
protected override DataTemplate SelectTemplateCore(object item)
{
string xamlString = @"
<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:ui='using:Hyperbar.Windows.UI'>
<ui: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:ui='using:Hyperbar.Windows.UI'>
<ui:TemplateGeneratorControl />
</DataTemplate>";
return (DataTemplate)XamlReader.Load(xamlString);
}
}
@@ -0,0 +1,25 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace Hyperbar.UI.Windows;
public class TemplateGeneratorControl :
ContentControl
{
public TemplateGeneratorControl()
{
DataContextChanged += OnDataContextChanged;
}
private void OnDataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
if (DataContext is ITemplatedViewModel templatedViewModel)
{
Content = templatedViewModel.TemplateFactory.Create(DataContext.GetType().Name);
}
else
{
}
}
}
+45
View File
@@ -0,0 +1,45 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Markup;
namespace Hyperbar.UI.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;
}
}
+48
View File
@@ -0,0 +1,48 @@
using Hyperbar.Interop.Windows;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Windows.Graphics;
using WinRT.Interop;
namespace Hyperbar.UI.Windows;
public static class WindowExtensions
{
public static WindowMessageListener CreateMessageListener(this Window window) =>
WindowMessageListener.Create(window.GetHandle());
public static IntPtr GetHandle(this Window window) =>
window is not null ? WindowNative.GetWindowHandle(window) : default;
public static void MoveAndResize(this Window window,
double x,
double y,
double width,
double height)
{
float num = HwndExtensions.GetDpiForWindow(window.GetHandle()) / 96f;
window.AppWindow.MoveAndResize(new RectInt32((int)x, (int)y, (int)(width * (double)num), (int)(height * (double)num)));
}
public static void SetIsAvailableInSwitchers(this Window window,
bool value) => window.AppWindow.IsShownInSwitchers = value;
public static void SetOpacity(this Window window,
byte value) => window.GetHandle().SetWindowOpacity(value);
public static void SetStyle(this Window window,
WindowStyle style) => window.GetHandle().SetWindowStyle(style);
public static void SetStyle(this Window window,
ExtendedWindowStyle style) => window.GetHandle().SetExtendedWindowStyle(style);
public static void SetTopMost(this Window window,
bool value)
{
AppWindow appWindow = window.AppWindow;
if (appWindow.Presenter is OverlappedPresenter presenter)
{
presenter.IsAlwaysOnTop = value;
}
}
}