Silverlight事件常見(jiàn)應(yīng)用指南
Silverlight事件的操作在實(shí)際應(yīng)用中是一個(gè)比較復(fù)雜的步驟。其中包含的內(nèi)容有很多種,想要全部掌握其中的操作技巧不是很容易實(shí)現(xiàn)。下面我們就先來(lái)了解一下Silverlight事件的基礎(chǔ)操作方法。#t#
先簡(jiǎn)單介紹下Silverlight事件冒泡機(jī)制,理解的朋友要以先跳過(guò)此節(jié)。Silverlight是使用控件套用控件實(shí)現(xiàn)的,比如一個(gè)button必須在Canvas容器控件內(nèi),所以在點(diǎn)擊Button的時(shí)候,如果Canvas指定了MouseLeftButtonDown事件,會(huì)先執(zhí)行Button的MouseLeftButtonDown事件,隨后執(zhí)行Canvas的MouseLeftButtonDown事件。
常規(guī)事件的綁定有三種方法可以實(shí)現(xiàn),在此為大家介紹。
1、在XAML的控件標(biāo)記中添加屬性制定,以屬性="事件的執(zhí)行名稱"。
- < Canvas onLeftButtonDown=
"leftButtonDown">- < /Canvas>
2、使用JavaScript的指定。。
- _silverlight_control.content.
findName("controlName").addEvent
Listener("onLeftButtonDown",
functionName);
3、使用MSIL的DLL中綁定(只限于Silverlight1.1以上版本)。。
- silverlight_control.MouseLeft
ButtonDown += new System.Windows.
Input.MouseEventHandler
(silverlight_control_MouseLeft
ButtonDown);
您可以自定義Silverlight事件方法,如果想在JavaScript中使用.cs或.vb中聲明托管事件,必須要使用Scriptable標(biāo)記聲明。
實(shí)例:
- C#
- [Scriptable]
- public partial class Page : Canvas
- {
- public Page()
- {
- this.Loaded += this.Page_Loaded;
- }
- private void Page_Loaded
(object sender, EventArgs args)- {
- InitializeComponent();
- Storyboard1.Completed += new
EventHandler(Storyboard1_Completed);- WebApplication.Current.Register
ScriptableObject("Page",this);- }
- [Scriptable]
- public event EventHandler eventName;
- }
調(diào)用:
- window.onload = function() {
- var silverlightControl = document.
getElementById(controlID);- if (silverlightControl)
- silverlightControl.focus();
- silverlightControl.content.Page.
Finish=window.finish_handler; //綁定- }
- window.finish_handler=function
(sender,args) //使用C#調(diào)用此方法,激活按鈕- {
- document.getElementById('RePlay').
style.display='inline';- }
本實(shí)例是一個(gè)Silverlight事件交互的程序,***用Blend在Xaml中制做出一個(gè)動(dòng)畫Storyboard的實(shí)例對(duì)象。使用程序聲明事件,并用Javascript調(diào)用事件委托。雖然可以在JavaScript中直接使用Storyboard. Completed實(shí)現(xiàn),但在此實(shí)現(xiàn)JavaScript與C#事件托管實(shí)現(xiàn)!