Skip to content

Commit

Permalink
.NET 8 support
Browse files Browse the repository at this point in the history
  • Loading branch information
driver1998 committed Aug 11, 2024
1 parent f4dd3c0 commit 43e29d7
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 24 deletions.
15 changes: 11 additions & 4 deletions Mile.Xaml.Managed/Interop/ICoreWindowInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@

using System;
using System.Runtime.InteropServices;
#if NET8_0_OR_GREATER
using System.Runtime.InteropServices.Marshalling;
#endif

namespace Mile.Xaml.Interop
{
#if NET8_0_OR_GREATER
[GeneratedComInterface]
#else
[ComImport]
[Guid("45D64A29-A63E-4CB6-B498-5781D298CB4F")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ICoreWindowInterop
#endif
[Guid("45D64A29-A63E-4CB6-B498-5781D298CB4F")]
public partial interface ICoreWindowInterop
{
IntPtr WindowHandle { get; }
bool MessageHandled { set; }
IntPtr GetWindowHandle();
void SetMessageHandled([MarshalAs(UnmanagedType.I1)]bool messageHandled);
}
}
12 changes: 10 additions & 2 deletions Mile.Xaml.Managed/Interop/IDesktopWindowXamlSourceNative2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,26 @@

using System;
using System.Runtime.InteropServices;
#if NET8_0_OR_GREATER
using System.Runtime.InteropServices.Marshalling;
#endif

namespace Mile.Xaml.Interop
{
#if NET8_0_OR_GREATER
[GeneratedComInterface]
#else
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
#endif
[Guid("e3dcd8c7-3057-4692-99c3-7b7720afda31")]
public interface IDesktopWindowXamlSourceNative2
public partial interface IDesktopWindowXamlSourceNative2
{
void AttachToWindow(IntPtr parentWnd);

IntPtr WindowHandle { get; }
IntPtr GetWindowHandle();

[return:MarshalAs(UnmanagedType.I1)]
bool PreTranslateMessage(ref System.Windows.Forms.Message message);
}
}
11 changes: 9 additions & 2 deletions Mile.Xaml.Managed/Interop/IInitializeWithWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@

using System;
using System.Runtime.InteropServices;
#if NET8_0_OR_GREATER
using System.Runtime.InteropServices.Marshalling;
#endif

namespace Mile.Xaml.Interop
{
#if NET8_0_OR_GREATER
[GeneratedComInterface]
#else
[ComImport]
[Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInitializeWithWindow
#endif
[Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
public partial interface IInitializeWithWindow
{
void Initialize(IntPtr hwnd);
}
Expand Down
17 changes: 14 additions & 3 deletions Mile.Xaml.Managed/Interop/IWindowPrivate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,25 @@

using System;
using System.Runtime.InteropServices;
#if NET8_0_OR_GREATER
using System.Runtime.InteropServices.Marshalling;
#endif

namespace Mile.Xaml.Interop
{
#if NET8_0_OR_GREATER
[GeneratedComInterface]
#else
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
#endif
[Guid("06636C29-5A17-458D-8EA2-2422D997A922")]
[InterfaceType(ComInterfaceType.InterfaceIsIInspectable)]
public interface IWindowPrivate
public partial interface IWindowPrivate
{
bool TransparentBackground { get; set; }
void GetIids(out int iidCount, out IntPtr iids);
void GetRuntimeClassName(out IntPtr className);
void GetTrustLevel(out int trustLevel);
[return: MarshalAs(UnmanagedType.I1)] bool GetTransparentBackground();
void SetTransparentBackground([MarshalAs(UnmanagedType.I1)] bool transparentBackground);
}
}
15 changes: 15 additions & 0 deletions Mile.Xaml.Managed/Interop/InteropExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public static class InteropExtensions
public static IDesktopWindowXamlSourceNative2 GetInterop(
this DesktopWindowXamlSource BaseObject)
{
#if NET8_0_OR_GREATER
IDesktopWindowXamlSourceNative2 InteropObject = (IDesktopWindowXamlSourceNative2)(object)BaseObject;
return InteropObject;
#else
IntPtr BaseObjectIntPtr = Marshal.GetIUnknownForObject(BaseObject);
try
{
Expand All @@ -33,11 +37,16 @@ public static IDesktopWindowXamlSourceNative2 GetInterop(
{
Marshal.Release(BaseObjectIntPtr);
}
#endif
}

public static ICoreWindowInterop GetInterop(
this CoreWindow BaseObject)
{
#if NET8_0_OR_GREATER
ICoreWindowInterop InteropObject = (ICoreWindowInterop)(object)BaseObject;
return InteropObject;
#else
IntPtr BaseObjectIntPtr = Marshal.GetIUnknownForObject(BaseObject);
try
{
Expand All @@ -50,10 +59,15 @@ public static ICoreWindowInterop GetInterop(
{
Marshal.Release(BaseObjectIntPtr);
}
#endif
}

public static IWindowPrivate GetInterop(this Window BaseObject)
{
#if NET8_0_OR_GREATER
IWindowPrivate InteropObject = (IWindowPrivate)(object)BaseObject;
return InteropObject;
#else
IntPtr BaseObjectIntPtr = Marshal.GetIUnknownForObject(BaseObject);
try
{
Expand All @@ -66,6 +80,7 @@ public static IWindowPrivate GetInterop(this Window BaseObject)
{
Marshal.Release(BaseObjectIntPtr);
}
#endif
}
}
}
10 changes: 10 additions & 0 deletions Mile.Xaml.Managed/Interop/UnsafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ internal static partial class UnsafeNativeMethods
/// SecurityCritical: This code happens to return a critical resource and causes unmanaged code elevation
/// </SecurityNote>
/// <returns>handle</returns>
#if NET8_0_OR_GREATER
[LibraryImport(ExternDll.User32, SetLastError = true, EntryPoint = "SetFocus")]
internal static partial IntPtr IntSetFocus(IntPtr hWnd);
#else
[DllImport(ExternDll.User32, EntryPoint = "SetFocus", ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr IntSetFocus(IntPtr hWnd);
#endif

/// <summary>
/// Changes an attribute of the specified window. The function also sets the 32-bit (long) value at the specified offset into the extra window memory.
Expand All @@ -29,7 +34,12 @@ internal static partial class UnsafeNativeMethods
/// <param name="nIndex">Zero-based offset</param>
/// <param name="dwNewLong">The replacement value</param>
/// <returns>A positive integer indicates success; zero indicates failure</returns>
#if NET8_0_OR_GREATER
[LibraryImport(ExternDll.User32, SetLastError = true, EntryPoint = "SetWindowLongW", StringMarshalling = StringMarshalling.Utf16)]
internal static partial int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
#else
[DllImport(ExternDll.User32, SetLastError = true)]
internal static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
#endif
}
}
16 changes: 14 additions & 2 deletions Mile.Xaml.Managed/Mile.Xaml.Managed.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<TargetFrameworks>net48;net8.0-windows</TargetFrameworks>
<TargetPlatformVersion>10.0.22621.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<LangVersion>latest</LangVersion>
Expand All @@ -11,9 +11,21 @@
<Copyright>© Project Mile. All rights reserved.</Copyright>
<Version>2.2.$([System.DateTime]::Today.Subtract($([System.DateTime]::Parse('2021-09-12'))).TotalDays).0</Version>
<UseWindowsForms>true</UseWindowsForms>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
<PropertyGroup Condition="$(TargetFramework) == 'net8.0-windows'">
<TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
<IsAotCompatible>true</IsAotCompatible>
<WindowsSdkPackageVersion>$([System.Version]::Parse('$(TargetPlatformMinVersion)').ToString(3)).39</WindowsSdkPackageVersion>
</PropertyGroup>

<ItemGroup Condition="$(TargetFramework) == 'net8.0-windows'">
<AssemblyAttribute Include="System.Runtime.CompilerServices.DisableRuntimeMarshalling" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'net48'">
<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="$([System.Version]::Parse('$(TargetPlatformVersion)').ToString(3)).*" />
</ItemGroup>

Expand Down
18 changes: 11 additions & 7 deletions Mile.Xaml.Managed/WindowsXamlHostBase.WndProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ protected override void OnPaintBackground(PaintEventArgs pevent)
// Do not draw the background
}

#if NET8_0_OR_GREATER
[LibraryImport("user32.dll", EntryPoint = "SendNotifyMessageW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
internal static partial IntPtr SendNotifyMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
#else
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
internal static extern IntPtr SendNotifyMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
#endif

/// <summary>
/// Processes Windows messages for XamlContentHost control window (not XAML window)
Expand All @@ -88,7 +93,7 @@ protected override void WndProc(ref Message m)
SetDesktopWindowXamlSourceWindowPos();

IntPtr CoreWindowHandle =
CoreWindow.GetForCurrentThread().GetInterop().WindowHandle;
CoreWindow.GetForCurrentThread().GetInterop().GetWindowHandle();
Message CurrentMessage = m;

// Use Delay execution for improving the resizing for ContentDialogs.
Expand All @@ -99,14 +104,13 @@ protected override void WndProc(ref Message m)
// ContentDialogs don't resize themselves when the XAML island
// resizes. However, if we manually resize our CoreWindow, that'll
// actually trigger a resize of the ContentDialog.
SendMessage(
SendNotifyMessage(
CoreWindowHandle,
CurrentMessage.Msg,
CurrentMessage.WParam,
CurrentMessage.LParam);
});



break;

// BUGBUG: Focus integration with Windows.UI.Xaml.Hosting.XamlSourceFocusNavigation is
Expand All @@ -133,15 +137,15 @@ protected override void WndProc(ref Message m)
case NativeDefines.WM_KILLFOCUS:
// If focus is being set on the UWP XAML island window then we should prevent LostFocus by
// handling this message.
if (_xamlIslandWindowHandle == null || _xamlIslandWindowHandle != m.WParam || _xamlSource.HasFocus)
if (_xamlIslandWindowHandle != IntPtr.Zero || _xamlIslandWindowHandle != m.WParam || _xamlSource.HasFocus)
{
base.WndProc(ref m);
}

break;

case NativeDefines.WM_DPICHANGED_AFTERPARENT:
if (_xamlIslandWindowHandle != null)
if (_xamlIslandWindowHandle != IntPtr.Zero)
{
UpdateDpiScalingFactor();
PerformLayout();
Expand Down
2 changes: 1 addition & 1 deletion Mile.Xaml.Managed/WindowsXamlHostBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ protected override void OnHandleCreated(EventArgs e)
// Attach window to DesktopWindowXamSource as a render target
var XamlSourceNative = _xamlSource.GetInterop();
XamlSourceNative.AttachToWindow(Handle);
_xamlIslandWindowHandle = XamlSourceNative.WindowHandle;
_xamlIslandWindowHandle = XamlSourceNative.GetWindowHandle();

// Set window style required by container control to support Focus and background refresh
if (Interop.UnsafeNativeMethods.SetWindowLong(Handle, Interop.NativeDefines.GWL_EXSTYLE, Interop.NativeDefines.WS_EX_CONTROLPARENT | Interop.NativeDefines.WS_EX_LAYERED) == 0)
Expand Down
24 changes: 21 additions & 3 deletions Mile.Xaml.Managed/XamlApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@

namespace Mile.Xaml
{
public static class XamlApplicationExtensions
public static partial class XamlApplicationExtensions
{
private const uint PM_NOREMOVE = 0x0000;
private const uint PM_REMOVE = 0x0001;
private const uint PM_NOYIELD = 0x0002;

#if NET8_0_OR_GREATER
[LibraryImport("user32.dll", EntryPoint = "PeekMessageW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool PeekMessage(
out System.Windows.Forms.Message lpMsg,
IntPtr hWnd,
uint wMsgFilterMin,
uint wMsgFilterMax,
uint wRemoveMsg);
#else
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool PeekMessage(
Expand All @@ -30,10 +40,18 @@ private static extern bool PeekMessage(
uint wMsgFilterMin,
uint wMsgFilterMax,
uint wRemoveMsg);
#endif

#if NET8_0_OR_GREATER
[LibraryImport("user32.dll", EntryPoint = "DispatchMessageW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
private static partial IntPtr DispatchMessage(
ref System.Windows.Forms.Message lpMsg);
#else
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr DispatchMessage(
ref System.Windows.Forms.Message lpMsg);
#endif


[ThreadStatic]
private static WindowsXamlManager XamlManager = null;
Expand Down Expand Up @@ -84,14 +102,14 @@ public static void ThreadUninitialize(
public static bool GetTransparentBackgroundAttribute(
this Application BaseObject)
{
return Window.Current.GetInterop().TransparentBackground;
return Window.Current.GetInterop().GetTransparentBackground();
}

public static void SetTransparentBackgroundAttribute(
this Application BaseObject,
bool AttributeValue)
{
Window.Current.GetInterop().TransparentBackground = AttributeValue;
Window.Current.GetInterop().SetTransparentBackground(AttributeValue);
}
}
}
7 changes: 7 additions & 0 deletions Mile.Xaml.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "net48", "net48", "{14C8B3F2
Mile.Xaml\NuGet\net48\Mile.Xaml.targets = Mile.Xaml\NuGet\net48\Mile.Xaml.targets
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "net8.0", "net8.0", "{2B753308-E55A-491B-B2FE-3CC4422864D6}"
ProjectSection(SolutionItems) = preProject
Mile.Xaml\NuGet\net8.0\Mile.Xaml.props = Mile.Xaml\NuGet\net8.0\Mile.Xaml.props
Mile.Xaml\NuGet\net8.0\Mile.Xaml.targets = Mile.Xaml\NuGet\net8.0\Mile.Xaml.targets
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM64 = Debug|ARM64
Expand Down Expand Up @@ -80,6 +86,7 @@ Global
{EB76F0B3-3CE0-4517-8BF2-367C53D745AA} = {931D228C-8E9D-47E5-A535-3F356CA96E78}
{1A7C0865-004B-454E-9AB7-35D593AFAF25} = {EB76F0B3-3CE0-4517-8BF2-367C53D745AA}
{14C8B3F2-E47B-49F5-9EA3-EB207CBECFCC} = {EB76F0B3-3CE0-4517-8BF2-367C53D745AA}
{2B753308-E55A-491B-B2FE-3CC4422864D6} = {EB76F0B3-3CE0-4517-8BF2-367C53D745AA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7988428E-156C-4D84-8DEE-989B06041193}
Expand Down
8 changes: 8 additions & 0 deletions Mile.Xaml/Mile.Xaml.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<group targetFramework=".NETFramework4.8">
<dependency id="Microsoft.Windows.SDK.Contracts" version="10.0.19041.1" />
</group>
<group targetFramework="net8.0-windows10.0.19041.0">
<dependency id="DisposableMemory.ModernNetUAP.XamlCompiler" version="0.2.0" />
</group>
</dependencies>
<repository type="git" url="https://github.com/ProjectMile/Mile.Xaml.git" />
</metadata>
Expand All @@ -38,6 +41,8 @@
<file src="NuGet\net48\Mile.Xaml.AfterImport.targets" target="build\net48" />
<file src="NuGet\net48\Mile.Xaml.props" target="build\net48" />
<file src="NuGet\net48\Mile.Xaml.targets" target="build\net48" />
<file src="NuGet\net8.0\Mile.Xaml.targets" target="build\net8.0-windows10.0.19041.0" />
<file src="NuGet\net8.0\Mile.Xaml.props" target="build\net8.0-windows10.0.19041.0" />
<file src="NuGet\MrtCore.PriGen.targets" target="build" />

<file src="..\Output\Binaries\Release\x64\Mile.Xaml.Styles.SunValley.xbf" target="Redist\Styles" />
Expand All @@ -49,6 +54,9 @@
<file src="..\Output\Binaries\Release\AnyCPU\net48\Mile.Xaml.Managed.dll" target="lib\net48" />
<file src="..\Output\Binaries\Release\AnyCPU\net48\Mile.Xaml.Managed.pdb" target="lib\net48" />

<file src="..\Output\Binaries\Release\AnyCPU\net8.0-windows\Mile.Xaml.Managed.dll" target="lib\net8.0-windows10.0.19041.0" />
<file src="..\Output\Binaries\Release\AnyCPU\net8.0-windows\Mile.Xaml.Managed.pdb" target="lib\net8.0-windows10.0.19041.0" />

<file src="..\ReadMe.md" target="." />
<file src="..\License.md" target="." />
<file src="..\ReleaseNotes.md" target="." />
Expand Down
Loading

0 comments on commit 43e29d7

Please sign in to comment.