[UWP] [VB]单击WebView超链接 - 可以Target = _WebView2吗? [英] [UWP][VB]WebView Hyperlink Clicked - Can Target=_WebView2?

查看:180
本文介绍了[UWP] [VB]单击WebView超链接 - 可以Target = _WebView2吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS2015 UWP中,使用两个WebView控件,如何在主WebView控件中单击其超链接的网页加载辅助WebView控件?类似于"TARGET = _"。标签?


解决方案

Hello David VB2005,


这里有没有内置的属性或方法来做到这一点。您可以尝试以下方式:


首先,您需要注册
ScriptNotify
 (请阅读本文档的说明)事件。然后你需要使用javascript函数"window.external.notify"来通知你的应用程序。您需要为当前网页
页面上的每个"< a />"html标记添加"onclick"事件。请参阅以下代码:

 

< WebView x:Name =" wv"源= QUOT; HTTPS://msdn.microsoft.com/en-us/default.aspx" NavigationCompleted = QUOT; wv_NavigationCompleted" ScriptNotify = QUOT; wv_ScriptNotify">< / web视图>


<预类= "prettyprint郎-VB">私人异步子wv_NavigationCompleted(发件人为web视图,ARGS作为WebViewNavigationCompletedEventArgs)
尺寸脚本作为字符串=" VAR超链接= document.getElementsByTagName( 'A');对于(VAR I = 0; I< hyperlinks.length;我++){超链接[I] .onclick =函数(){window.external.notify('LaunchLink:'+ this.href); return false;}}"
Dim parameters As String()= {script}
Dim ret As String = Await wv.InvokeScriptAsync(" eval",parameters)
End Sub

其次,您需要显示新视图。新视图中有一个Webview控件。由于我们需要知道新的url,我们可以在App.cs中定义一个全局变量,然后新的Webview可以在打开新视图时导航到该URL。

私人异步子wv_ScriptNotify(发送者为对象,例如作为NotifyEventArgs)
。如果e.Value.Contains(QUOT; LaunchLink:")然后
App.url =新的URI(例如Value.Substring(QUOT; LaunchLink:"。长度),UriKind.Absolute)

尺寸NewView的作为CoreApplicationView = CoreApplication.CreateNewView()
尺寸newViewId为整数= 0
等待newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,Function()
Dim frame As New Frame()
frame.Navigate(GetType(SecondaryView),Nothing)
Wi ndow.Current.Content = frame
Window.Current.Activate()

newViewId = ApplicationView.GetForCurrentView()。Id
End Function)
Dim viewShown As Boolean = Await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId)
End if
End Sub


< Page 
x:Class =" ; AppWebView.SecondaryView"
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local =" using:AppWebView"
xmlns:d =" http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable =" d">

< Grid Background =" {ThemeResource ApplicationPageBackgroundThemeBrush}">
< WebView x:Name =" webview">< / WebView>
< / Grid>
< / Page>


 NotInheritable Class App 
继承应用
公共共享网址为Uri

......


受保护的覆盖Sub OnNavigatedTo(e As NavigationEventArgs)
MyBase。 OnNavigatedTo(e)
如果App.url IsNot Nothing然后
wv.Navigate(App.url)
结束如果
结束子

您还可以考虑使用Pivot控件来制作类似的TabControl。每次单击链接时,都可以动态创建选项卡以显示该页面。


您可以参考此主题获取详细信息:  在UWP应用功能选项卡控制;&NBSP ;


另一种方法是使用WebView的NavigationStarting事件,您可以将它与preious url进行比较,并设置args.Cancel = True以防止导航。然后打开在新视图此链接。


<预类= "prettyprint郎-VB">私人小组wv_NavigationStarting(发件人为web视图,ARGS作为WebViewNavigationStartingEventArgs)
。如果(args.Uri IsNot运算previousUrl )然后
'然后使用此args.Uri在次级视图中打开
args.Cancel = True
结束如果
结束子

最诚挚的问候,


Xavier Eoro



In VS2015 UWP, using two WebView controls, how do I load a secondary WebView control with the webpage whose hyperlink was clicked on in the primary WebView control? Similar to the "TARGET=_" tag?

解决方案

Hello David VB2005,

There’s no built-in property or method to do it. You could try the following ways:

First, you need to register ScriptNotify (please read the note on this documentation) event for WebView. Then you need to use javascript function "window.external.notify" to notify your app. You would need to add "onclick" event for every "<a/>" html tag on current web page. Please see the following code:

<WebView x:Name="wv" Source="https://msdn.microsoft.com/en-us/default.aspx" NavigationCompleted="wv_NavigationCompleted" ScriptNotify="wv_ScriptNotify"></WebView>

Private Async Sub wv_NavigationCompleted(sender As WebView, args As WebViewNavigationCompletedEventArgs)
        Dim script As String = "var hyperlinks = document.getElementsByTagName('a');for(var i = 0; i < hyperlinks.length; i++){hyperlinks[i].onclick = function(){window.external.notify('LaunchLink:'+ this.href);return false;}}"
        Dim parameters As String() = {script}
        Dim ret As String = Await wv.InvokeScriptAsync("eval", parameters)
    End Sub

Second, you need to show a new view. There’s a Webview control in the new view. Since we need to know the new url, we can define a global variable in App.cs, then the new Webview can navigate to that url when the new view is opened.

Private Async Sub wv_ScriptNotify(sender As Object, e As NotifyEventArgs)
        If e.Value.Contains("LaunchLink:") Then
            App.url = New Uri(e.Value.Substring("LaunchLink:".Length), UriKind.Absolute)

            Dim newView As CoreApplicationView = CoreApplication.CreateNewView()
            Dim newViewId As Integer = 0
            Await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, Function()
                                                                                 Dim frame As New Frame()
                                                                                 frame.Navigate(GetType(SecondaryView), Nothing)
                                                                                 Window.Current.Content = frame
                                                                                 Window.Current.Activate()

                                                                                 newViewId = ApplicationView.GetForCurrentView().Id
                                                                             End Function)
            Dim viewShown As Boolean = Await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId)
        End If
    End Sub

<Page
    x:Class="AppWebView.SecondaryView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AppWebView"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <WebView x:Name="webview"></WebView>
    </Grid>
</Page>

NotInheritable Class App
    Inherits Application
    Public Shared url As Uri

......

Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
        MyBase.OnNavigatedTo(e)
        If App.url IsNot Nothing Then
            wv.Navigate(App.url)
        End If
    End Sub

You could also think about using Pivot control to make a similar TabControl. Every time you click link, you could dynamically create tab to show that page.

You could refer to this thread for details: Tab control in UWP application  

Another way is using NavigationStarting event of WebView, you could compare it with preious url and set args.Cancel = True to prevent navigating. Then open this link in new view.

Private Sub wv_NavigationStarting(sender As WebView, args As WebViewNavigationStartingEventArgs)
        If (args.Uri IsNot previousUrl) Then
            'then using this args.Uri to open in secondary view
            args.Cancel = True
        End If
    End Sub

Best Regards,

Xavier Eoro


这篇关于[UWP] [VB]单击WebView超链接 - 可以Target = _WebView2吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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