Silverlight的Data Bindings:Silverlight 與 ASP.NET Ajax
不可否認,對於網頁美工人員或是動畫設計師而言,Silverlight提供了Flash以外的一個畫布,令她們可盡情揮灑創意!但對於設計師而言,Silverlight如何結合資料庫來呈現資料則是更具吸引力的課題,很明顯的!Silverlight 1.0 RC與Silverlight 1.1並不支援Data Binding機制,而且目前對於中文顯示仍存在著問題(下載字型不算是個解法,因為有法律問題),不過如果這個問題獲得解決,Silverlight結合資料庫後所呈現的效果,相信會給客戶完全不同的網頁操作感受,當然!前提得要Silverlight支援Data Bindings,否則一切都是紙上談兵罷了。要令Silverlight支援Data Bindings說來也不難,只要處理幾個關鍵問題即可。一、資料提供者為何?ASP.NET Ajax的PageMethods/Web Service可以勝任此工作,如下列程式片段所示。
private {
{
"NorthwindConnectionString"].ConnectionString)) {
"SELECT * FROM Employees ORDER BY EmployeeID", conn);
adapter.Fill(table);
} } } [WebMethod]
{
} [WebMethod]
{
}
{ } } [Serializable] public { [NonSerialized]
{
{
} }
{
{
} }
{
{
} }
{
{
} }
{
{
string.Empty : ((DateTime)_rv["HireDate"]).ToShortDateString()); } }
{ _rv = rv; } } |
運用ASP.NET Ajax及JSON,我們可以輕易的用JavaScript來取得資料庫的資料,接著只要將資料設定給指定Silverlight控制項即可,這個工作可以簡單化也可以複雜化,簡單的作法是直接在JavaScript以findName來取得控制項,然後一一設定其屬性值。
handleLoad: function(plugIn, userContext, rootElement) { this.plugIn = plugIn; this.dataContext = rootElement.children.getItem(0); PageMethods.GetCount(this.OnSucceeded,this.OnFailed,this); this.receiveData(this.currentDataIndex); }, OnSucceeded: function(result, userContext, methodName) { if (methodName == "GetData") { userContext.displayData(result); } else if(methodName == "GetCount") userContext.recordCount = result; }, OnFailed:function(error, userContext, methodName) { if(error !== null) { alert(error.get_message()); } }, displayData: function(data) { var idCtrl = this.dataContext.findName('txtEmployeeID'); var firstNameCtrl = this.dataContext.findName('txtLastName'); var lastNameCtrl = this.dataContext.findName('txtFirstName'); var titleCtrl = this.dataContext.findName('txtTitle'); var hireDateCtrl = this.dataContext.findName('txtHireDate'); var imgCtrl = this.dataContext.findName("imgPhoto"); idCtrl.text = data.EmployeeID; firstNameCtrl.text = data.FirstName; lastNameCtrl.text = data.LastName; titleCtrl.text = data.Title; hireDateCtrl.text = data.HireDate; imgCtrl.source = "Default.aspx?ID="+idCtrl.text; this.dataContext.findName("imgAnimation").begin(); } |
這種做法的缺點是設計師得用JavaScript寫下許多程式碼。另一種做法是利用Silverlight控制項的Tag屬性,指定Binding Expression,然後於JavaScript中巡覽她們來一一指定值,如下列片段所示。
<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Canvas <Canvas.Background> <LinearGradientBrush> <GradientStop <GradientStop <GradientStop </LinearGradientBrush> </Canvas.Background> <TextBlock <TextBlock <TextBlock <TextBlock <TextBlock <Image <Image.Triggers> <EventTrigger <BeginStoryboard> <Storyboard <DoubleAnimation Storyboard.TargetName="imgPhoto" Storyboard.TargetProperty="Opacity" From="0.0" </Storyboard> </BeginStoryboard> </EventTrigger> </Image.Triggers> </Image> <TextBlock <TextBlock <TextBlock <TextBlock <TextBlock </Canvas> </Canvas> |
那如何解析Tag的Binding Expression並指定值呢?答案是利用Silverlight的JavaScript支援,一一巡覽所有控制項,一一解析Tag,然後再指定值。
///////////////////////////////////////////////////////////////////////// // Silverlight Data Binding Helper 0.1 ///////////////////////////////////////////////////////////////////////// if (!window.SilverlightBinding) window.SilverlightBinding = {}; SilverlightBinding.BindingData = function(ctrl,bindingExpression) {
{
{
}
}
} SilverlightBinding.BindingData.prototype = { updateValue : function(dataItem) {
{
eval('this.ctrl.'+this.bindingProperty+ " = this.format.replace('{0}',dataItem."+this.bindingField+');');
eval('this.ctrl.'+this.bindingProperty+' = dataItem.'+this.bindingField+';'); } } } SilverlightBinding.BindingContext = function(bindingContainer) {
{
{
} }
} SilverlightBinding.BindingContext.prototype = { _childWorker : function(parent,parseParent) {
{
{
{
}
}
{
}
{
}
} }, initialize:function() {
}, OnSucceeded: function(result, userContext, methodName) {
{
userContext.bindingControls[i].updateValue(result); }
userContext.recordCount = result; }, OnFailed:function(error, userContext, methodName) {
{ alert(error.get_message()); } }, _receiveData: function(index) { eval('PageMethods.'+this.bindingMethod+ '(index,this.OnSucceeded,this.OnFailed,this);'); }, _receiveCount: function() { eval('PageMethods.'+this.bindingCountMethod+ '(this.OnSucceeded,this.OnFailed,this);'); }, next:function() {
}, prev:function() {
} } |
使用此JavaScript物件的方法很簡單,只要於handleLoad中建立此物件,然後將Silverlight的根控制項傳入即可。
handleLoad: function(plugIn, userContext, rootElement) { this.plugIn = plugIn; this.dataContext = rootElement.children.getItem(0); this.bindingContext = new SilverlightBinding.BindingContext(rootElement.children.getItem(0)); this.bindingContext.initialize(); } |
切換上下筆時也很簡單。
function Next() { window.Silverlight.current_DataContext.bindingContext.next(); window.Silverlight.current_DataContext.dataContext.findName("imgAnimation").begin(); } function Prev() { window.Silverlight.current_DataContext.bindingContext.prev(); window.Silverlight.current_DataContext.dataContext.findName("imgAnimation").begin(); } |
下圖為執行畫面。
看不到炫麗效果啊?哈~~~我承認,我的美工細胞粉差!
本例下載位址:
http://www.dreams.idv.tw/~code6421/files/SLDataDemo1.zip
No comments:
Post a Comment