使用 Document.InvokeScript 和 JavaScript 代码设置数据



我在webBrowser控件中嵌入了TinyMCE代码,并用作HTML编辑器。获取数据不提供错误,但设置数据出现问题。这是我的代码:

public partial class TinyMCE : UserControl
{
    public TinyMCE()
    {
        InitializeComponent();
    }
    public string HTML
    {
        get
        {
            var content = string.Empty;
            if (webBrowserControl.Document != null)
            {
                if (webBrowserControl.Document.Body != null)
                {
                    var html = webBrowserControl.Document.InvokeScript("GetContent");
                    content = html.ToString();
                }
            }
            return content;
        }
        set
        {
            if (webBrowserControl.Document != null)
            {
                //MessageBox.show("bug...");
                webBrowserControl.Document.InvokeScript("SetContent",
                    new object[] { value });
            }
        }
    }
}

我是这样使用它的:tinyMCE1.HTML = "<b>Some</b> <u>HTML</u> data in <i>here</i>.";

以下是嵌入的 HTML 代码:

<!DOCTYPE html>
<html>
<script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
    function GetContent()
    {
        return tinyMCE.activeEditor.getContent();
    }
    function SetContent(htmlContent)
    {
        tinyMCE.activeEditor.setContent(htmlContent);
    }
</script>
<script type="text/javascript">
    tinyMCE.init({
      oninit : function() {
        tinyMCE.get('tinyMceEditor').execCommand('mceFullScreen');
      },
      mode: "textareas",
      skin: "o2k7",
      theme: "advanced",
      plugins: "fullpage,pagebreak,style,fullscreen",
      theme_advanced_buttons1: "bold, italic, underline, strikethrough, |, justifyleft, justifycenter, justifyright, justifyfull, |, bullist, numlist, |, outdent, indent, |, undo, redo, |, forecolor, backcolor, |, formatselect,fontselect,fontsizeselect, hr, |, sub, sup, |, pagebreak",
      theme_advanced_buttons2: "",
      theme_advanced_toolbar_location: "top",
      theme_advanced_toolbar_align: "left",
      theme_advanced_resizing: false,
      extended_valid_elements: "a[name|href|target|title|onclick],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
    });
</script>
<body>
  <form method="post">
      <textarea name="tinyMceEditor" cols="1" rows="1" style="width:100%; height: 100%"></textarea>
  </form>
</body>
</html>

这不起作用,但是当我添加一个愚蠢的消息框时,它可以工作。我不明白为什么会这样。

今天,当我和我亲爱的朋友坦塞尔(Tansel(交谈时,他告诉我使用Application.DoEvents();。我还看到,当我使用Application.DoEvents();而不是MessageBox.Show("A silly solution...");时,问题得到了解决。感谢坦塞尔的这个好主意。

相关内容

  • 没有找到相关文章

最新更新