为什么我不能在WebView(UWP)的外部浏览器中打开链接? [英] Why cannot I open links in an external browser in a WebView (UWP)?

查看:915
本文介绍了为什么我不能在WebView(UWP)的外部浏览器中打开链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个即将完成的Web App应用程序,该应用程序有一个本地WebApp,它有一些链接,我想在外部浏览器(Edge,Chrome等)中打开它们。



我的代码分为3部分:



1)Windows运行时组件:

 使用System; 
使用System.Collections.Generic;
使用Windows.Foundation.Metadata;
使用Windows.Storage;
使用Windows.System;

命名空间CallJSInterface
{
[AllowForWeb]
公共密封类CallJSCSharp
{
public async void OpenURL(string url)
{
等待Launcher.LaunchUriAsync(new Uri(url));
}
}
}

2)WebView代码:



XAML:

 < WebView x:Name =webView1Source =ms-appx-web:///Assets/index.htmlNavigationStarting =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'));
});

但没有任何反应,没有错误,没有消息,没有。有谁知道我应该改变什么?感谢您的帮助。



其他信息:



<我正在为两个项目使用相同的配置。

解决方案

有两个问题会导致代码无效。



首先 - Windows运行时组件自动修改命名约定以匹配执行它的目标平台。因为按惯例的JavaScript方法名称以小写字母开头,所以必须像这样调用 OpenURL 方法:

  CallJSCSharp.openURL($(本).attr( 'HREF')); 

现在你应该能够在Windows运行时组件中设置一个断点并看到实际调用该方法。但还有另一个问题 - Launcher 需要在UI线程上调用 OpenURL 方法才能正常工作。要绕过这个,你需要使用 Dispatcher

  [AllowForWeb] 
公共密封类CallJSCSharp
{
public async void OpenURL(string url)
{
await CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async()=> {
await Launcher.LaunchUriAsync(new Uri(url));
});
}
}

有了这个改变,代码应该像以下一样工作打算。



最后 - 谢谢你写一个完美的问题:-)。


I'm developing a Web App application that is almost finished, the app has a local WebApp that has some links and I'd like to open them in an external browser (Edge, Chrome, etc.).

My code is split in 3 parts:

1) Windows Runtime Component:

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 code:

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'));
});

But nothing happens, no error, no message, nothing. Does anyone know what should I change? Thanks for your help.

Additional Information:

I'm using the same configuration for both projects.

解决方案

There are two issues that cause the code not to work.

First - the Windows Runtime Component automatically modifies the naming conventions to match the target platform on which it is executed. Because JavaScript method names by convention start with a lowercase letter, you must call the OpenURL method like this:

CallJSCSharp.openURL($(this).attr('href'));

Now you should be able to set a breakpoint in the Windows Runtime Component and see the method is actually called. But there is another catch - the OpenURL method is not called on the UI thread which is required for Launcher to work properly. To circumvent this you need to use Dispatcher:

[AllowForWeb]
public sealed class CallJSCSharp
{
    public async void OpenURL(string url)
    {
        await CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            async () => { 
                 await Launcher.LaunchUriAsync(new Uri(url)); 
            });
    }
}

With this change in place, the code should work as intended.

Finally - thank you for a perfectly written question :-) .

这篇关于为什么我不能在WebView(UWP)的外部浏览器中打开链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆