wpf webbrowser为vb.net中单击的链接添加处理程序 [英] wpf webbrowser add handler for clicked links in vb.net

查看:72
本文介绍了wpf webbrowser为vb.net中单击的链接添加处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VB.NET表单应用程序,它使用webcontrol来显示带有音乐专辑封面的本地html页面。所有封面都包含指向本地文本文件的链接。我为linkcollection添加了一个处理程序,这样当单击一个封面时,子程序会读取文本文件并将其内容添加到音乐播放器中。这完美无缺。



但是,我现在正在将此应用程序转换为WPF,并且我一直试图在WPF webcontrol中添加处理程序几天,但只是不要让它工作。



谷歌搜索只给我C ++示例,我只能部分转换为VB.NET。





到目前为止我的代码:





I have a VB.NET forms application that uses the webcontrol to show a local html page with music album covers. All covers contain a link to a local text file. I added a handler to the linkcollection, so that when clicking on a cover, a subroutine reads the text file and adds its content to a music player. This works flawlessly.

However, I am now converting this application to WPF, and I have been trying to add a handler to the WPF webcontrol for days now, but just don't get it to work.

Googling only gives me C++ examples that I am only able to convert partially to VB.NET.


The code I have so far:


Private Sub WhatsNewBrowser_LoadCompleted(sender As Object, e As NavigationEventArgs) Handles WhatsNewBrowser.LoadCompleted
        Dim doc As mshtml.HTMLDocumentClass = WhatsNewBrowser.Document 

        For Each link As mshtml.IHTMLElement In doc.links
            Dim anchor As mshtml.HTMLAnchorElement = link
            If Not anchor Is Nothing Then
                Dim handler As mshtml.HTMLAnchorEvents_Event = anchor
                If Not handler Is Nothing Then
                    ''what code goes here to run a sub onclick? The sub     
                    ''obviously needs to know what link is clicked.
                End If
            End If
        Next

    End Sub





此代码可以不工作,我不知道要改变什么。

我做错了什么,如何创建处理程序来运行sub。



提前致谢!



This code does not work and I have no idea what to change.
What am I doing wrong and how do I create the handler to run the sub.

Thanks in advance!

推荐答案

处理程序创建如下:
AddHandler eventargs, AddressOf subroutine

。即AddHandler TextBox1.KeyDown,AddressOf MySub



当然,在这个例子中你需要有一个名为MySub的子程序,然后从这里添加你想要做的代码将该事件用于该子例程(MySub)。请参阅添加处理程序



如何将值传递给子例程将演示如何从链接声明中将值发送到其他子例程在你的For Each循环中。



你的For循环属于导航EventArgs 子程序。



从那里,您可以检查导航到的URL。 (显然,如果单击链接,则只会导航,因此这是正确使用的子类)。



因为WebBrowser控件在导航到新页面时会引发导航事件,并且当导航事件开始导航到新页面时会触发导航事件。



使用这些事件,您可以检查循环中单击的链接,以及您导航到的位置。但是,通过使用导航和导航事件args,您不需要循环。



希望这有帮助。

. I.e. AddHandler TextBox1.KeyDown, AddressOf MySub

Of course, you need to have a subroutine called MySub for this example, and from here you add the code you want to do the work for that event to that subroutine (MySub). See Adding Handlers.

How to Pass Values to Subroutines will demonstrate how you can send values to other subroutines from your Link declaration in your For Each Loop.

Your For Loop belongs in the Navigating EventArgs subroutine.

From there, you can check the URL that's been navigated to. (Obviously you would only be navigating if you click a link, so this is the correct sub to use).

Because the WebBrowser control raises the Navigating event when it's about to navigate to a new page and it will fire the Navigated event when it has started navigating to a new page.

Using these events will allow you to check the link that was clicked in your loop, against where you are navigating to. However, by using the navigating and navigated event args, you don't need a loop.

Hope this helps.


这篇关于wpf webbrowser为vb.net中单击的链接添加处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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