CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

How to CreateMarshaler with Generic types?

Open Gaoyifei1011 opened this issue 1 year ago • 2 comments

Describe the bug

How to CreateMarshaler with Generic types?

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. 如图所示,我使用不带有泛型的 EventHandler(对应 object sender 和 EventArgs args),事件正常触发。因为这个事件对应 sender 类型为 CoreWindow,args 为 object,故没有太大影响。

However when I use TypedEventHandler with generics, an exception occurs, so I would like to ask you, is there any workaround? 然而当我使用带有泛型的 TypedEventHandler 时,却发生了异常,所以想问您一下,有什么解决方法吗? image

                EventHandler eventHandler = new(OnWindowPositionChanged);
                IObjectReference objectReference = MarshalInterface<EventHandler>.CreateMarshaler(eventHandler);
                internalCoreWindow2.add_WindowPositionChanged(objectReference.ThisPtr, out EventRegistrationToken token);
                EventRegistrationToken = token;

Expected behavior

CreateMarshaller with Generic types normally

Version Info

CsWinRT 2.1.3 Windows SDK 10.0.26100.45 .NET SDK 9.0.0-preivew7

Additional context

None

Gaoyifei1011 avatar Sep 15 '24 12:09 Gaoyifei1011