另類的Silverlight中文解法
文/黃忠成
如你所知,由於字型的關係,Silverlight目前對於雙位元文字顯示可說是困難重重,大概可歸類出兩種解法,一種是下載字型到客戶端,一種是利用Blend2將文字變成圖形,下載字型目前有法律問題的隱憂,而且一個中文字型檔大小約5MB,仍嫌過大。在上次嘗試實作DataBinding功能後,心中就有一種想法,假如於Server端將文字轉成圖形後下載到客戶端,那麼因為不是直接下載字型,所以應無法律問題(這我不確定!),也不會因下載整個字型檔而導致網頁開啟速度過慢,這樣是否就能讓前次的Data Bindigns例子支援中文的顯示呢?可惜前幾天因陪老婆考試,一直沒時間來實現心中的構想,這幾天終於有時間來實現這個架構了。基本上,實現這個架構有兩個問題必須先行解決,第一個問題是Server端如何將文字變成圖形?這不難,下列的程式便可辦到。
| protected { 
 Request.QueryString["ID"].Length > 0) { .................. } 
 { 
 
 ResolveParams(Request.QueryString["Transform"], out index, out column); 
 { 
 
 
 { Response.Clear(); Response.BufferOutput = true; Response.ContentType = "image/bmp"; Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length); ms.Dispose(); Response.Flush(); Response.End(); } 
 Response.End(); } } } 
 { 
 index = -1; column = string.Empty; 
 { index = int.Parse(p[0]); column = p[1]; } } 
 { 
 
 
 new 
 } 
 Color background, int width, int height) { 
 
 
 
 g.Clear(Color.Transparent); g.DrawString(str, font, bFore, 2, 2); bFore.Dispose(); bBack.Dispose(); 
 bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); bmp.Dispose(); ms.Position = 0; 
 } | 
如果你仔細看上面的程式,會發覺其中有字體預設的問題,若要以此觀念實作一個完整的架構,關於字型的資訊應於XAML中指定才好,不過目前我只是展示這個想法的可行性,就先放著這部份不理了。第二個要解決的問題是如何於XAML中指定Data Bindings資訊,這點不難,依據前版以TAG來指定Binding Expression的概念,只需加油添醋一番,即可套用。
| <Canvas ................... <Image </Canvas> | 
請注意,由於採圖形方式的緣故,這裡已不再使用TextBlock,而是使用Image控制項來顯示,SLDH.js也需稍做修改。
| ///////////////////////////////////////////////////////////////////////// // Silverlight Data Binding Helper 0.1 ///////////////////////////////////////////////////////////////////////// if (!window.SilverlightBinding) window.SilverlightBinding = {}; SilverlightBinding.BindingData = function(ctrl,bindingExpression,context) { 
 
 
 
 
 { 
 
 { 
 
 } 
 
 
 
 
 
 } 
 } SilverlightBinding.BindingData.prototype = { updateValue : function(dataItem) { 
 { 
 { 
 
 str = str.replace("{INDEX}",this.context.currentDataIndex); 
 str = str.replace("{BindingField}",this.bindingField); eval('this.ctrl.'+this.bindingProperty+" = str;"); } 
 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() { 
 
 
 } } | 
下圖為執行例。

後記
這種方式僅是一個應急的解法,畢竟傳輸圖形也是需要時間的,自然不比直接使用位於客戶端的字型來的有效率。
示例下載:
http://www.dreams.idv.tw/~code6421/files/SLDataDemo2.zip
 
 
 
 
 
No comments:
Post a Comment