microsoft / CsWinRT

C# language projection for the Windows Runtime
MIT License
553 stars 106 forks source link

Bug: MarshalInspectable.CreateMarshaler exhibits different behaviors and results in UWP and WinUI 3 #1846

Open Gaoyifei1011 opened 1 week ago

Gaoyifei1011 commented 1 week ago

Description

MarshalInspectable.CreateMarshaler exhibits different behaviors and results in UWP and WinUI 3

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

MarshalInspectable.CreateMarshaler 在 UWP 和 WinUI 3 中表现出不同的行为和结果


Steps To Reproduce

  1. Create WinUI 3 and UWP blank projects, the configuration used by the project: .NET 9 rc 2, CsWinRT 2.1.6, Windows App SDK 1.6.240923002
  2. After the application is initialized, add the following code:
TypedEventHandler<CoreWindow, object> windowPositionChangedEventHandler = new(OnWindowPositionChanged);
IObjectReference objectReference = MarshalInterface<TypedEventHandler<CoreWindow,object>>.CreateMarshaler(windowPositionChangedEventHandler);
  1. An exception occurred in UWP and the WinUI 3 project executed smoothly.
------------------------------------------

1.创建 WinUI 3 和 UWP 空白项目,项目使用的配置: .NET 9 rc 2,CsWinRT 2.1.6,Windows App SDK 1.6.240923002 2.应用初始化完成后,添加如下代码

TypedEventHandler<CoreWindow, object> windowPositionChangedEventHandler = new(OnWindowPositionChanged);
IObjectReference objectReference = MarshalInterface<TypedEventHandler<CoreWindow,object>>.CreateMarshaler(windowPositionChangedEventHandler);

3.在 UWP 中发生了异常,WinUI 3 项目顺利执行。


Expected Behavior

The above types of code will hopefully be called smoothly in UWP as well

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

上述类型代码希望也可以在 UWP 中顺利调用


Version Info

CsWinRT 2.1.6 .NET 9 RC2 Windows App SDK 1.6.240923002 Visual Studio 2022 17.12.preview 4.0


Additional Context

ScreenShot(截图)

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

UWP Image

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

WinUI 3 Image

dongle-the-gadget commented 1 week ago

You're likely using WinUI 3 without enabling <PublishAot>, which causes different behavior due to IsDynamicCodeCompiled returning false, not due to UWP and WinUI 3 differences. Chances are if you enable that property on WinUI 3 as well you would also see that exception.

devsko commented 1 week ago

Add <DynamicCodeSupport>true</DynamicCodeSupport> to your UWP .csproj. Only works without AOT.

Gaoyifei1011 commented 1 week ago

You're likely using WinUI 3 without enabling <PublishAot>, which causes different behavior due to IsDynamicCodeCompiled returning false, not due to UWP and WinUI 3 differences. Chances are if you enable that property on WinUI 3 as well you would also see that exception.

Nowhere. When I turned on aot in the winui3 project, it did throw the same exceptions as the uwp project. So do you have a solution for non-dynamic code? Thank you very much

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

确实。当我在winui3项目中开启了aot后发现它确实抛出了和uwp项目一样的异常。所以想问一下您有针对非动态代码的解决方案吗?非常感谢

dongle-the-gadget commented 1 week ago

Nope, it's just unsupported.

Gaoyifei1011 commented 1 week ago

Nope, it's just unsupported.

All right. I can now do this only by temporarily replacing the generic TypedEventHandler with a non-generic EventHandler.

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

好吧。我现在只能用非泛型的 EventHandler 临时替代泛型的 TypedEventHandler 来完成这一任务了。