WebBrowser.Url 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置当前文档的 URL。
public:
property Uri ^ Url { Uri ^ get(); void set(Uri ^ value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.WebBrowserUriTypeConverter))]
public Uri Url { get; set; }
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.WebBrowserUriTypeConverter))]
public Uri? Url { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.WebBrowserUriTypeConverter))>]
member this.Url : Uri with get, set
Public Property Url As Uri
属性值
一个 Uri 表示当前文档的 URL。
- 属性
例外
此 WebBrowser 实例不再有效。
无法从基础 ActiveX WebBrowser 控件检索对接口实现的IWebBrowser2引用。
设置此属性时指定的值不是绝对 URI。 有关详细信息,请参阅 IsAbsoluteUri。
示例
下面的代码示例演示如何使用 Url 属性实现控件的 WebBrowser 地址栏。 此示例要求窗体包含一个名为WebBrowser、一TextBoxAddresswebBrowser1个名为TextBox控件和一个名为Button控件的ButtonGo控件。 在文本框中键入 URL 并按 Enter 或单击 Go 按钮时,控件 WebBrowser 将导航到指定的 URL。 单击超链接导航时,文本框会自动更新以显示当前 URL。
// Navigates to the URL in the address text box when
// the ENTER key is pressed while the text box has focus.
void TextBoxAddress_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e )
{
if ( e->KeyCode == System::Windows::Forms::Keys::Enter && !this->TextBoxAddress->Text->Equals( "" ) )
{
this->WebBrowser1->Navigate( this->TextBoxAddress->Text );
}
}
// Navigates to the URL in the address text box when
// the Go button is clicked.
void ButtonGo_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
if ( !this->TextBoxAddress->Text->Equals( "" ) )
{
this->WebBrowser1->Navigate( this->TextBoxAddress->Text );
}
}
// Updates the URL in TextBoxAddress upon navigation.
void WebBrowser1_Navigated( Object^ /*sender*/, System::Windows::Forms::WebBrowserNavigatedEventArgs^ /*e*/ )
{
this->TextBoxAddress->Text = this->WebBrowser1->Url->ToString();
}
// Navigates to the URL in the address box when
// the ENTER key is pressed while the ToolStripTextBox has focus.
private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Navigate(toolStripTextBox1.Text);
}
}
// Navigates to the URL in the address box when
// the Go button is clicked.
private void goButton_Click(object sender, EventArgs e)
{
Navigate(toolStripTextBox1.Text);
}
// Navigates to the given URL if it is valid.
private void Navigate(String address)
{
if (String.IsNullOrEmpty(address)) return;
if (address.Equals("about:blank")) return;
if (!address.StartsWith("http://") &&
!address.StartsWith("https://"))
{
address = "http://" + address;
}
try
{
webBrowser1.Navigate(new Uri(address));
}
catch (System.UriFormatException)
{
return;
}
}
// Updates the URL in TextBoxAddress upon navigation.
private void webBrowser1_Navigated(object sender,
WebBrowserNavigatedEventArgs e)
{
toolStripTextBox1.Text = webBrowser1.Url.ToString();
}
' Navigates to the URL in the address box when
' the ENTER key is pressed while the ToolStripTextBox has focus.
Private Sub toolStripTextBox1_KeyDown( _
ByVal sender As Object, ByVal e As KeyEventArgs) _
Handles toolStripTextBox1.KeyDown
If (e.KeyCode = Keys.Enter) Then
Navigate(toolStripTextBox1.Text)
End If
End Sub
' Navigates to the URL in the address box when
' the Go button is clicked.
Private Sub goButton_Click( _
ByVal sender As Object, ByVal e As EventArgs) _
Handles goButton.Click
Navigate(toolStripTextBox1.Text)
End Sub
' Navigates to the given URL if it is valid.
Private Sub Navigate(ByVal address As String)
If String.IsNullOrEmpty(address) Then Return
If address.Equals("about:blank") Then Return
If Not address.StartsWith("http://") And _
Not address.StartsWith("https://") Then
address = "http://" & address
End If
Try
webBrowser1.Navigate(New Uri(address))
Catch ex As System.UriFormatException
Return
End Try
End Sub
' Updates the URL in TextBoxAddress upon navigation.
Private Sub webBrowser1_Navigated(ByVal sender As Object, _
ByVal e As WebBrowserNavigatedEventArgs) _
Handles webBrowser1.Navigated
toolStripTextBox1.Text = webBrowser1.Url.ToString()
End Sub
注解
设置此属性等效于调用 Navigate 该方法并向其传递指定的 URL。
该 WebBrowser 控件维护浏览会话期间访问的所有网页的历史记录列表。 设置 Url 属性时,控件 WebBrowser 将导航到指定的 URL,并将其添加到历史记录列表的末尾。
该 WebBrowser 控件将最近访问的网站中的网页存储在本地硬盘上的缓存中。 每个页面都可以指定一个过期日期,指示它将保留在缓存中的时长。 当控件导航到页面时,它可以通过显示缓存的版本(如果有)来节省时间,而不是再次下载页面。 Refresh使用该方法通过下载控件来强制WebBrowser重新加载当前页,确保控件显示最新版本。
注释
此属性包含当前文档的 URL,即使已请求另一个文档也是如此。 如果设置此属性的值,然后再次检索它,则在控件没有时间加载新文档时 WebBrowser ,检索的值可能与设置的值不同。 可以在事件处理程序中 DocumentCompleted 检索新值。