-
Notifications
You must be signed in to change notification settings - Fork 107
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
How to definition event with ComWrappers? #1789
Comments
ComWrappers doesn't support that scenario (actually, nor does |
This is correct. You need to make a WinMD with that type definition and then create projections for it. Technically speaking I suppose you could do all of this with just C# tooling: create a C# authoring project with that type definition and then create another project to project the resulting WinMD produced by the first project. Otherwise you can use IDL and C++ tooling. |
Personally, I don't think winmd and Projection.dll should be used to accomplish this if there is a way to define events in ComWrappers. Yesterday I had an in-depth conversation with https://github.com/driver1998 and https://github.com/cnbluefire Z, and the event can be managed by manually defining the add and remove methods, and using the EventRegistrationToken to manage the event. |
Personally, I don't think winmd and Projection.dll should be used to accomplish this if there is a way to define events in ComWrappers. Yesterday I had an in-depth conversation with https://github.com/driver1998 and https://github.com/cnbluefire Z, and the event can be managed by manually defining the add and remove methods, and using the EventRegistrationToken to manage the event. |
Here's sample code to define an event C++ way
Convert to C#
invoke
As you can see, I use an EventHandler without generics (corresponding to object sender and EventArgs args) and the event fires normally. Since the sender type is CoreWindow and args is object, it doesn't have much impact. However when I use TypedEventHandler with generics, an exception occurs, so I would like to ask you, is there any workaround? Test Samples |
Yes, because there is no WinRT marshaller for ComWrappers. I'm not too sure where the team stands with this, but I personally would prefer the WinRT way. What you're effectively doing is redefining the ABI, which can be quite fragile. |
@Sergio0694 it looks like we can't use CsWinRT to generate the WinMD currently, as the authoring generator currently ignores the |
.NET 6 will cease to be supported in November 2024, at which point only.NET 8 and.NET 9 will be supported. The.NET team has provided ComWrappers source generation support since.NET 8, so I'm curious to know if the CsWinRT team will make CsWinRT more compatible with ComWrappers source generation support after.NET 6 stops supporting it? For example, using StrategyBasedComWrappers provided by.NET 8+, modify and delete their own implementation in StrategyBasedComWrappers, define the interface using GeneratedComInterface, GeneratedComClass Implements the corresponding class of the interface. In.NET Core 3.1 or the.NET Framework, the interface generated by WinRT is a built-in Com type, which is then compatible with the built-in COM provided by.NET. With the release of.NET 5+ and ComWrappers in both CsWinRT and.NET teams, this very strong compatibility has been broken. .NET 6 将于 2024 年 11 月停止支持,这时受支持的 .NET 版本仅有 .NET 8 和 .NET 9。从 .NET 8 开始 .NET 团队提供了 ComWrappers 源生成的支持,所以我很想知道 CsWinRT 团队是否会在 .NET 6 停止支持后,CsWinRT 会做与 ComWrappers 源生成支持更多的兼容?比如使用 .NET 8+ 提供的 StrategyBasedComWrappers,在 StrategyBasedComWrappers 做出修改并删除自己的实现,使用 GeneratedComInterface 定义接口,GeneratedComClass 实现接口对应的类。在 .NET Core 3.1 或 .NET Framework 中,WinRT 生成的接口是内置的 Com 类型,那时与 .NET 提供的内置的 COM 保持兼容。而随着 .NET 5+ 的发布,以及 CsWinRT 和 .NET 团队两套方式的 ComWrappers 实现,这种非常强的兼容被打破了。 |
@dongle-the-gadget I definitely want to fix that. I want to make it possible to just use C# to create WinMD-s. Can you please open an issue for that if there isn't one already? That would be another step towards no longer needing any C++ projects 🙂 |
Description
How to definition event with ComWrappers?
如何使用 ComWrappers 定义事件?
As you can see in the image, I have a COM interface for IInternalCoreWindow2 (the interface is derived from the IInspectable interface) that contains some events. How do I define these events in ComWrappers? How do I call it after the definition is complete?
如图所示,我有一个 IInternalCoreWindow2 的 COM 接口(接口派生于 IInspectable 接口),其中包含了一些事件。如何在 ComWrappers 中定义这些事件?在定义完成后,如何调用呢?
https://github.com/dillydylann/WindowsXamlHosting/blob/a4c8f95759c866d2b724394b0d1bbf866b94d087/CoreWindowAPI/CoreWindow.Interfaces.cs#L23
In the .NET Framework we can define it like this (using COM Interop)
在 .NET Framework 中我们可以这样定义(使用了 COM Interop)
Reproduction Steps
None
Expected behavior
Provide an example of defining and calling the interface
提供一个定义并调用该接口的例子
Actual behavior
There are only examples of cpp native and .NET Framework COM Interop
只有 cpp native 和 .NET Framework COM Interop 的示例
Regression?
None
Known Workarounds
None
Configuration
.NET 9 With UWP
10.0.26100.0
x64
None
Other information
Test UWP App
App1.zip
dotnet/runtime#107779
The text was updated successfully, but these errors were encountered: