1

我在 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>

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

4

1 回答 1

0

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

于 2018-04-16T18:13:17.793 回答