CefSharp RegisterJsObject throw StackOverflowException

本文关键字:StackOverflowException throw RegisterJsObject CefSharp | 更新日期: 2023-09-27 17:56:53

我目前在使用CefSharp RegisterJsObject时遇到了一个问题。我的代码如下。

我正在使用

<package id="cef.redist.x64" version="3.2704.1432" targetFramework="net45" />
<package id="cef.redist.x86" version="3.2704.1432" targetFramework="net45" />
<package id="CefSharp.Common" version="51.0.0-pre02" targetFramework="net45" />
<package id="CefSharp.WinForms" version="51.0.0-pre02" targetFramework="net45" />

程序.cs

namespace Project1
{
    public static class Program
    {
        [STAThread]
        private static void Main()
        {
            Initialize();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var cefSettings = new CefSettings
            {
                UserAgent =
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36"
            };
            Cef.Initialize(cefSettings);
            new FormSplash().Show();
            Application.Run();
        }
    }
}

表单飞溅.cs

namespace Project1
{
    public partial class FormSplash : Form
    {
        public ChromiumWebBrowser Browser { get; set; }
        public JsObject JsObj { get; set; }
        public FormSplash()
        {
            InitializeComponent();
            JsObj = new JsObject(this);
        }
        private void FormChromeToneExcelRegistration_Load(object sender, EventArgs e)
        {
            Browser = new ChromiumWebBrowser("https://google.com");
            Browser.RegisterJsObject("jsObj", new JsObject(this));
            Browser.FrameLoadStart += Browser_FrameLoadStart;
            Browser.FrameLoadEnd += Browser_FrameLoadEnd;
            Controls.Add(Browser);
            Browser.Dock = DockStyle.Fill;
        }
        private void SetMessage(string message)
        {
            ...
        }
        public class JsObject
        {
            public FormSplash Form { get; set; }
            public JsObject(FormSplash form)
            {
                Form = form;
            }
            public void SetMessage(string message)
            {
                Form.SetMessage(message);
            }
        }
    }
}

我一直在 RegisterJsObject 行上没有内部异常详细信息的情况下获得An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

我也尝试过的场景:

  • SetMessage 更改为 setMessage (不起作用)
  • 已移出嵌套类(不起作用)

我已经尝试了下面的这些代码及其工作。

Browser.RegisterJsObject("jsObj", "");
Browser.RegisterJsObject("jsObj", new {});
Browser.RegisterJsObject("jsObj", new DateTime());

请帮助我为什么我的项目中的类会抛出 StackOverflowException?

CefSharp RegisterJsObject throw StackOverflowException

感谢@amaitland。解决方案是将JsObject中的Form设为私有或JavascriptIgnoreAttribute

相关文章:
  • 没有找到相关文章