博文

【UWP】让 UWP 自己托管自己 —— Windows SDK 篇

图片
众所周知,UWP 使用的窗口模型是 CoreWindow,但是 UWP 本身只是一个应用模型,所以完全可以创建 win32 窗口,那么我们可以不可以创建一个 win32 窗口,然后像 XAML 岛 (XAML Islands) 一样把 XAML 托管上去呢?本篇将讲述如何在 UWP 创建一个 XAML 岛窗口。 首先,XAML 岛会判断当前的应用模型是否为 ClassicDesktop ,所以我们需要利用 Detours 劫持 AppPolicyGetWindowingModel 方法。具体内容如下: #r "nuget:Detours.Win32Metadata" #r "nuget:Microsoft.Windows.CsWin32" using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.Storage.Packaging.Appx; using Detours = Microsoft.Detours.PInvoke; /// <summary> /// Represents a hook for the <see cref="PInvoke.AppPolicyGetWindowingModel(HANDLE, AppPolicyWindowingModel*)"/> function. /// </summary> public sealed partial class HookWindowingModel : IDisposable { /// <summary> /// The value that indicates whether the class has been disposed. /// </summary> private bo...

【UWP】让 UWP 自己托管自己 —— Windows App SDK 篇

图片
众所周知,UWP 使用的窗口模型是 CoreWindow,但是 UWP 本身只是一个应用模型,所以完全可以创建 win32 窗口,那么我们可以不可以创建一个 win32 窗口,然后像 XAML 岛 (XAML Islands) 一样把 XAML 托管上去呢?本篇将讲述如何利用 WAS (Windows App SDK,俗称 WinUI3) 在 UWP 创建一个 XAML 岛窗口。 演示视频: https://x.com/wherewhere7/status/1721570411388039587 由于 WAS 在 win32 应用模型下本身就是个 XAML 岛,所以 WAS 对 XAML 岛的支持要比 WUXC (Windows.UI.Xaml.Controls) 要好多了,接下来的内容大多是将 WAS 中实现窗口的方法迁移到 C#。 首先,不管是 WUXC 还是 WAS 的 XAML 岛都会判断当前的应用模型是否为 ClassicDesktop ,所以我们需要利用Detours劫持 AppPolicyGetWindowingModel 方法。具体内容如下: #r "nuget:Detours.Win32Metadata" #r "nuget:Microsoft.Windows.CsWin32" using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.Storage.Packaging.Appx; using Detours = Microsoft.Detours.PInvoke; /// <summary> /// Represents a hook for the <see cref="PInvoke.AppPolicyGetWindowingModel(HANDLE, AppPolicyWindowingModel*)"/> functi...

【UWP】在 UWP 中使用 Windows App SDK

众所周知,WAS (Windows App SDK,俗称 WinUI3)在刚开始是支持 UWP 的,甚至最早 只支持 UWP ,但是微软在正式版发布前删除了对 UWP 的支持,不过真的删除了吗? 初生之鸟 在 2023年10月发现 在 VS 调试下无视报错继续运行可以正常在 UWP 加载 WAS。随着 WAS 的开源,WAS 阻止在 UWP 上运行的原因也被找到,至此大家终于找到在 UWP 上使用 WAS 的方法了。 WAS 阻止在 UWP 上运行的方法很简单,就是检查注册表 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WinUI\Xaml\EnableUWPWindow 是否为 00000001 ,如果不是就直接报错。 Window_Partial.cpp#L80-L114 // ---------------------------------------------------------------------- // IWindow // ---------------------------------------------------------------------- Window:: Window () { // The first window created internally by DXamlCore _must_ be a UWP Window. DXamlCore // requires and controls the lifetime of a hidden UWP Microsoft.UI.Xaml.Window. // note that this Window instance will be the 'real' window for UWP instances, but // serves as a dummy for all other instances. dummy behavior is deprecated and being removed. auto dxamlCore = DXamlCore:: GetCurrent (); ...