Silverlight与javascript互调用方法总结
2011-03-17 17:32
417 查看
1. silverlight 中使用javascript
(1) 引用名字空间: using System.Windows.Browser;
(2) 方法1:HtmlPage.Window.Eval() 这个方法直接执行字符串的javascript;
string prompt = "alert(\"";
prompt += ex.Message;
prompt += " \");";
HtmlPage.Window.Eval(prompt);
\\ 其中的ex为异常对象,我经常用来打印异常信息。
方法2:HtmlPage.Window.Invoke("InitializeFso", new object[] { });
其中第一个参数是javascript 的函数名称,第二参数是调用这个函数的参数数组。
earchPath.Text = strNode;
string strPath = "";
try
{
strPath = HtmlPage.Window.Invoke("GetAllDirectory", new string[] { strNode }) as string;
}
catch (System.Exception ex)
{
HtmlPage.Window.Eval("alert(\"Get directory error!\")");
string prompt = "alert(\"";
prompt += ex.Message;
prompt += " \");";
HtmlPage.Window.Eval(prompt);
}
方法3:htmlpage.window.createInstance();
方法4:silverlight中定义事件,同时在承载silverlight的页面上利用javascript注册事件响应
sl中:
public class JSControl
{
public JSControl()
{
System.Windows.Browser.HtmlPage.RegisterScriptableObject("jscontrol", ctrl);
}
[System.Windows.Browser.ScriptableMember]
public event EventHandler CallJs;
public void TriggerCallJs()
{
if (CallJs != null)
CallJs(this, EventArgs.Empty);
}
}
js中: function OnLoaded(sender, args) {
sender.Content.jscontrol.CallJs = calledBySL;
}
function calledBySL(sender, args) {
alert("i'm js, called by silverlight" + args);
}
function createSilverlight() {
Silverlight.createObjectEx({
source: "ClientBin/CensorProtal.xap",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "100%",
height: "100%",
minRuntimeVersion: "4.0.50401.0",
enableHtmlAccess: "true"
},
events: { onLoad: OnLoaded }
});
另外的例子:
1.首先在silverlight布局界面中增加一个按钮,如下:
<Button Content="Call javascript" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="callButton" VerticalAlignment="Top" Width="175" Click="callButton_Click" />
2.在代码文件中增加以下代码,可逐个验证一下4种方法
private void callButton_Click(object sender, RoutedEventArgs e)
{
//method 1
//ScriptObject script = HtmlPage.Window.GetProperty("HelloWorld") as ScriptObject;
//script.InvokeSelf();
//method 2
//HtmlPage.Window.Alert("Hollo world 2!");
//method 3
//ScriptObject script = HtmlPage.Window.CreateInstance("myHello");
//script.Invoke("ShowAlert");
//method 4
HtmlPage.Window.Eval("a=1+2;alert(a);");
}
3.在html页面中增加以下javascript代码。
<script type="text/javascript">
function HelloWorld() {
alert("Hollo world!");
}
myHello = function (message) {
this.Message = message;
};
myHello.prototype.ShowAlert = function () {
alert("Hollo world 3!");
};
</script>
2. JS 调用Silverlight
1. 在Silverlight的UserControl的派生类的构造函数中进行注册Silverlight给js 的运行期。
try
{
HtmlPage.RegisterScriptableObject("Test", this);
}
catch (System.Exception ex)
{
HtmlPage.Window.Eval("alert(\"register Silverlight error!\")");
string prompt = "alert(\"";
prompt += ex.Message;
prompt += " \");";
HtmlPage.Window.Eval(prompt);
}
2. 在Silverlight中建立可以让js 调用的方法:使用关键字[ScriptableMember], 还有[ScriptableType]
例如:[ScriptableMember]
public void QueryResultstring(string strmessage)
{
QueryScanResult Result = new QueryScanResult();
Result.ParserFromString(strmessage);
if (Result.ErrorCode != "0")
{
return;
}
string strShow = "scaning: ";
if (Result.Section == "2")
{
ScanPathShower.Text = strShow + Result.TargetFile;
}
if (Result.Section == "4")
{
ScanPathShower.Text = strShow + "Finished!";
m_bScanButton = false;
}
// Section 0 undefine
// section 1 ready
// section 2 scaning
// section 3 pause
// section 4 completed
// Query again
}
3. 在测试silverlight 的页面增加 silverlight obj 的ID
<object id ="SilverlightControlXXX" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="DubaSilverlightDemo.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40307.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=141205" mce_href="http://go.microsoft.com/fwlink/?LinkID=141205" style="text-decoration: none;" mce_style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" mce_src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none" mce_style="border-style: none"/>
</a>
</object>
4. 在Js 中调用
var control = document.getElementById("SilverlightControlxxx");
//alert("OK");
try
{
alert("Call silverlight");
control.Content.Test.QueryVirusstring(KxRes);
}
catch(e)
{
alert("Exception");
}
(1) 引用名字空间: using System.Windows.Browser;
(2) 方法1:HtmlPage.Window.Eval() 这个方法直接执行字符串的javascript;
string prompt = "alert(\"";
prompt += ex.Message;
prompt += " \");";
HtmlPage.Window.Eval(prompt);
\\ 其中的ex为异常对象,我经常用来打印异常信息。
方法2:HtmlPage.Window.Invoke("InitializeFso", new object[] { });
其中第一个参数是javascript 的函数名称,第二参数是调用这个函数的参数数组。
earchPath.Text = strNode;
string strPath = "";
try
{
strPath = HtmlPage.Window.Invoke("GetAllDirectory", new string[] { strNode }) as string;
}
catch (System.Exception ex)
{
HtmlPage.Window.Eval("alert(\"Get directory error!\")");
string prompt = "alert(\"";
prompt += ex.Message;
prompt += " \");";
HtmlPage.Window.Eval(prompt);
}
方法3:htmlpage.window.createInstance();
方法4:silverlight中定义事件,同时在承载silverlight的页面上利用javascript注册事件响应
sl中:
public class JSControl
{
public JSControl()
{
System.Windows.Browser.HtmlPage.RegisterScriptableObject("jscontrol", ctrl);
}
[System.Windows.Browser.ScriptableMember]
public event EventHandler CallJs;
public void TriggerCallJs()
{
if (CallJs != null)
CallJs(this, EventArgs.Empty);
}
}
js中: function OnLoaded(sender, args) {
sender.Content.jscontrol.CallJs = calledBySL;
}
function calledBySL(sender, args) {
alert("i'm js, called by silverlight" + args);
}
function createSilverlight() {
Silverlight.createObjectEx({
source: "ClientBin/CensorProtal.xap",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "100%",
height: "100%",
minRuntimeVersion: "4.0.50401.0",
enableHtmlAccess: "true"
},
events: { onLoad: OnLoaded }
});
另外的例子:
1.首先在silverlight布局界面中增加一个按钮,如下:
<Button Content="Call javascript" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="callButton" VerticalAlignment="Top" Width="175" Click="callButton_Click" />
2.在代码文件中增加以下代码,可逐个验证一下4种方法
private void callButton_Click(object sender, RoutedEventArgs e)
{
//method 1
//ScriptObject script = HtmlPage.Window.GetProperty("HelloWorld") as ScriptObject;
//script.InvokeSelf();
//method 2
//HtmlPage.Window.Alert("Hollo world 2!");
//method 3
//ScriptObject script = HtmlPage.Window.CreateInstance("myHello");
//script.Invoke("ShowAlert");
//method 4
HtmlPage.Window.Eval("a=1+2;alert(a);");
}
3.在html页面中增加以下javascript代码。
<script type="text/javascript">
function HelloWorld() {
alert("Hollo world!");
}
myHello = function (message) {
this.Message = message;
};
myHello.prototype.ShowAlert = function () {
alert("Hollo world 3!");
};
</script>
2. JS 调用Silverlight
1. 在Silverlight的UserControl的派生类的构造函数中进行注册Silverlight给js 的运行期。
try
{
HtmlPage.RegisterScriptableObject("Test", this);
}
catch (System.Exception ex)
{
HtmlPage.Window.Eval("alert(\"register Silverlight error!\")");
string prompt = "alert(\"";
prompt += ex.Message;
prompt += " \");";
HtmlPage.Window.Eval(prompt);
}
2. 在Silverlight中建立可以让js 调用的方法:使用关键字[ScriptableMember], 还有[ScriptableType]
例如:[ScriptableMember]
public void QueryResultstring(string strmessage)
{
QueryScanResult Result = new QueryScanResult();
Result.ParserFromString(strmessage);
if (Result.ErrorCode != "0")
{
return;
}
string strShow = "scaning: ";
if (Result.Section == "2")
{
ScanPathShower.Text = strShow + Result.TargetFile;
}
if (Result.Section == "4")
{
ScanPathShower.Text = strShow + "Finished!";
m_bScanButton = false;
}
// Section 0 undefine
// section 1 ready
// section 2 scaning
// section 3 pause
// section 4 completed
// Query again
}
3. 在测试silverlight 的页面增加 silverlight obj 的ID
<object id ="SilverlightControlXXX" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="DubaSilverlightDemo.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40307.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=141205" mce_href="http://go.microsoft.com/fwlink/?LinkID=141205" style="text-decoration: none;" mce_style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" mce_src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none" mce_style="border-style: none"/>
</a>
</object>
4. 在Js 中调用
var control = document.getElementById("SilverlightControlxxx");
//alert("OK");
try
{
alert("Call silverlight");
control.Content.Test.QueryVirusstring(KxRes);
}
catch(e)
{
alert("Exception");
}
相关文章推荐
- 总结Silverlight与HTML页面交互基本方法(js调用Silverlight方法/属性)
- SilverLight浏览器交互之:SilverLight程序调用外部Html中的Javascript方法(附源码)
- JavaScript调用Windows应用程序方法总结
- Silverlight 4 WebBrowser的使用及调用 WebBrowser 中的 javascript 方法
- [Asp.net+JS]asp.net中调用javascript自定义函数的方法总结
- [Silverlight探秘]利用Silverlight调用Javascript的方法
- (转)ASP.NET调用javascript脚本的方法总结
- JavaScript调用后台方法总结
- 在Silverlight1.1中定义与调用Javascript事件的方法
- asp.net中调用javascript自定义函数的方法(包括引入JavaScript文件)总结
- 从 JavaScript 调用silverlight方法
- SilverLight浏览器交互之:Html页面通过Javascript调用SilverLight程序内方法(附源码)
- [Asp.net+JS]asp.net中调用javascript自定义函数的方法总结
- [Silverlight探秘]利用Javascript调用silverlight的方法
- ASP.NET调用javascript脚本的方法总结
- 总结Silverlight与HTML页面交互基本方法(js调用Silverlight方法/属性 和Silverligth调用页面js方法)
- asp.net中调用javascript自定义函数的方法(包括引入JavaScript文件)总结
- SilverLight学习笔记--Silverligh之在SL中调用Javascript (四种调用方法+简单与复杂参数的传递)
- ASP.NET调用javascript脚本的方法总结
- ASP.NET调用javascript脚本的方法总结