CsWinRT
CsWinRT copied to clipboard
Bug: Can't directly convert WinRT.IInspectable to Windows.ApplicationModel.LaunchActivatedEventArgs
Description
Bug: Can't directly convert WinRT.IInspectable to Windows.ApplicationModel.LaunchActivatedEventArgs
---------------------------
不能直接将 WinRT.IInspectable 转换为 Windows.ApplicationModel.LaunchActivatedEventArgs
Steps To Reproduce
- Create a new desktop bridge project, and add a desktop application.
- Define the app execution alias
AppExceutionAliasin the app manifestpackage.appxmanifest - Try to start the app in the console, the
Windows.ApplicationModel.AppInstance.GetActivatedEventArgsmethod directly returnsnull - When you call the corresponding
Microsoft.Windows.AppLifecycle.AppInstance.GetActivatedEventArgsmethod of the Windows App SDK, it is directly returned in theWindows.ApplicationModel.AppInstance.GetActivatedEventArgsmethod null is further wrapped to force the creation of a newLaunchActivatedEventArgsclass, and theGetCommandLinefunction is added to retrieve the command-line string of the current process, and the final return isExtendedActivationKind.Launch. - In the desktop bridge app, when you try to call the
Windows App SDK Microsoft.Windows.AppLifecycle.AppInstance.GetActivatedEventArgsmethod, anAppActivationArgumentsobject is returned with aDataproperty of an object, When debugging, it is displayed asWinRT.IInspectable. You can callGetRunTimeClassNameto get a type ofWindows.ApplicationModel.ILaunchActivatedEventArgs. - At this time, if the as extension method of winrt is called, the
InvaildCastExceptionexception will occur, and the .NETaskeyword will returnnull - If you call
LaunchActivatedEventArgs.FromAbi((activationArguments.Data as IInspectable).ThisPtr)method will returnLaunchActivatedEventArgs
---------------------------
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 函数检索当前进程的命令行字符串,最终返回的 ActivationKind 为 ExtendedActivationKind.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
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