microsoft-ui-xaml: AppWindow.GetFromWindowId crashes in WinUI3 desktop application in unpackaged mode.

Describe the bug

  • NuGet package version: Windows App SDK 1.0.0 (stable)
  • VisualStudio 2022. 17.0.1
  • x64

Steps to reproduce the bug

  1. Create “Blank App, Packaged (WinUI 3 in Desktop)” project.
  2. Delete “Package.appxmanifest”.
  3. Add <WindowsPackageType>None</WindowsPackageType> to the property group in the project.
  4. Add this code.
public MainWindow()
{
    this.InitializeComponent();

    IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
    WindowId myWndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
    AppWindow aw = AppWindow.GetFromWindowId(myWndId);
}
  1. Run the application.
Exception thrown: 'System.Reflection.TargetInvocationException' in System.Private.CoreLib.dll
Exception has been thrown by the target of an invocation.

stack trace.

   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& hasNoDefaultCtor)
   at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
   at System.Activator.CreateInstance[T]()
   at WinRT.WeakLazy`1.get_Value()
   at Microsoft.UI.Windowing.AppWindow._IAppWindowStatics.get_Instance()
   at Microsoft.UI.Windowing.AppWindow.GetFromWindowId(WindowId windowId)
   at ModuleHub.MainWindow..ctor() in C:\\k4\\ModuleHub\\ModuleHub\\MainWindow.xaml.cs:line 42"	string

Inner exception.

SEHException: External component has thrown an exception.

stack trace.

   at WinRT.Platform.RoGetActivationFactory(IntPtr runtimeClassId, Guid& iid, IntPtr* factory)
   at WinRT.WinrtModule.GetActivationFactory(IntPtr hstrRuntimeClassId)
   at WinRT.WinrtModule.GetActivationFactory(String runtimeClassId)
   at WinRT.BaseActivationFactory..ctor(String typeNamespace, String typeFullName)
   at Microsoft.UI.Windowing.AppWindow._IAppWindowStatics..ctor()"	string

Expected behavior

No response

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 0.8: 0.8.0 (If you’re seeing your issue in older previews of WinUI 3, please try this release)

Windows app type

  • UWP
  • Win32

Device form factor

Desktop

Windows version

May 2020 Update (19041)

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 16 (2 by maintainers)

Most upvoted comments

There should not be any hacks needed to make a winui3 app run on an already supported platform. Sadly this 1.0 version feels rushed, nowhere near production viable.

I had signaled it in the WindowsAppSDK forum, but after some tests, I found that the bug is fixed by adding

 <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> 
        <application>
			<!-- Windows 10 -->
			<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>           
        </application> 
    </compatibility>

in the app.manifest