dka*_*zon 10 c# windows-runtime winrt-xaml windows-8.1
我正在使用Windows 8.1 xaml应用程序中的WebView,并需要处理导航到自定义协议,即."应用:// 12345".
我让WebView导航到一个网站进行身份验证,然后重定向到此自定义协议作为响应.
没有任何WebView导航事件被触发,Windows正在选择它并尝试用它打开一个应用程序("在Store中查找应用程序"对话框).
是否有可能在WebView导航到此协议时捕获?
我遇到了类似的问题,我通过将这段代码注入 HTML 来解决它。或者您可以直接在 WebView 上运行此代码。
for (var i = 0; i < document.links.length; i++) {
if(document.links[i].href.indexOf('app') === 0){
var currentHref = document.links[i].href;
document.links[i].setAttribute('href', 'javascript:window.external.notify(\'' + currentHref + '\')');
document.links[i].removeAttribute('target');
}
}
Run Code Online (Sandbox Code Playgroud)
之后,您可以在 C# 代码中捕获 window.external.notify 并执行您想要的操作。
private void WebView_OnScriptNotify(object sender, NotifyEventArgs e)
{
if (e.Value.StartsWith("app"))
{
DoAction(e.Value);
return;
}
}
Run Code Online (Sandbox Code Playgroud)