gpt4 book ai didi

javascript - “system.Action”不是有效的 Windows 运行时参数类型

转载 作者:行者123 更新时间:2023-11-30 19:28:50 25 4
gpt4 key购买 nike

在 uwp c# Windows 运行时组件项目中,我想公开一个 webview js 可以通过一些回调调用它的 api。所以我正在尝试:

// c#
[Windows.Foundation.Metadata.AllowForWeb]
public sealed class xx{
public void setOnChange(string name,System.Action action) {
xxx
}
}
// js
windows.xx.setOnChange("js",function(){xxx});

然后我得到“system.Action”不是有效的 Windows 运行时参数类型错误。

那我换个方式试试:

//c#
public delegate void Action();
[Windows.Foundation.Metadata.AllowForWeb]
public sealed class xx{
public void setOnChange(string name,Action action) {
xxx
}
}
// js
windows.xx.setOnChange("js",function(){xxx});

c#可以编译,调用setOnChange,js不调用。

最佳答案

终于找到解决办法了:

//c#
[Windows.Foundation.Metadata.AllowForWeb]
public sealed class xx{
public void setOnChange(string name,EventHanlder<Object> action) {
xxx
}
}
// js
windows.xx.setOnChange("js",function(){xxx});

我猜 EventHanlder 是 Windows 运行时组件中的一种特殊类型。

Ps: 我看过https://learn.microsoft.com/en-us/windows/uwp/winrt-components/raising-events-in-windows-runtime-components的文档.然后我发现它可以通过带有事件处理程序的类包装来完成。它在 js 和 c# 中都有很多代码,然后我尝试将事件处理程序嵌入到参数中,然后它就可以工作了。 uwp 的隐藏/未记录功能?

关于javascript - “system.Action”不是有效的 Windows 运行时参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56647216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com