0

我在使用LoadPageAsync Method时发现错误。所有要重现的代码如下。

1)等待new ChromiumWebBrowser("https://www.google.com/")页面加载。

2) 然后按button1。结果:canExecuteJs将是falsecanExecuteJs1将是true

另一方面,如果不等待new ChromiumWebBrowser("https://www.google.com/")页面加载,并且在屏幕上出现按钮的那一刻单击它,则两个变量都是true.

public ChromiumWebBrowser chromeBrowser;

public Form1()
{
    InitializeComponent();

    Cef.EnableHighDPISupport();
    Cef.Initialize(settings);

    chromeBrowser = new ChromiumWebBrowser("https://www.google.com/");

    Controls.Add(chromeBrowser);
    chromeBrowser.Dock = DockStyle.Fill;
}

public Task LoadPageAsync(IWebBrowser browser, string address = null)
{
    var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
    EventHandler<LoadingStateChangedEventArgs> handler = null;
    handler = (sender, args) =>
    {
        if (!args.IsLoading)
        {
            browser.LoadingStateChanged -= handler;
            tcs.TrySetResult(true);
        }
    };

    browser.LoadingStateChanged += handler;
    if (!string.IsNullOrEmpty(address))
        browser.Load(address);
    return tcs.Task;
}

private async void button1_Click(object sender, EventArgs e)
{
   await LoadPageAsync(chromeBrowser, "https://stackoverflow.com/questions/59862329/wordpress-submit-form-into-new-tab-and-continue-processing");
   var canExecuteJs = chromeBrowser.CanExecuteJavascriptInMainFrame; //FALSE
   await LoadPageAsync(chromeBrowser, "https://stackoverflow.com/questions/59861819/getting-2-values-from-array-of-objects");
   var canExecuteJs1 = chromeBrowser.CanExecuteJavascriptInMainFrame; //TRUE
}
4

0 回答 0