轉(zhuǎn)帖|其它|編輯:郝浩|2010-12-14 15:45:23.000|閱讀 619 次
概述:要實(shí)現(xiàn)JavaScript調(diào)用Silverlight程序里面的托管代碼,需要先在應(yīng)用程序的啟動(Application_Startup)事件里注冊要進(jìn)行訪問的對象,而要從Silverlight的托管代碼里訪問HTML頁面對象或者頁面中的JavaScript,使用HtmlPage的Document/HtmlElement和HtmlWindow即可。本文就以例子來說明兩者相互訪問的方法,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
要實(shí)現(xiàn)JavaScript調(diào)用Silverlight程序里面的托管代碼,需要先在應(yīng)用程序的啟動(Application_Startup)事件里注冊要進(jìn)行訪問的對象,而要從Silverlight的托管代碼里訪問HTML頁面對象或者頁面中的JavaScript,使用HtmlPage的Document/HtmlElement和HtmlWindow即可。
下面,我們就以例子來說明兩者相互訪問的方法,代碼里面有很詳細(xì)的注釋,這里不再累述。
Page.xaml:
<UserControlx:Class="SilverlightApplication1.Page"
xmlns="//schemas.microsoft.com/client/2007"
xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
Width="400"Height="300">
<Gridx:Name="LayoutRoot"Background="White">
<CanvasCanvas.Top="20">
<TextBlockCanvas.Top="10"Canvas.Left=
"20">請輸入您的姓名:</TextBlock>
<TextBoxx:Name="UserInput"Width=
"200"Height="30"Canvas.Top="40"Canvas.Left="20"></TextBox>
<TextBlockx:Name="Msg"Canvas.Top=
"90"Canvas.Left="20"Foreground="Navy"FontSize="36"></TextBlock>
<ButtonClick="Button_Click"Content="單擊我"FontSize="24"Width="160"Height=
"60"x:Name="BtnTest"Canvas.Top="160"Canvas.Left="20"></Button>
</Canvas>
</Grid>
</UserControl>
Page.xaml.cs:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Animation;
usingSystem.Windows.Shapes;
usingSystem.Windows.Browser;
namespaceSilverlightApplication1
{
public
partial
classPage : UserControl
{
publicPage()
{
InitializeComponent();
}
private
voidButton_Click(objectsender, RoutedEventArgs e)
{
stringUserInputContent=
this.UserInput.Text;
if(UserInputContent.Equals(String.Empty))
{
UserInputContent=
"Hello Silverlight World!";
}
else
{
UserInputContent=
"你好,"
+UserInputContent;
}
HtmlWindow win=HtmlPage.Window;
this.Msg.Text=UserInputContent;
win.Alert("Silverlight 里面彈出的對話框。"
+UserInputContent);
//執(zhí)行頁面中的js函數(shù):
win.Eval("getArrayTest()");
Object[] args={"將此參數(shù)傳遞給 js 函數(shù)"};
win.Invoke("getArrayTest", args);
//如果頁面中的值
HtmlDocument doc=HtmlPage.Document;
doc.GetElementById("UserName").SetAttribute("value",this.UserInput.Text);
}
[ScriptableMember()]
public
stringInterInvole()
{
stringusername=
HtmlPage.Document.GetElementById("UserName").GetAttribute("value");
this.UserInput.Text=username;
this.Msg.Text=
"您輸入了:"
+username;
return
"你從js腳本中調(diào)用了 Silverlight 里面的方法。";
}
}
}
App.xaml.cs:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Animation;
usingSystem.Windows.Shapes;
usingSystem.Windows.Browser;
namespaceSilverlightApplication1
{
public
partial
classApp : Application
{
publicApp()
{
this.Startup+=
this.Application_Startup;
this.Exit+=
this.Application_Exit;
this.UnhandledException+=
this.Application_UnhandledException;
InitializeComponent();
}
private
voidApplication_Startup(objectsender, StartupEventArgs e)
{
//Load the main control
Page p=
newPage();
HtmlPage.RegisterScriptableObject("SilverlightApplicationExample", p);
//請注意這里的定義方法,如果這里的p寫成 new Page(),則Javascript基本不能給 UserInput 賦值!
this.RootVisual=p;
}
private
voidApplication_Exit(objectsender, EventArgs e)
{
}
private
voidApplication_UnhandledException
(objectsender, ApplicationUnhandledExceptionEventArgs e)
{
}
}
}
SilverlightApplication1TestPage.aspx:
<%@ Page Language="C#"AutoEventWireup="true"
%>
<%@ Register Assembly="System.Web.Silverlight"Namespace=
"System.Web.UI.SilverlightControls"TagPrefix="asp"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="//www.w3.org/1999/xhtml">
<headrunat="server">
<title>Silverlight 2托管代碼與Javascript交互的例子</title>
<scripttype="text/javascript">
//<!{CDATA[
//定義全局變量:
vartestVar=
"孟憲會";
//定義全局函數(shù):
functiongetArrayTest()
{
if(arguments.length>
0)
{
alert("js 對話框:您傳遞了參數(shù)。"
+arguments[0]);
return arguments[0];
}
else
{
alert("js 對話框:無參數(shù)調(diào)用。");
return
"js 函數(shù)返回值";
}
}
functionSetUserName()
{
alert(SilverlightPlugin.Content.SilverlightApplicationExample.InterInvole());
}
//定義Silverlight插件對象
varSilverlightPlugin=
null;;
functionpluginLoaded(sender)
{
SilverlightPlugin=sender.get_element();
}
//]]>
</script>
</head>
<bodystyle="height: 100%; margin: 0;">
<formid="form1"runat="server">
<divstyle="border: 2px solid #EEE; margin: 20px;padding:20px">
請輸入你的名字:<inputid="UserName"type="text"value=""
/>
<inputtype="button"onclick="SetUserName()"value="將名字傳遞到 Silverlight"
/>
</div>
<br/>
<divstyle="border: 2px solid #EEE;margin: 20px;">
<asp:ScriptManagerID="ScriptManager1"runat="server">
</asp:ScriptManager>
<asp:SilverlightID="Xaml1"runat="server"OnPluginLoaded=
"pluginLoaded"Source="~/ClientBin/SilverlightApplication1.xap"Version="2.0"
Width="400px"Height="300px"
/>
</div>
</form>
</body>
</html>
運(yùn)行結(jié)果:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載