首页 > 解决方案 > 如何在 CefSharp v57 for Revit 2019 中使用本地 html 文件

问题描述

我正在开发一个需要在 for Revit 插件上使用 CefSharp 的项目。我可以使用 CefSharp v65 完成任务,并尝试使用带有本地 html、css 和 js 文件的 v67 完成相同的任务。

public static string rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location).Replace("testapp.dll\\", "");

public MainView()
{
    this.chromeBrowser = new ChromiumWebBrowser(string.Format(@"{0}\ui\test.html", MainView.rootPath));
        this.Controls.Add(this.chromeBrowser);
    if (!Cef.IsInitialized)
    {
        CefSettings settings = new CefSettings();
        settings.CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache");
        settings.BrowserSubprocessPath = Path.Combine(MainView.rootPath, "CefSharp.BrowserSubprocess.exe");
        settings.LocalesDirPath = Path.Combine(MainView.rootPath, "locales");
        settings.ResourcesDirPath = Path.Combine(MainView.rootPath);
        Cef.EnableHighDPISupport();
        CefSharpSettings.LegacyJavascriptBindingEnabled = true;
        Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
    }
}

表单加载

    private void MainView_Load(object sender, EventArgs e)
    {
        CefSharpSettings.LegacyJavascriptBindingEnabled = true;
        string page = string.Format(@"{0}\ui\test.html", MainView.rootPath);
        string downloadpg = System.Net.WebClient().DownloadString(string.Format(page, MainView.rootPath));
        this.chromeBrowser.LoadHtml(string.Format(page, MainView.rootPath), page);
        this.chromeBrowser.FrameLoadEnd += OnFrameLoadEnd;
    }

    public void OnFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
    {
        string pathJS = string.Format(@"{0}\ui\js\app.js", MainView.rootPath);
        if (File.Exists(pathJS))
        {
            string script = File.ReadAllText(pathJS);
            if (e.Frame.IsMain)
            {
                chromeBrowser.ExecuteScriptAsync(script);
            }
        }
    }

上面的代码在版本 65 和 67 上可以正常工作,但是由于 Revit 2019 使用CefSharp 版本 57,因此我需要在版本 57 上进行相同的工作,并且由于某种原因这不能按预期工作。

更新

由于存在一些编译时错误,因此进行了以下更改 该行

CefSharpSettings.LegacyJavascriptBindingEnabled = true; 

去掉了。此更改已编译,但在以下位置存在运行时错误。

public void OnFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
    {
        string pathJS = string.Format(@"{0}\ui\js\app.js", Form1.rootPath);
        if (File.Exists(pathJS))
        {
            string script = File.ReadAllText(pathJS);
            if (e.Frame.IsMain)
            {
                chromeBrowser.ExecuteScriptAsync(script);// The error occurred here
            }
        }
    }

异常说此时无法执行javascript,脚本只能在V8Context内执行。使用IWebBrowser.CanExecuteJavascriptInMainFrame属性来防范这个异常。

loadHtml 方法不在屏幕上显示本地 Html 文件 如何在 CefSharp 57 版中进行这项工作。

请帮忙

谢谢

标签: c#-4.0cefsharprevit

解决方案


推荐阅读