为什么 UWP WebView AddWebAllowedObject 不起作用?
Why doesn't UWP WebView AddWebAllowedObject work?
我正在尝试使用 WebView 的 AddWebAllowedObject 方法,但在运行时,调用使用它的函数时返回错误。难道我做错了什么?
提前致谢。
注意:拨号class包含在运行时项目中。
[AllowForWeb]
public sealed class Dial
{
public void Greet()
{
Debug.WriteLine("Hello!");
}
}
public sealed partial class MainPage : Page
{
public MainPage()
{
InitializeComponent();
}
private void WebView_Loaded(object sender, RoutedEventArgs e)
{
wv.NavigateToString("" +
"<html>" +
"<head>" +
"<script>function hi() { dial.Greet(); }</script>" +
"</head>" +
"<body>" +
"</body>" +
"</html>"
);
}
private void WebView_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
wv.AddWebAllowedObject("dial", new Dial());
}
private async void WebView_DOMContentLoaded(WebView sender, WebViewNavigationStartingEventArgs args)
{
await wv.InvokeScriptAsync("hi", new string[] {}); // Error 0x80020101
}
}
通过测试,您需要在 html 内容中以小写形式调用 Dial class 的 Greet() 方法,如 document.
private void WebView_Loaded(object sender, RoutedEventArgs e)
{
wv.NavigateToString("" +
"<html>" +
"<head>" +
"<script>function hi() { dial.greet(); }</script>" +
"</head>" +
"<body>" +
"</body>" +
"</html>"
);
}
我正在尝试使用 WebView 的 AddWebAllowedObject 方法,但在运行时,调用使用它的函数时返回错误。难道我做错了什么? 提前致谢。
注意:拨号class包含在运行时项目中。
[AllowForWeb]
public sealed class Dial
{
public void Greet()
{
Debug.WriteLine("Hello!");
}
}
public sealed partial class MainPage : Page
{
public MainPage()
{
InitializeComponent();
}
private void WebView_Loaded(object sender, RoutedEventArgs e)
{
wv.NavigateToString("" +
"<html>" +
"<head>" +
"<script>function hi() { dial.Greet(); }</script>" +
"</head>" +
"<body>" +
"</body>" +
"</html>"
);
}
private void WebView_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
wv.AddWebAllowedObject("dial", new Dial());
}
private async void WebView_DOMContentLoaded(WebView sender, WebViewNavigationStartingEventArgs args)
{
await wv.InvokeScriptAsync("hi", new string[] {}); // Error 0x80020101
}
}
通过测试,您需要在 html 内容中以小写形式调用 Dial class 的 Greet() 方法,如 document.
private void WebView_Loaded(object sender, RoutedEventArgs e)
{
wv.NavigateToString("" +
"<html>" +
"<head>" +
"<script>function hi() { dial.greet(); }</script>" +
"</head>" +
"<body>" +
"</body>" +
"</html>"
);
}