ICompositorDesktopInterop throws exception in TFM .NET 5
See original GitHub issueI was trying to port a C# code from .NET 4.8 to .NET 5, older version of .NET was using Microsoft.Windows.SDK.Contracts
, In .NET 5 Microsoft.Windows.SDK.Contracts
is replaced by Target Framework Monikers (TFMs) I used the same code an tried to cast the compositor to ICompositorDesktopInterop
but it throws exception :
System.InvalidCastException: 'Invalid cast from ‘Windows.UI.Composition.Compositor’ to ‘ICompositorDesktopInterop’
Here is the code:
private void InitComposition(IntPtr hwnd)
{
ICompositorDesktopInterop interop;
object iunknown = Compositor as object;
interop = (ICompositorDesktopInterop)iunknown;
IntPtr raw;
interop.CreateDesktopWindowTarget(hwnd, true, out raw);
object rawObject = Marshal.GetObjectForIUnknown(raw);
var compositionTarget = (ICompositionTarget)rawObject;
f (raw == null) { throw new Exception("QI Failed"); }
}
Since this method throws exception I tried a different method:
ICompositorDesktopInterop interop;
object iunknown = Compositor as object;
interop = iunknown.As<ICompositorDesktopInterop>();
interop.CreateDesktopWindowTarget(hwnd, true, out ICompositionTarget target);
For this there was no exception but whenever I use ICompositionTarget the Application closes without any messages.
Why is it not working with TFM ? Is there something I missed ?
I have the same issue in Cs/WinRT package also.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
ICompositorDesktopInterop throws exception in TFM .NET 5
The issue with TFM can be solved by using DirectN or by using InteropCompositor. For InteropCompositor: Set the TargetFramework in the ...
Read more >ICompositorDesktopInterop throws exception in TFM .NET 5-wpf
The issue with TFM can be solved by using DirectN or by using InteropCompositor. For InteropCompositor: Set the TargetFramework in the project file...
Read more >Casting from Compositor to ICompositorDesktopInterop ...
This is the exception thrown: System.InvalidCastException: 'Invalid cast from 'Windows.UI.Composition.Compositor' to 'Acrylic.
Read more >c#-ICompositorDesktopInterop throws exception in TFM .NET 5
c# ICompositorDesktopInterop throws exception in TFM .NET 5 I was trying to port a C# code from .NET 4.8 to .NET 5, older...
Read more >Handling and throwing exceptions in .NET
Learn how to handle and throw exceptions in .NET. Exceptions are how .NET operations indicate failure to applications.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Extrimis Thank you.
I just find a fix now, and it is working for me, I will post the code on GitHub once I clean it up