CEFSharp RegisterJsObject 引用错误
CEFSharp RegisterJsObject reference error
我知道这个问题已经被这个库解决了,但是尽管有可用的示例,但我很难让 Javascript 绑定与 WPF 一起工作。
我正在使用 CefSharp.Common 37.0.0
我得到的 Javascript 错误是:Uncaught ReferenceError: myClass is not defined
下面是一个小代码示例。我觉得这很简单。让我知道是否有人需要其他信息。谢谢
这是我的HTML / JS逻辑
<html>
<head>
<script type="text/javascript">
function test(){
MyClass.myFunc();
}
</script>
</head>
<body>
This is a cat
<button id='testBtn' onclick="test();" type="button">Meow</button>
</body>
</html>
C#/.NET 逻辑
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace HTML5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
webBrowser.Loaded += webBrowser_Loaded;
}
void webBrowser_Loaded(object sender, RoutedEventArgs e)
{
webBrowser.ShowDevTools();
webBrowser.RegisterJsObject("MyClass", new MyClass());
}
}
public class MyClass
{
public void MyFunc()
{
Console.WriteLine("");
}
}
}
--编辑 刚刚注意到 LoadError 事件每次都以 Aborted 状态触发。无论我是使用我的本地 HTML 文件还是托管 Google.com,这都会触发。这可能是问题的一部分吗?
Need to Register objects directly after Browser is created as there's no on the fly binding as yet
我猜你的对象注册得太晚了。在 InitializeComponent
.
后尝试注册
作为参考,有一个开放的 Feature Request
用于动态绑定,请参阅 https://github.com/cefsharp/CefSharp/issues/602
至于 Aborted
错误,我已经在 37
中看到了相当多的错误,即使页面加载正确也是如此。它似乎在版本 39
中消失了(有 -pre
个版本可用)。
我知道这个问题已经被这个库解决了,但是尽管有可用的示例,但我很难让 Javascript 绑定与 WPF 一起工作。
我正在使用 CefSharp.Common 37.0.0 我得到的 Javascript 错误是:Uncaught ReferenceError: myClass is not defined
下面是一个小代码示例。我觉得这很简单。让我知道是否有人需要其他信息。谢谢
这是我的HTML / JS逻辑
<html>
<head>
<script type="text/javascript">
function test(){
MyClass.myFunc();
}
</script>
</head>
<body>
This is a cat
<button id='testBtn' onclick="test();" type="button">Meow</button>
</body>
</html>
C#/.NET 逻辑
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace HTML5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
webBrowser.Loaded += webBrowser_Loaded;
}
void webBrowser_Loaded(object sender, RoutedEventArgs e)
{
webBrowser.ShowDevTools();
webBrowser.RegisterJsObject("MyClass", new MyClass());
}
}
public class MyClass
{
public void MyFunc()
{
Console.WriteLine("");
}
}
}
--编辑 刚刚注意到 LoadError 事件每次都以 Aborted 状态触发。无论我是使用我的本地 HTML 文件还是托管 Google.com,这都会触发。这可能是问题的一部分吗?
Need to Register objects directly after Browser is created as there's no on the fly binding as yet
我猜你的对象注册得太晚了。在 InitializeComponent
.
作为参考,有一个开放的 Feature Request
用于动态绑定,请参阅 https://github.com/cefsharp/CefSharp/issues/602
至于 Aborted
错误,我已经在 37
中看到了相当多的错误,即使页面加载正确也是如此。它似乎在版本 39
中消失了(有 -pre
个版本可用)。