当文档属性更改WPF WebBrowser控件不进入设计模式 [英] WPF WebBrowser control doesn't enter design mode when the document property is changed

查看:406
本文介绍了当文档属性更改WPF WebBrowser控件不进入设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个令人沮丧的问题。下面是我在做什么的简化版本:

I have a frustrating problem. Here's a simplified version of what I'm doing:

在C#中的用户控件包含一个工具栏和一个嵌入式web浏览器对象。工具栏包含编辑按钮,点击它集时,在设计模式中WebBrowser控件。另一个按钮,取消,关闭设计模式

A UserControl in c# contains a toolbar and an embedded WebBrowser object. The toolbar contains an "Edit" button, which when clicked sets the webbrowser control in design mode. Another button, "Cancel", turns off design mode.

伪代码(很简单):

public void SetDesignMode(bool dm) {
  IHTMLDocument2 doc = webBrowser.Document as IHTMLDocument2;
  if (dm) doc.designMode = "On";
  else doc.designMode = "Off";
  _designMode = dm;
  ReloadDocument(); // setting designmode clears the document element, so it must be reloaded
}

public void OnLoadCompleted() {
  IHTMLDocument2 doc = webBrowser.Document as IHTMLDocument2;
  if (!_documentLoaded) {
    if (_designMode) doc.designMode = "On";
    else doc.designMode = "Off";
    ReloadDocument();
    _documentLoaded = true;
  }
}

public void ReloadDocument() {
  _documentLoaded = false;
  // code that navigates to the document
}



的问题:
。如果我点击所显示的网页上,然后在编辑按钮,WebBrowser控件不会成为编辑。用吸尘器吸尘过的照片时,鼠标指针/链接显示在Web浏览器导航鼠标光标,而不是编辑的。如果我在文本中单击,光标将不会显示。

The problem: If I click on the displayed web page, and then on the "Edit" button, the WebBrowser control will not become editable. The mouse pointer when hoovering over pictures/links show the web browser navigation mouse pointers, not the editing ones. If I click in the text, the caret won't display.

调试表明,如果它被设置为文档上的designMode属性实际上是设置为开在这种情况下,但控制行为关。

如果我不点击网页中点击之前的编辑按钮,一切正常

If I don't click in the web page before clicking the "Edit" button, everything works as expected.

精化:
。如果我点击时,控制在取消按钮设计模式中,我得到相应的(MIS)的行为,如果文件已经被点击了

Elaboration: If I click the "Cancel" button when the control is in design mode, I get the corresponding (mis)behaviour, if the document have been clicked in.

您只需通过编辑单击,然后选择取消,然后在编辑等,而不在文档中点击过正常工作(鼠标悬停测试显示了适当的鼠标指针,并根据设计模式,如果我点击所显示的文档中的链接,我得到的链接导航或编辑)。

Simply clicking on "Edit", then "Cancel", then "Edit" etc. without ever clicking in the document works fine (the mouseover test shows the proper mouse pointers, and I get link navigation or editing depending on the design mode if I click a link in the displayed document).

我已经试过各种方法,以确保其他控件获得之前,我改变了designMode属性集中,但它并没有任何区别。我搜索MSDN和已知互联网的一半,还没有发现此类问题的任何提及。 。翻转这样的designMode属性似乎是相当不同寻常。

I've tried various techniques to make sure that another control gets focus before I change the designMode property, but it doesn't make any difference. I've searched MSDN and half of the known internet and haven't found any mention of this kind of problem. Flipping the designMode property like this seems to be quite unusal.

信息还有一个花絮:我的文档由实施了水槽建议设置文档事件用户控件。我怀疑,这应该对这个问题有任何影响,但是我在这里包括它的是完整的缘故。 更新:禁用这并不改变关于什么问题。

One more tidbit of information: I'm setting up document events by advising the document with a sink implemented by the usercontrol. I doubt that this should have any bearing on the problem, but I've included it here for the sake of being complete. Update: Disabling this doesn't change anything regarding the problem.

有谁认识这个问题。

更新:
我已经通过重新创造SetDesignMode的Web浏览器控件解决该问题的工作()。这是一个丑陋的解决方案,但它的工作原理和它实际上看起来OK。我在这个问题上的任何反馈非常感兴趣,虽然。我相信这是在MSHTML中的错误。

Update: I've worked around the problem by re-creating the web browser control in SetDesignMode(). It's an ugly solution, but it works and does actually look ok. I'm very interested in any feedback on this problem, though. I believe it is a bug in MSHTML.

推荐答案

我不太清楚,如果我们有完全相同的问题,但我假设我的解决方案应为你工作,以及。

I'm not quite sure if we had exactly the same problem, but I suppose my solution should work for you as well.

的基本问题似乎是64复位的designMode属性,如的这篇文章。就我而言,我将它设置为开实例化网页浏览器后,但在DocumentCompleted事件时,它被继承了。设置回开DocumentCompleted使得它可编辑的,但清除文档。设置DocumentText再次重新启动整个厄运循环。

The basic issue seems to be that x64 reset the designMode attribute, as noted in this article. In my case, I set it to "On" after instantiating the webbrowser, but in the DocumentCompleted event, it was "Inherit" again. Setting it back to "On" in DocumentCompleted makes it editable, but clears the document. Setting the DocumentText again restarts the whole doom loop.

所以,一个解决方案,我发现是自定DocumentText克制,而不是我创建了一个空文件,然后将身体的(它在这一点不再为空)的innerHTML属性:

So one solution I found was to refrain from setting the DocumentText, instead I created an empty document, then set the body's (which at this point is no longer null) InnerHtml property:

doc.designMode = "On"; // enable editing

// designMode change resets the document, create it anew
webBrowser1.Document.Write("<html><body></body></html>")
webBrowser1.Document.Body.InnerHtml = "myDocumentText"

显然,如果你有现成的文字,而不是如果你导航到一个URL这仅适用。然而,还有另一种解决方案,它为我工作,这似乎更容易和安全。我发现它在此答案由LaughingJohn 的。我想的第一行依赖于你的应用程序,你有IHTMLDocument直接webBrowser1.Document。

Obviously, this works only if you have the text ready, and not if you're navigating to an URL. However, there is another solution which worked for me, which seems easier and safer. I found it in this answer by LaughingJohn. I guess the first line depends on your application, you had the IHTMLDocument directly in webBrowser1.Document.

doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
if (doc != null && doc.body != null)
    ((HtmlBody)doc.body).contentEditable = "true";

这篇关于当文档属性更改WPF WebBrowser控件不进入设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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