【发布时间】:2018-08-04 22:15:30
【问题描述】:
我正在开发一个几乎完成的 Web App 应用程序,该应用程序有一个本地 WebApp 有一些链接,我想在外部浏览器(Edge、Chrome 等)中打开它们。
我的代码分为三部分:
1) Windows 运行时组件:
using System;
using System.Collections.Generic;
using Windows.Foundation.Metadata;
using Windows.Storage;
using Windows.System;
namespace CallJSInterface
{
[AllowForWeb]
public sealed class CallJSCSharp
{
public async void OpenURL(string url)
{
await Launcher.LaunchUriAsync(new Uri(url));
}
}
}
2) WebView 代码:
XAML:
<WebView x:Name="webView1" Source="ms-appx-web:///Assets/index.html" NavigationStarting="webView1_NavigationStarting" />
C#:
private void webView1_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
sender.AddWebAllowedObject("CallJSCSharp", pObject: new CallJSCSharp());
}
3) JavaScript (jQuery):
$("a").click(function (event) {
event.preventDefault();
CallJSCSharp.OpenURL($(this).attr('href'));
});
但是什么也没有发生,没有错误,没有消息,什么都没有。有谁知道我应该改变什么?感谢您的帮助。
我对两个项目使用相同的配置。
【问题讨论】:
标签: c# webview hyperlink uwp windows-10-universal