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

Copper Contributor

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?

1 Reply