CefSharp Javascript注册和执行在79.1.36版中不起作用 [英] CefSharp Javascript registration and execution is not working in Release 79.1.36

查看:1865
本文介绍了CefSharp Javascript注册和执行在79.1.36版中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将CefSharp从69.0.0.0版本升级到79.1.36。
我无法正常运行Javascript交互。
注册已更改

I tried to upgrade CefSharp from Version 69.0.0.0 to 79.1.36. I could not get the Javascript interaction working. The registration changed from

this.Browser.RegisterJsObject

this.Browser.JavascriptObjectRepository.Register

根据 https://github.com/cefsharp/CefSharp/issues/2990

当我执行EvaluateScriptAsync时,

When I execute EvaluateScriptAsync, I get a response back with Status Canceled.

试图了解如何正确实现它,我检查了CefSharp.WpfExample并注意到示例WPF应用程序中的Javascript功能不起作用要么。
单击运行按钮时,执行Java脚本(异步)不执行任何操作。
评估Java语言(异步)返回:

Trying to understand how to implement it correctly, I examined the CefSharp.WpfExample and noticed that the Javascript functionality in the example WPF application does not work either. The Execute Javascript (asynchronously) does not do anything when clicking the Run button. The Evaluate Javascript (Async) returns:


未捕获的ReferenceError:未定义绑定@ @about:空白:1:0

Uncaught ReferenceError: bound is not defined @ about:blank:1:0

Javascript功能在最新版本中是否中断?

Did the Javascript functionality break in the latest release?

更新

此处是我们代码中的用法。

Here is how it is used in our code.

这是注册

public void RegisterJavaScriptHandler(string name, object handler)
{
    try
    {
        CefSharpSettings.LegacyJavascriptBindingEnabled = true;
        this.Browser.JavascriptObjectRepository.Register(name, handler, false, new BindingOptions() { CamelCaseJavascriptNames = false });
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

这是EvaluateScriptAsync部分

This is the EvaluateScriptAsync part

public void InitializeLayers()
{
    try
    {
        int count = _mapLogic.Layers.Count();
        foreach (WMSLayer layer in _mapLogic.Layers)
        {
            if (!_loadedLayers.Contains(layer))
            {
                var script = string.Format("addWMSLayer('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}')",
                    layer.ProviderCode.Url, layer.AttributionText, layer.AttributionHref,
                    layer.Layer, layer.FormatCode.Format, layer.ServerType, layer.Res1, layer.Res2, layer.Res3, layer.Res4);

                var response = this.ECBBrowser.Browser.EvaluateScriptAsync(script, new TimeSpan(0, 0, 1));
                response.ContinueWith(t =>
                {
                    count--;
                    if (count == 0) this.initializeMap();

                });
                _loadedLayers.Add(layer);
            }
            else
            {
                count--;
                if(count == 0) this.initializeMap();
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

Update II

我现在认为资源加载已发生了一些变化。
这就是我所拥有的(忽略了不重要的部分)。

I now believe something has changed with the resource loading. This is what I have (unimportant parts are left out).

public class ECBSchemeHandler : IResourceHandler
{
    private string _mimeType;
    private Stream _stream;

    public bool Open(IRequest request, out bool handleRequest, ICallback callback)
    {
        var result = open(request, callback);
        handleRequest = result;
        return result;
    }

    public bool Read(Stream dataOut, out int bytesRead, IResourceReadCallback callback)
    {
        return read(dataOut, out bytesRead, callback);
    }

    public bool ReadResponse(Stream dataOut, out int bytesRead, ICallback callback)
    {
        return read(dataOut, out bytesRead, callback);
    }

    private bool open(IRequest request, ICallback callback)
    {
        var u = new Uri(request.Url);
        var file = u.Authority + u.AbsolutePath;

        var ass = Assembly.GetExecutingAssembly();
        var resourcePath = ECBConfiguration.DEFAULT_ASSEMBLY_NAMESPACE + "." + file.Replace("/", ".");

        if (ass.GetManifestResourceInfo(resourcePath) != null)
        {
            Task.Run(() =>
            {
                using (callback)
                {
                    _stream = ass.GetManifestResourceStream(resourcePath);
                    var fileExtension = Path.GetExtension(file);
                    _mimeType = ResourceHandler.GetMimeType(fileExtension);
                    callback.Continue();
                }
            });
            return true;
        }
        else
        {
            callback.Dispose();
        }           

        return false;
    }

    private bool read(Stream dataOut, out int bytesRead, IDisposable callback)
    {
        callback.Dispose();

        if (_stream == null)
        {
            bytesRead = 0;
            return false;
        }

        //Data out represents an underlying buffer (typically 32kb in size).
        var buffer = new byte[dataOut.Length];
        bytesRead = _stream.Read(buffer, 0, buffer.Length);

        dataOut.Write(buffer, 0, buffer.Length);

        return bytesRead > 0;
    }
}

}

推荐答案

使用@amaitland指出的内置ResourceHandlers可以解决Javascript注册问题。

Using the built-in ResourceHandlers as pointed out by @amaitland solved the problem with the Javascript registration.

这篇关于CefSharp Javascript注册和执行在79.1.36版中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆