CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

Bug: Can't directly convert WinRT.IInspectable to Windows.ApplicationModel.LaunchActivatedEventArgs

Open Gaoyifei1011 opened this issue 10 months ago • 2 comments

Description

Bug: Can't directly convert WinRT.IInspectable to Windows.ApplicationModel.LaunchActivatedEventArgs

---------------------------

不能直接将 WinRT.IInspectable 转换为 Windows.ApplicationModel.LaunchActivatedEventArgs

Steps To Reproduce

  1. Create a new desktop bridge project, and add a desktop application.
  2. Define the app execution alias AppExceutionAlias in the app manifest package.appxmanifest
  3. Try to start the app in the console, the Windows.ApplicationModel.AppInstance.GetActivatedEventArgs method directly returns null
  4. When you call the corresponding Microsoft.Windows.AppLifecycle.AppInstance.GetActivatedEventArgs method of the Windows App SDK, it is directly returned in the Windows.ApplicationModel.AppInstance.GetActivatedEventArgs method null is further wrapped to force the creation of a new LaunchActivatedEventArgs class, and the GetCommandLine function is added to retrieve the command-line string of the current process, and the final return is ExtendedActivationKind.Launch.
  5. In the desktop bridge app, when you try to call the Windows App SDK Microsoft.Windows.AppLifecycle.AppInstance.GetActivatedEventArgs method, an AppActivationArguments object is returned with a Data property of an object, When debugging, it is displayed as WinRT.IInspectable. You can call GetRunTimeClassName to get a type of Windows.ApplicationModel.ILaunchActivatedEventArgs.
  6. At this time, if the as extension method of winrt is called, the InvaildCastException exception will occur, and the .NET as keyword will return null
  7. If you call LaunchActivatedEventArgs.FromAbi((activationArguments.Data as IInspectable).ThisPtr) method will return LaunchActivatedEventArgs
---------------------------

1.创建新桌面桥项目,并添加桌面应用程序。 2.在应用清单 Package.appxmanifest 中定义应用执行别名 AppExceutionAlias 3.在控制台尝试启动应用,Windows.ApplicationModel.AppInstance.GetActivatedEventArgs 方法直接返回 null 4.调用 Windows App SDK 相应的 Microsoft.Windows.AppLifecycle.AppInstance.GetActivatedEventArgs 方法时,会在 Windows.ApplicationModel.AppInstance.GetActivatedEventArgs 方法直接返回 null 的基础上做进一步包装,直接强制创建一个新的 LaunchActivatedEventArgs 类,并添加 GetCommandLine 函数检索当前进程的命令行字符串,最终返回的 ActivationKindExtendedActivationKind.Launch。 5.在桌面桥应用中,尝试调用 Windows App SDK Microsoft.Windows.AppLifecycle.AppInstance.GetActivatedEventArgs 方法时,会返回 AppActivationArguments 对象,Data 属性为 object,调试时显示成 WinRT.IInspectable。可以调用 GetRunTimeClassName 获取的类型为 Windows.ApplicationModel.ILaunchActivatedEventArgs。 6.这时如果调用winrt的as扩展方法,会发生 InvaildCastException 异常,.NET as 关键字返回 null 7.如果调用 LaunchActivatedEventArgs.FromAbi((activationArguments.Data as IInspectable).ThisPtr) 方法就能正常返回 LaunchActivatedEventArgs

Image Image Image Image https://github.com/microsoft/WindowsAppSDK/blob/main/dev/AppLifecycle/AppInstance.cpp#L544

Expected Behavior

In the case of Windows.ApplicationModel.AppInstance.GetActivatedEventArgs returns null, the Windows App SDK's Microsoft.Windows.AppLifecycle.AppInstance.GetActivatedEventArgs returns The Data property of the AppActivationArguments object (shown as an object class in .NET) is no longer of type IInspectable, but Windows.ApplicationModel.AppInstance.LaunchActivatedEventArgs

Windows.ApplicationModel.AppInstance.GetActivatedEventArgs 返回 null 的情况下,Windows App SDK 的 Microsoft.Windows.AppLifecycle.AppInstance.GetActivatedEventArgs 返回的 AppActivationArguments 对象的 Data 属性(.NET 中显示为 object 类),不再是 IInspectable 类型,而是 Windows.ApplicationModel.AppInstance.LaunchActivatedEventArgs

Version Info

Windows App SDK 1.6.241114003 CsWinRT 2.2.0 .NET 9

Additional Context

No response

Gaoyifei1011 avatar Jan 07 '25 08:01 Gaoyifei1011