CefSharp错误:执行“ ExecuteScriptAsync”或“ EvaluateScriptAsync”时,“浏览器尚未初始化”

问题描述 投票:1回答:1

我正在尝试使用CeSharp执行javascript。最后我得到一个错误:浏览器尚未初始化。使用IsBrowserInitializedChanged事件,并检查IsBrowserInitialized属性,以确定何时初始化浏览器。

当我使用时:像这样的“ ExecuteScriptAsync”方法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;

namespace CefSharp
{

    public partial class Form1 : Form
    {
        public ChromiumWebBrowser chromeBrowser;


        public Form1()
        {
            InitializeComponent();
            // Start the browser after initialize global component
            InitializeChromium();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            chromeBrowser.ExecuteScriptAsync("alert('Adam')");
        }

        public void InitializeChromium()
        {
            CefSettings settings = new CefSettings
            {
                CachePath = "cache"
            };
            // Initialize cef with the provided settings
            Cef.Initialize(settings);
            // Create a browser component
            chromeBrowser = new ChromiumWebBrowser("http://localhost/Test/index.html");

            // Add it to the form and fill it to the form window.
            this.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cef.Shutdown();
        }
    }    
}

或当我使用“ EvaluateScriptAsync”

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;

namespace CefSharp
{

    public partial class Form1 : Form
    {
        public ChromiumWebBrowser chromeBrowser;


        public Form1()
        {
            InitializeComponent();
            // Start the browser after initialize global component
            InitializeChromium();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var task = chromeBrowser.EvaluateScriptAsync("alert('Adam')");

            task.ContinueWith(t =>
            {
                if (!t.IsFaulted)
                {
                    var response = t.Result;
                    var EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message;
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }

        public void InitializeChromium()
        {
            CefSettings settings = new CefSettings();
            // Initialize cef with the provided settings
            Cef.Initialize(settings);
            // Create a browser component
            chromeBrowser = new ChromiumWebBrowser("http://localhost/Test/index.html");

            // Add it to the form and fill it to the form window.
            this.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cef.Shutdown();
        }
    }    
}

有人知道如何处理吗?

c# winforms chromium-embedded cefsharp
1个回答
0
投票

在x86下构建项目?我有一个类似的问题,只能通过x86汇编解决]

© www.soinside.com 2019 - 2024. All rights reserved.