如何将WPF WebBrowser设为只读?

时间:2021-09-16 05:27:29

I'm using a WPF WebBrowser control to preview HTML typed by the user.

我正在使用WPF WebBrowser控件来预览用户键入的HTML。

example...

WPF TextBox and WebBrowser controls http://img411.imageshack.us/img411/2296/appbz9.jpg

WPF TextBox和WebBrowser控件http://img411.imageshack.us/img411/2296/appbz9.jpg

But, how do I made the WebBrowser control read-only? I don't want the user to be clicking links in there and navigating away from the preview page.

但是,如何将WebBrowser控件设为只读?我不希望用户点击那里的链接并导航离开预览页面。

I want my users to create links. I just want to make sure the "preview" pane is a preview of the correct page.

我希望我的用户创建链接。我只是想确保“预览”窗格是正确页面的预览。

5 个解决方案

#1


Capture the Navigating event of the WebBrowser control and set the Cancel property of its NavigatingCancelEventArgs to True.

捕获WebBrowser控件的Navigating事件,并将其NavigatingCancelEventArgs的Cancel属性设置为True。

Visual Basic code...

Visual Basic代码......

Private Sub WebBrowser1_Navigating(...) Handles WebBrowser1.Navigating
    If WebBrowser1Locked Then
        e.Cancel = True
    End If
End Sub

This requires a global locking boolean variable.

这需要一个全局锁定布尔变量。

Partial Public Class Window1
    Dim WebBrowser1Locked As Boolean = True
    ...
End Class

And locking and unlocking to be wrapped around the desired navigation.

并锁定和解锁以包裹所需的导航。

WebBrowser1Locked = False
WebBrowser1.NavigateToString("...")
WebBrowser1Locked = True

#2


Can't you just place a transparent window over the entire control and capture all the mouse and keyboard events there?

难道你不能只在整个控件上放置一个透明窗口并捕获那里的所有鼠标和键盘事件吗?

#3


Maybe you can catch the control's click event and throw it away before it tries to navigate to the link? I'm not sure if it's possible but I'd try that.

也许你可以捕获控件的click事件并在它尝试导航到链接之前扔掉它?我不确定是否可能,但我会尝试。

#4


You will have to sanitize the input.

您将不得不清理输入。

Do not allow anchor or script tags or on attributes. (I don't know if you can disable javascript on the control, but that would be a good idea to.

不允许使用锚点或脚本标记或属性。 (我不知道你是否可以在控件上禁用javascript,但这对你来说是一个好主意。

#5


Have anyone tried with WebBrowserControl.AllowNavigation = False I think this will do. You can enable and disable the property as you need during runtime. I use a Timer to disable the property with a custom latency, so that the document has time to load.

有没有人尝试使用WebBrowserControl.AllowNavigation = False我认为这样做。您可以在运行时根据需要启用和禁用该属性。我使用Timer来禁用具有自定义延迟的属性,以便文档有时间加载。

Good luck, Dan

祝你好运,丹

#1


Capture the Navigating event of the WebBrowser control and set the Cancel property of its NavigatingCancelEventArgs to True.

捕获WebBrowser控件的Navigating事件,并将其NavigatingCancelEventArgs的Cancel属性设置为True。

Visual Basic code...

Visual Basic代码......

Private Sub WebBrowser1_Navigating(...) Handles WebBrowser1.Navigating
    If WebBrowser1Locked Then
        e.Cancel = True
    End If
End Sub

This requires a global locking boolean variable.

这需要一个全局锁定布尔变量。

Partial Public Class Window1
    Dim WebBrowser1Locked As Boolean = True
    ...
End Class

And locking and unlocking to be wrapped around the desired navigation.

并锁定和解锁以包裹所需的导航。

WebBrowser1Locked = False
WebBrowser1.NavigateToString("...")
WebBrowser1Locked = True

#2


Can't you just place a transparent window over the entire control and capture all the mouse and keyboard events there?

难道你不能只在整个控件上放置一个透明窗口并捕获那里的所有鼠标和键盘事件吗?

#3


Maybe you can catch the control's click event and throw it away before it tries to navigate to the link? I'm not sure if it's possible but I'd try that.

也许你可以捕获控件的click事件并在它尝试导航到链接之前扔掉它?我不确定是否可能,但我会尝试。

#4


You will have to sanitize the input.

您将不得不清理输入。

Do not allow anchor or script tags or on attributes. (I don't know if you can disable javascript on the control, but that would be a good idea to.

不允许使用锚点或脚本标记或属性。 (我不知道你是否可以在控件上禁用javascript,但这对你来说是一个好主意。

#5


Have anyone tried with WebBrowserControl.AllowNavigation = False I think this will do. You can enable and disable the property as you need during runtime. I use a Timer to disable the property with a custom latency, so that the document has time to load.

有没有人尝试使用WebBrowserControl.AllowNavigation = False我认为这样做。您可以在运行时根据需要启用和禁用该属性。我使用Timer来禁用具有自定义延迟的属性,以便文档有时间加载。

Good luck, Dan

祝你好运,丹