2
這是HTML,它被保存在一個字符串資源:WebBrowser控件外部腳本不工作
<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
(function(){
window.external.hello()
})()
</script>
</body>
</html>
這是Form1.cs中的內容:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace JSIE
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("about:blank");
webBrowser1.Document.Write(String.Empty);
webBrowser1.Document.Write(Properties.Resources.DDocument);
webBrowser1.ObjectForScripting = new JSCallbacks();
}
}
}
,這是JSCallbacks.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace JSIE
{
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class JSCallbacks
{
public void hello() {
MessageBox.Show("Hello, world!");
}
}
}
當我運行它,它不能在JavaScript訪問hello()
方法對象,並給我一個腳本錯誤消息框。我嘗試使用this
作爲ObjectForScripting
,但它也不起作用。
對於那些用'ObjectForScripting'初次使用的人:http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.objectforscripting.aspx – jcolebrand
好的,這很奇怪。出於某種原因,當我從'