Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Gaoyifei1011 opened this issue Oct 25, 2024 · 5 comments
Labels

Comments

@Gaoyifei1011
Copy link
Contributor

Gaoyifei1011 commented Oct 25, 2024

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
Copy link
Contributor

dongle-the-gadget commented Oct 25, 2024

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
Copy link

devsko commented Oct 25, 2024

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

@Gaoyifei1011
Copy link
Contributor Author

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
Copy link
Contributor

Nope, it's just unsupported.

@manodasanW manodasanW added the AOT label Oct 25, 2024
@Gaoyifei1011
Copy link
Contributor Author

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 来完成这一任务了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants