InteropCompositor 1.0.0

dotnet add package InteropCompositor --version 1.0.0                
NuGet\Install-Package InteropCompositor -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="InteropCompositor" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add InteropCompositor --version 1.0.0                
#r "nuget: InteropCompositor, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install InteropCompositor as a Cake Addin
#addin nuget:?package=InteropCompositor&version=1.0.0

// Install InteropCompositor as a Cake Tool
#tool nuget:?package=InteropCompositor&version=1.0.0                

Interop Compositor

With the release of .NET5 Preview 8, Microsoft introduced a streamlined way for .NET developers to access the set of Windows Runtime (WinRT) APIs that are shipped as part of Windows..NET5 applications can now access Windows APIs through a new set of Target Framework Monikers (TFMs), which have been extended to support platform specific APIs.

This change means that applications targeting .NET5 no longer need to reference the Microsoft.Windows.SDK.Contracts NuGet package. But in Target Framework Moniker Using Compositor object to get ICompositorDesktopInterop throws exception

System.PlatformNotSupportedException: 'Marshalling as IInspectable is not supported in the .NET runtime.'

To overcome this issue you can use InteropCompositor.

Install

NuGet Package

Install-Package InteropCompositor

https://nuget.org/packages/InteropCompositor

Usage

Set the TargetFramework in the project file

<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>

and in your code use

Compositor compositor = new Compositor();
ICompositorDesktopInterop interop = compositor.TryAs<ICompositorDesktopInterop>();
interop.CreateDesktopWindowTarget(hwnd, true, out var target).ThrowOnError();
ICompositionTarget compositionTarget = (ICompositionTarget)target;

Note: Don't forget to create DispatcherQueueController on MainWindow Constructor.

Credits 🏅

Simon Mourier and his Repository DirectN

Product Compatible and additional computed target framework versions.
.NET net5.0-windows10.0.19041 is compatible.  net6.0-windows was computed.  net7.0-windows was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0-windows10.0.19041

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 575 8/26/2021