生活随笔
收集整理的這篇文章主要介紹了
Silverlight 4 新特性之Silverlight as Drop Target
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在上次項(xiàng)目中寫過多篇關(guān)于拖拽的實(shí)現(xiàn). 這些拖拽都是控件之間的效果. Silverlight 4 中我們甚至可以直接把文件系統(tǒng)中文件拖拽到Silverlight Application中承載. 這就是 silverlight 4中新特性Silverlight As Drop Target 支持這一點(diǎn). 為了達(dá)到演示目的. 使用桌面圖片拖拽到Silverlight Application中ScrollViewer動(dòng)態(tài)顯示. 先體驗(yàn)一下[你可以嘗試從本地文件系統(tǒng)直接拖拽圖片到這個(gè)Silverlight Application中看一下效果]:
實(shí)現(xiàn)步驟:
<1>: 頁(yè)面布局
<StackPanel?x:Name="LayoutRoot"?Background="White"> ? ?????????<TextBlock?Text="??"></TextBlock> ? ?????????<TextBlock?Text="Silverlight?AS?Drop?target.-chenkai[10.6.28]"?VerticalAlignment="Center"?HorizontalAlignment="Left"?FontSize="16"?Foreground="Red"??FontFamily="Comic?Sans?MS"?FontWeight="BOld"?Height="25"?Width="655"?/> ? ?????????<ScrollViewer?x:Name="ImagesTarget"?Background="White"??Height="360"?BorderBrush="Red"? ???????????????????????VerticalScrollBarVisibility="Hidden"?HorizontalScrollBarVisibility="Auto"?AllowDrop="True"> ? ?????????????????<ItemsControl?x:Name="ImageList"?Height="353"> ? ?????????????????????<!? ?????????????????????<ItemsControl.ItemTemplate> ??????????????????????????<DataTemplate> ? ?????????????????????????????<Image?Source="{Binding}"?Margin="5"?Stretch="UniformToFill"?Height="240"?/> ? ?????????????????????????</DataTemplate> ??????????????????????</ItemsControl.ItemTemplate> ? ?????????????????????<!? ?????????????????????<ItemsControl.ItemsPanel> ??????????????????????????<ItemsPanelTemplate> ? ?????????????????????????????<StackPanel?Orientation="Horizontal"?VerticalAlignment="Center"?HorizontalAlignment="Center"/> ? ?????????????????????????</ItemsPanelTemplate> ??????????????????????</ItemsControl.ItemsPanel> ??????????????????</ItemsControl> ??????????????</ScrollViewer> ??????</StackPanel>?<2>:后臺(tái)實(shí)現(xiàn)代碼
//定義存儲(chǔ)Image集合. ??????????ObservableCollection<BitmapImage>?_images?=?new?ObservableCollection<BitmapImage>(); ?? ? ?????????public?MainPage() ? ?????????{ ??????????????InitializeComponent(); ??????????????this.Loaded?+=?new?RoutedEventHandler(MainPage_Loaded); ??????????} ?? ??????????void?MainPage_Loaded(object?sender,?RoutedEventArgs?e) ??????????{ ??????????????//如果Image數(shù)據(jù)則直接加載進(jìn)來. ??????????????ImageList.ItemsSource?=?_images; ? ?????????????ImagesTarget.Drop?+=?new?DragEventHandler(ImagesTarget_Drop); ? ?????????} ??????????void?ImagesTarget_Drop(object?sender,?DragEventArgs?e) ??????????{ ??????????????//判斷拖拽數(shù)據(jù)是否存在 ? ?????????????if?(e.Data?==?null) ? ?????????????{ ? ?????????????????return; ? ?????????????} ? ?????????????else? ?????????????{ ??????????????????//利用Fileinfo?來初始化關(guān)于文件系統(tǒng)日常操作io對(duì)象?Fileinfo?【】數(shù)組?意味支持多張Image同時(shí)拖拽Silverlight?Application ? ?????????????????IDataObject?dataObject?=?e.Data?as?IDataObject; ? ?????????????????FileInfo[]?files?=dataObject.GetData(DataFormats.FileDrop)?as?FileInfo[]; ? ? ? ?????????????????foreach?(FileInfo?file?in?files) ? ?????????????????{ ??????????????????????try ??????????????????????{ ??????????????????????????using?(var?stream?=?file.OpenRead()) ??????????????????????????{ ??????????????????????????????//讀取拖拽中圖片源. ??????????????????????????????var?imageSource?=?new?BitmapImage(); ??????????????????????????????imageSource.SetSource(stream); ??????????????????????????????//添加到集合中. ? ?????????????????????????????_images.Add(imageSource); ? ?????????????????????????} ??????????????????????} ??????????????????????catch?(Exception) ??????????????????????{ ? ?????????????????????????MessageBox.Show("Not?a?suppoted?image."); ? ?????????????????????} ??????????????????} ??????????????} ??????????} ?因?yàn)榍芭_(tái)ScrollView中DataTemplate中定義格式是Image綁定. 后臺(tái)數(shù)據(jù)源用到ObservableCollection<BitmapImage>來封裝從拖拽中得到圖片數(shù)據(jù). 另外一個(gè)就是FileInfo,提供創(chuàng)建、復(fù)制、刪除、移動(dòng)和打開文件的實(shí)例方法,并且?guī)椭鷦?chuàng)建?FileStream?對(duì)象, 既然通過Fileinfo得到FileStream對(duì)象 其他操作就是平常IO操作. 而Fileinfo[]數(shù)組則是用來支持同時(shí)拖拽多個(gè)對(duì)象.關(guān)于Fileinfo 更多詳細(xì)資料請(qǐng)參見MSDN.
本文轉(zhuǎn)自chenkaiunion 51CTO博客,原文鏈接:http://blog.51cto.com/chenkai/764886
總結(jié)
以上是生活随笔為你收集整理的Silverlight 4 新特性之Silverlight as Drop Target的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。