日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Silverlight 5 Beta新特性[5]隐式模板支持

發(fā)布時間:2025/3/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Silverlight 5 Beta新特性[5]隐式模板支持 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

繼續(xù)更新Silverlight 5 Beta新特性.在Silverlight 5 BEta更新出來新特性中有一些是WPF已經(jīng)存在的影子.類似前面提到的多窗體[Multiple Window Support].其實很多熟悉WPF同學(xué)應(yīng)該能看到隱式模板[Implicit DataTemplates]也是借鑒WPF而來.Silverlight 4中DataTemplate在非UI項實現(xiàn)數(shù)據(jù)綁定模板可以多處重用.大大增加數(shù)據(jù)顯示多樣性提高重用的效率.當(dāng)然在Silverlight 5 Beta增加了Implicit DataTemplate隱式模板.提供一些新的功能:

Silverlight 5 Implicit DataTemplate:
[1]ContentPresenter的DataTemplates可以選擇基于某個數(shù)據(jù)類型進(jìn)行相關(guān)聯(lián)綁定
[2]隱式定義DataTemplates
[3]當(dāng)數(shù)據(jù)源DataContent發(fā)生變化時.動態(tài)更新的ContentPresenter DataTemplate
[4]在適當(dāng)范圍啟用的DataTemplates
?

類似我們現(xiàn)在定義一項需求:星級酒店推出系統(tǒng)中包含多個服務(wù):RoomService[客房服務(wù)]/Communication[外接服務(wù)]當(dāng)我們把服務(wù)綁定UI上供給用戶選擇時通常采用DataTemplate方式進(jìn)行綁定呈現(xiàn).這時一種簡單通用的方式.定義服務(wù)的實體類:

  • public?class?Room??? ?
  • ???{??? ?
  • ???????public?string?RoomId?{?get;?set;?}??? ?
  • ???????public?string?RoomName?{?get;?set;?}??? ?
  • ???????public?string?RoomType?{?get;?set;?}??? ?
  • ???}??? ?
  • ??? ?
  • ???public?class?RoomService?:Room??? ?
  • ???{?? ?
  • ???????public?string?ServiceId?{?get;?set;?}?? ?
  • ???????public?string?ServiceName?{?get;?set;?}?? ?
  • ???????public?double?ServicePrice?{?get;?set;?}?? ?
  • ???????public?DateTime?ServiceDate?{?get;?set;?}?? ?
  • ???}?? ?
  • ?
  • ???public?class?RoomCommunication?:?Room?? ?
  • ???{?? ?
  • ???????public?string?CommClientID?{?get;?set;?}?? ?
  • ???????public?int?CommSpyCounter?{?get;?set;?}?? ?
  • ???}?
  • 模擬提供數(shù)據(jù)源:

  • public?class?VipRoom_Service??? ?
  • ???{??? ?
  • ???????public?static?IList<Room>?GetRoomServiceDataRecord()??? ?
  • ???????{??? ?
  • ???????????List<Room>?getDataRoomService?=?new?List<Room>();??? ?
  • ???????????getDataRoomService.Add(?new?Room?{??RoomName="StandBy_Phone_Convert"});??? ?
  • ???????????getDataRoomService.Add(?new?RoomService()?{?RoomName?=?"DUIUser_DataType_Room"});??? ?
  • ???????????getDataRoomService.Add(?new?RoomService()?{?RoomName?=?"VIPService_DataType_Room"});??? ?
  • ???????????getDataRoomService.Add(?new?RoomCommunication()?{?RoomName?=?"Communication_Client_Room"?});?? ?
  • ???????????return?getDataRoomService;?? ?
  • ???????}?? ?
  • ???}?
  • 定義需要數(shù)據(jù)綁定ViewModel:

  • public?class?RoomService_ViewModel?:INotifyPropertyChanged??? ?
  • ????{??? ?
  • ????????public?event?PropertyChangedEventHandler?PropertyChanged;??? ?
  • ????? ?
  • ????????public?ObservableCollection<Room>?serviceList=new?ObservableCollection<Room>();??? ?
  • ????????public?ObservableCollection<Room>?ServiceList??? ?
  • ????????{??? ?
  • ????????????get?{return?this.serviceList;?}??? ?
  • ????????????set?{this.serviceList?=?value;?}?? ?
  • ????????}?? ?
  • ??? ?
  • ????????public?void?SpiltHandleBaseDataList()?? ?
  • ????????{?? ?
  • ????????????if?(this.serviceList?!=?null)?? ?
  • ????????????{?? ?
  • ????????????????var?getresult?=?VipRoom_Service.GetRoomServiceDataRecord();?? ?
  • ????????????????if?(getresult.Count?>?0)?? ?
  • ????????????????{?? ?
  • ????????????????????foreach?(Room?getService?in?getresult)?? ?
  • ????????????????????????serviceList.Add(getService);?? ?
  • ????????????????}?? ?
  • ????????????}?? ?
  • ????????}?? ?
  • ?????????????? ?
  • ????}?
  • 在UI呈現(xiàn)中做數(shù)據(jù)綁定:

  • <Grid?x:Name="LayoutRoot"?Background="White"?Margin="30"?>??? ?
  • ???????<TextBlock?Text="This?Common?DataBind?About?Itemlist!"?FontSize="14"></TextBlock>??? ?
  • ???????<ListBox???HorizontalAlignment="Center"?VerticalAlignment="Center"??? ?
  • ??????????????????ItemsSource="{Binding?ServiceList}"???? ?
  • ??????????????????ScrollViewer.VerticalScrollBarVisibility="Visible"??? ?
  • ??????????????????Margin="167,47,172,112"?Width="401"?Height="181"></ListBox>??? ?
  • ??? ?
  • ???</Grid>?
  • 這種方式很簡單直接把需要所有服務(wù)主題提供了給了用戶.:

    但是系統(tǒng)需求發(fā)生變化后 我們需要在酒店服務(wù)系統(tǒng)需要提供訂飛機(jī)票服務(wù)時.你會發(fā)現(xiàn)這種方式呈現(xiàn)使我們的DataBinding的數(shù)據(jù)源發(fā)生變化.在代碼改動代價上前段UI和后臺數(shù)據(jù)綁定都需要修改.這讓人情何以堪.而今天提到Silverlight 5 Beta中隱式模板則徹底解決這個問題.

    當(dāng)我們在同一個數(shù)據(jù)加入N數(shù)據(jù)服務(wù)實體對象[存在FirstEntity/SecendEntity/ThreeEntit…N個] 因此我們需要要更加智能化.在數(shù)據(jù)源發(fā)生變動后.前段綁定數(shù)據(jù)UI能夠根據(jù)數(shù)據(jù)源來進(jìn)行自動識別綁定.而這就是Implicit Template隱式模板所做的工作.定義Template:

  • <UserControl.Resources>??? ?
  • ????????<DataTemplate?DataType="model:Room">??? ?
  • ????????????<TextBlock?Text="{Binding?RoomName}"?/>??? ?
  • ????????</DataTemplate>??? ?
  • ????????<DataTemplate?DataType="model:RoomService">??? ?
  • ????????????<TextBlock?Text="{Binding?ServiceName}"?/>??? ?
  • ????????</DataTemplate>??? ?
  • ????????<DataTemplate?DataType="model:RoomCommunication">??? ?
  • ????????????<TextBlock?Text="{Binding?CommSpyCounter}"?/>?? ?
  • ????????</DataTemplate>?? ?
  • ????</UserControl.Resources>?
  • 做了如上綁定.則無需修改后臺數(shù)據(jù)綁定.當(dāng)數(shù)據(jù)發(fā)生變更時依然在前臺UI會根據(jù)DataContent數(shù)據(jù)綁定服務(wù)實體對象[FirstEntity/SecondEntity/ThreeEntity]數(shù)據(jù)類型時則會使用定義DataTemplate模板中綁定相同數(shù)據(jù)類型的模板執(zhí)行.這種方式很方便解決與某個數(shù)據(jù)類型進(jìn)行直接關(guān)聯(lián)而非統(tǒng)一的DataContent.另外一個問題數(shù)據(jù)發(fā)生改變動態(tài)更新使用對應(yīng)DataTemplate.

    數(shù)據(jù)模板是很好的方式來規(guī)范應(yīng)用程序中數(shù)據(jù)綁定的格式。有了它們,可能會發(fā)現(xiàn)自己重新思考如何著手重用樣式和格式,可能使用更多的內(nèi)容較少的直接控制和應(yīng)用的樣式.

    轉(zhuǎn)載于:https://blog.51cto.com/chenkai/763591

    總結(jié)

    以上是生活随笔為你收集整理的Silverlight 5 Beta新特性[5]隐式模板支持的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。