简体   繁体   English

VB.NET HTML超链接

[英]VB.NET HTML Hyperlinks

I'm currently attempting to use a .NET WebBrowser to display help information (stored locally in html files) for an application I'm writing, however I'm experiencing two problems, both related to hyperlinks. 我目前正在尝试使用.NET WebBrowser显示我正在编写的应用程序的帮助信息(本地存储在html文件中),但是我遇到两个问题,两个都与超链接有关。

Firstly, I have a search function which returns the correct URL in the format file:\\\\\\C:\\... I can copy and paste it into a browser, and it will navigate there. 首先,我有一个搜索功能,可以以格式file:\\\\\\C:\\...返回正确的URL file:\\\\\\C:\\...我可以将其复制并粘贴到浏览器中,然后它将导航至该浏览器。 Yet clicking on the link within the control itself does nothing at all. 但是,单击控件本身内的链接根本没有任何作用。

Secondly, the HTML files all contain relative paths to other HTML files. 其次,HTML文件都包含指向其他HTML文件的相对路径。 These of course do not work as I just end up with file:\\\\\\C:\\help.html which gives me a 'webpage is unavailable.' 这些当然不起作用,因为我最终得到的是file:\\\\\\C:\\help.html ,这给了我一个“网页不可用”的信息。 But I can't think of a way to get it to work without parsing the HTML file and concatenating the path to the directory on to the front of the link. 但是我想不出一种方法来使其不解析HTML文件并将目录路径串联到链接最前面。

Edit: Just to clarify, in the first problem I am dynamically building a search result page as the user types. 编辑:只是为了澄清,在第一个问题中,我随着用户的输入动态地构建搜索结果页面。 The HTML contains several results similar to this (yes, it's unfinished, I'm just showing you the link part): HTML包含与此类似的几个结果(是的,它还没有完成,我只是向您展示链接部分):

<a style='font-family:verdana;color:#0645AD;font-size:20px;text-decoration:underline' href='C:\\Users\\User\\Documents\\project\\bin\\Debug\\..\\..\\Help\\FAQ.html'>FAQ</a><br />...This is the <b>FA</b>Q File.

Now, when I click on that link in the control nothing happens, it doesn't give me a 'webpage is unavailable' or take me to the actual page. 现在,当我单击控件中的链接时,没有任何反应,它并没有给我“网页不可用”或带我到实际页面。 Saving the HTML, however, and opening it with Chrome, IE and Firefox works fine. 但是,保存HTML并在Chrome,IE和Firefox中打开它可以正常工作。

In the second problem, I have a different help file for different sections, each containing relative links to several others. 在第二个问题中,我为不同部分提供了一个不同的帮助文件,每个文件都包含指向其他几个文件的相对链接。 VB picks these up as direct paths, and attempts to go there from root, ie file:\\\\C:\\file.html. VB选择这些作为直接路径,并尝试从根目录开始,即file:\\\\ C:\\ file.html。 The only solution I can think of is to parse the file and use WebBrowser.Navigate(Path.Combine(pathToDirectory, nameOfHelpFile.html) , which seems a lot more inefficient than it should be. 我能想到的唯一解决方案是解析文件并使用WebBrowser.Navigate(Path.Combine(pathToDirectory, nameOfHelpFile.html) ,这似乎比应有的效率低得多。

Thanks 谢谢

I think you're going to need to show us some code to see exactly what your problem is. 我认为您将需要向我们展示一些代码以准确了解您的问题所在。 I worked up a quick example of using the WebBrowser control with one HTML file that links to the other and its working just as expected. 我编写了一个简单的示例,将WebBrowser控件与一个链接到另一个HTML文件的HTML文件结合使用,并且按预期工作。

Relative links are relative to the current document being browsed. 相对链接是相对于正在浏览的当前文档的。 If you're writing raw HTML to the browser then I think that links a relative to what it thinks is root which might be file:///c:/ but I'm not sure. 如果您正在向浏览器编写原始HTML,那么我认为该链接指向的是它认为是root的相对file:///c:/ ,可能是file:///c:/但我不确定。 Also, you might be running into a permission problem if you're files are actually living in the drive's root. 另外,如果文件实际上位于驱动器的根目录中,则可能会遇到权限问题。

Here's the sample that's working fine for me: 这是适合我的示例:

Option Strict On
Option Explicit On

Imports System.IO

Public Class Form1

    Private WithEvents WebB As WebBrowser

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''//Create our web browser
        Me.WebB = New WebBrowser()
        ''//Set it to fill the form
        Me.WebB.Dock = DockStyle.Fill
        ''//Add it to the form
        Me.Controls.Add(Me.WebB)

        ''//We will put our HTML files in this folder which is on the desktop
        Dim WorkingFolder = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "HTMLTest")
        ''//Create it if it doesn't exist
        If Not Directory.Exists(WorkingFolder) Then Directory.CreateDirectory(WorkingFolder)

        ''//The names of the two files that we are creating
        Dim FirstFile = "Start.html"
        Dim SecondFile = "End.html"

        ''//Write HTML in the first file that has a link to the second file
        My.Computer.FileSystem.WriteAllText(Path.Combine(WorkingFolder, FirstFile), <html><head><title>Start</title></head><body><a href=<%= SecondFile %>>Link to second file</a></body></html>.ToString(), False)
        ''//Write HTML in the second file
        My.Computer.FileSystem.WriteAllText(Path.Combine(WorkingFolder, SecondFile), <html><head><title>End</title></head><body>This is the second file</body></html>.ToString(), False)

        ''//Tell the web browser to navigate to the second file
        Me.WebB.Navigate(Path.Combine(WorkingFolder, FirstFile))
    End Sub
End Class

The fix involved adding a bit of code to the 'Navigating' event of the WebBrowser control. 该修复程序涉及向WebBrowser控件的“ Navigating”事件中添加一些代码。

  Private Sub HelpBrowser_Navigating(sender As System.Object, e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles HelpBrowser.Navigating
    If e.Url.Scheme = "about" And e.Url.AbsolutePath <> "blank" Then 
        ' The clicked URL is of the format about:<file>.
        HelpBrowser.Navigate(HelpRootPath + "\" + e.Url.AbsolutePath)
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM