dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.24k stars 1.76k forks source link

.NET MAUI Throws Exception w/ Lib C#/WinRT #4039

Closed jingkecn closed 1 year ago

jingkecn commented 2 years ago

Description

TL;DR Project to reproduce: https://github.com/jingkecn/Microsoft.WinRT.Starter/tree/app/main-maui A .NET 6 console app Main.Console works fine w/ the C#/WinRT lib Lib.CsWinRT. However, when comes to the .NET 6 MAUI app Main.MAUI, it throws an exception:

System.Reflection.TargetInvocationException
  HResult=0x80131604
  Message=Exception has been thrown by the target of an invocation.
  Source=System.Private.CoreLib
  StackTrace:
   at System.RuntimeType.CreateInstanceOfT()
   at System.Activator.CreateInstance[T]()
   at WinRT.WeakLazy`1.get_Value()
   at WinRT.ActivationFactory`1.ActivateInstance[I]()
   at Lib.CppWinRT.MyStruct..ctor()
   at Main.MAUI.MainPage.OnCounterClicked(Object sender, EventArgs e) in C:\Users\...\source\repos\microsoft\winrt\starter\src\Main.MAUI\MainPage.xaml.cs:line 16
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.Controls.Internals.IButtonElement.PropagateUpClicked()
   at Microsoft.Maui.Controls.ButtonElement.ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager)
   at Microsoft.Maui.Controls.Button.SendClicked()
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.IButton.Clicked()
   at Microsoft.Maui.Handlers.ButtonHandler.OnClick(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.<>c__DisplayClass10_0.<Do_Abi_Invoke>b__0(RoutedEventHandler invoke)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
COMException: Class not registered (0x80040154 (REGDB_E_CLASSNOTREG))

PS: I follow the walkthrough guide of C#/WinRT: https://docs.microsoft.com/en-us/windows/uwp/csharp-winrt/net-projection-from-cppwinrt-component PPS: Remember to build at first the project Lib.CsWinRT to generate the NuGet package.

Any ideas?

Steps to Reproduce

  1. Clone my sample project: https://github.com/jingkecn/Microsoft.WinRT.Starter/tree/app/main-maui
  2. Launch Main.MAUI (on Windows Machine) and click the button

PS:

Version with bug

Preview 10 (current)

Last version that worked well

Unknown/Other

Affected platforms

Windows, I was not able test on other platforms

Affected platform versions

Windows SDK 10.0.19041

Did you find any workaround?

Nope

Relevant log output

System.Reflection.TargetInvocationException
  HResult=0x80131604
  Message=Exception has been thrown by the target of an invocation.
  Source=System.Private.CoreLib
  StackTrace:
   at System.RuntimeType.CreateInstanceOfT()
   at System.Activator.CreateInstance[T]()
   at WinRT.WeakLazy`1.get_Value()
   at WinRT.ActivationFactory`1.ActivateInstance[I]()
   at Lib.CppWinRT.MyStruct..ctor()
   at Main.MAUI.MainPage.OnCounterClicked(Object sender, EventArgs e) in C:\Users\...\source\repos\microsoft\winrt\starter\src\Main.MAUI\MainPage.xaml.cs:line 16
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.Controls.Internals.IButtonElement.PropagateUpClicked()
   at Microsoft.Maui.Controls.ButtonElement.ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager)
   at Microsoft.Maui.Controls.Button.SendClicked()
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.IButton.Clicked()
   at Microsoft.Maui.Handlers.ButtonHandler.OnClick(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.<>c__DisplayClass10_0.<Do_Abi_Invoke>b__0(RoutedEventHandler invoke)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
COMException: Class not registered (0x80040154 (REGDB_E_CLASSNOTREG))
XamlTest commented 2 years ago

Verified Repro with Visual Studio Enterprise 2022 main 17.2.0 Preview 2.0 [32219.17.main]. Repro Project: https://github.com/jingkecn/Microsoft.WinRT.Starter.git

ghost commented 2 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

jfversluis commented 1 year ago

Hey @jingkecn it has been a while since you reported this. Is this still an issue with all the latest versions of .NET MAUI and Visual Studio? Thanks!

ghost commented 1 year ago

Hi @jingkecn. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost commented 1 year ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

jingkecn commented 1 year ago

Hello @jfversluis, sorry for late reply. I just tried the latest versions of VS + MAUI Workload, the issue seems to be fixed. You can refer to the repo I mentioned before, I just updated it: https://github.com/jingkecn/Microsoft.WinRT.Starter/tree/app/main-maui