You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从WPF WebBrowser中获取CKEditor的实例值?

WPF WebBrowser + CKEditor 问题解决方案

Hey there! Let's break down how to fix your two main issues—getting your PHP page loaded in the WPF WebBrowser and retrieving the CKEditor content—so you can get back to your CMS capstone project.

1. 解决本地PHP文件无法加载的问题

The root problem here is that PHP is a server-side language: the WebBrowser can't parse PHP code directly when you point it to a local C:\... file path—it'll just show you the raw PHP source instead of the rendered HTML with CKEditor. Here's how to fix this:

  • Set up a local PHP server: Use PHP's built-in development server (super easy!) or tools like XAMPP.
    • Open your command prompt, navigate to the folder where your PHP file is stored, and run:
      php -S localhost:8000
      
    • Now, instead of navigating to the local file path, point your WebBrowser to the server URL:
      W1.Navigate("http://localhost:8000/your-post-page.php");
      
  • Verify first: Load that same http://localhost:8000/your-post-page.php in a regular browser (Chrome/Edge) to make sure CKEditor loads correctly before testing in WPF.

2. 获取CKEditor实例的内容

Once your PHP page loads properly in the WebBrowser, you can retrieve the CKEditor content by executing JavaScript through the control. Here's how to implement this:

Step 1: Hook into the WebBrowser's LoadCompleted event

This ensures you only try to access CKEditor after the page has fully loaded. Update your add_post constructor and add an event handler:

public add_post()
{
    InitializeComponent();
    // Attach the LoadCompleted event handler
    W1.LoadCompleted += W1_LoadCompleted;
    // Use the local server URL instead of file path
    W1.Navigate("http://localhost:8000/your-post-page.php");
}

private void W1_LoadCompleted(object sender, NavigationEventArgs e)
{
    try
    {
        // Replace "editor1" with your CKEditor instance name (check your PHP page's CKEditor init code)
        string ckEditorContent = W1.InvokeScript("eval", new object[] { "CKEDITOR.instances.editor1.getData()" }) as string;
        
        // Do something with the content—like store it in a variable or save to your CMS database
        Console.WriteLine("CKEditor Content: " + ckEditorContent);
    }
    catch (Exception ex)
    {
        // Handle cases where CKEditor isn't ready yet or the instance name is wrong
        MessageBox.Show("Error fetching CKEditor content: " + ex.Message);
    }
}

Key Notes:

  • CKEditor Instance Name: Double-check your PHP page's CKEditor initialization code—if you initialized it with a different name (e.g., CKEDITOR.replace('post-content')), use that ID (post-content) instead of editor1.
  • Browser Compatibility: Older IE modes might break CKEditor. Add this meta tag to the <head> of your PHP page to force the WebBrowser to use Edge/modern rendering:
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    
  • Wait for CKEditor Ready: If you still get errors, you can add a delay or listen for CKEditor's instanceReady event in your PHP page's JS, then notify your WPF app when it's ready (using window.external.notify() and handling the WebBrowser's ScriptNotify event).

内容的提问来源于stack exchange,提问作者Chun Chun Maru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:12:47