AttachedCardShadow crashed on .Net 8.0
Describe the bug
We are preparing the upgrade to .Net 8.0 from our .Net 6.0 based project.
Unfortunately the AttachedCardShadow causes a crash with the following exception:
Error message: The type initializer for 'WinRT.ActivationFactory1' threw an exception.`
Stacktrace:
at WinRT.ActivationFactory1.As(Guid iid)
at Microsoft.Graphics.Canvas.Geometry.CanvasGeometry.Make___objRef_global__Microsoft_Graphics_Canvas_Geometry_ICanvasGeometryStatics()
at Microsoft.Graphics.Canvas.Geometry.CanvasGeometry.get__objRef_global__Microsoft_Graphics_Canvas_Geometry_ICanvasGeometryStatics()
at Microsoft.Graphics.Canvas.Geometry.CanvasGeometry.CreateRoundedRectangle(ICanvasResourceCreator resourceCreator, Single x, Single y, Single w, Single h, Single radiusX, Single radiusY)
at CommunityToolkit.WinUI.UI.Media.AttachedCardShadow.GetShadowClip(AttachedShadowElementContext context)
at CommunityToolkit.WinUI.UI.AttachedShadowBase.UpdateShadowClip(AttachedShadowElementContext context)
at CommunityToolkit.WinUI.UI.AttachedShadowBase.OnElementContextInitialized(AttachedShadowElementContext context)
at CommunityToolkit.WinUI.UI.AttachedShadowElementContext.Initialize(Boolean forceIfNotLoaded)
at CommunityToolkit.WinUI.UI.AttachedShadowElementContext.OnElementLoaded(Object sender, RoutedEventArgs e)
at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.<GetEventInvoke>b__1_0(Object sender, RoutedEventArgs e)
at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)
--- End of stack trace from previous location ---
at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
at ABI.Windows.ApplicationModel.Core.IUnhandledErrorMethods.Propagate(IObjectReference _obj)
at Windows.ApplicationModel.Core.UnhandledError.Propagate()
at Microsoft.AppCenter.Utils.ApplicationLifecycleHelperWinUI.<.ctor>b__0_3(Object sender, UnhandledErrorDetectedEventArgs eventArgs)
--- End of stack trace from previous location ---
at Microsoft.AppCenter.Utils.ApplicationLifecycleHelperWinUI.<.ctor>b__0_3(Object sender, UnhandledErrorDetectedEventArgs eventArgs)
at WinRT.EventSource__EventHandler1.EventState.<GetEventInvoke>b__1_0(Object obj, T e) at ABI.System.EventHandler1.Do_Abi_Invoke[TAbi](Void* thisPtr, IntPtr sender, TAbi args)`
There is an innerexception though but it says less:
Message: Class not registered (0x80040154 (REGDB_E_CLASSNOTREG))
Stacktrace:
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode) at WinRT.BaseActivationFactory..ctor(String typeNamespace, String typeFullName) at WinRT.ActivationFactory1..ctor()
at WinRT.ActivationFactory1..cctor()
A example of how we use it:
We are using version 7.1.2. from the CommunityToolkit. A quick attempt to upgrade to version 8 didn't resolve the issue either (Same exception).
We are targeting Windows 10.0.19041.0 I am running on Windows 11.
I hope someone can help.
Steps to reproduce
1. Create a WinUI project in .Net 8.0
2. Add a NavigationView to the page
3. Add an AttachedCardShadow to the Navigation view.
4. Run the program.
Expected behavior
I expect a shadow to be shown and the program to not crash.
Screenshots
No response
Code Platform
- [ ] UWP
- [X] WinAppSDK / WinUI 3
- [ ] Web Assembly (WASM)
- [ ] Android
- [ ] iOS
- [ ] MacOS
- [ ] Linux / GTK
Windows Build Number
- [ ] Windows 10 1809 (Build 17763)
- [ ] Windows 10 1903 (Build 18362)
- [ ] Windows 10 1909 (Build 18363)
- [ ] Windows 10 2004 (Build 19041)
- [ ] Windows 10 20H2 (Build 19042)
- [ ] Windows 10 21H1 (Build 19043)
- [ ] Windows 10 21H2 (Build 19044)
- [ ] Windows 10 22H2 (Build 19045)
- [X] Windows 11 21H2 (Build 22000)
- [ ] Other (specify)
Other Windows Build number
No response
App minimum and target SDK version
- [ ] Windows 10, version 1809 (Build 17763)
- [ ] Windows 10, version 1903 (Build 18362)
- [x] Windows 10, version 1909 (Build 18363)
- [ ] Windows 10, version 2004 (Build 19041)
- [ ] Windows 10, version 2104 (Build 20348)
- [ ] Windows 11, version 22H2 (Build 22000)
- [ ] Other (specify)
Other SDK version
No response
Visual Studio Version
No response
Visual Studio Build Number
No response
Device form factor
No response
Additional context
No response
Help us help you
No, I'm unable to contribute a solution.
@Arlodotexe don't know if this is also related to #238. I believe the Media package wasn't ported with the WinUI changes from the prior WinUI branch.
@Joost-Jens-Luminis @michael-hawker Hi, I'm trying to migrate my application to .net 8. And after almost a half day's exploration I found a solution:
- Change your target framework version to at least:
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>(explained below). - Explicitly reference the
Microsoft.Graphics.Win2Dpacakge<PackageReference Include="Microsoft.Graphics.Win2D" Version="1.1.0" />Since current CommunityToolkit is referencing the 1.0.54 version and 1.1.0 is the only version newer. - Start building your application and see it works.
The Microsoft.Graphics.Win2D is built againt Microsoft.Windows.NET.SDK version 10.0.22621.24 which is why we have to change the TFM to 22621, otherwise your application won't build.
Thanks @Lightczx!
For what it's worth I just had to add a direct package reference to Microsoft.Graphics.Win2D v1.0.5.1 and that solved the crash for our app (also going from net6 to net8), no TFM update necessary.
FYI @Arlodotexe looks like we should ensure the Win2D dependency is up-to-date as well.