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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Silverlight实用窍门系列:42.读取拖动到控件上的外部txt和jpg文件,多外部文件的拖动【附带实例源码】...

發布時間:2025/3/19 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Silverlight实用窍门系列:42.读取拖动到控件上的外部txt和jpg文件,多外部文件的拖动【附带实例源码】... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本實例將讀取拖動到Silverlight的ListBox控件中的txt文件或者Jpg文件。在本實例中將講如果通過UIelementA.Drop事件獲取到拖動到UIelementA上的文件的相關名稱以及路徑等信息,以處理多個外部文件拖動到Silverlight中的相關一些小技巧的應用和操作。

在本例中我們設置外部文件拖動到ListBox中去,首先我們要設置ListBox的AllowDrop="True",再添加一個Drop事件Drop="listBox1_Drop",這樣在外部文件拖動到ListBox中的時候可以觸發Drop事件。

首先我們來看MainPage.xaml代碼如下所示:

<Grid x:Name="LayoutRoot" Background="White" Width="600">

<StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Width="600"

Orientation="Horizontal">

<ListBox Name="listBox1" Background="AliceBlue" Width="240"

HorizontalAlignment="Left" VerticalAlignment="Top"

AllowDrop="True" Height="400" Drop="listBox1_Drop">

</ListBox>

<TextBlock Height="149" HorizontalAlignment="Left"

Name="textBlock1" VerticalAlignment="Top"

Width="323" TextWrapping="Wrap" />

</StackPanel>

<Image Height="238" Name="image1" HorizontalAlignment="Left"

VerticalAlignment="Top" Margin="240 160 0 0" Stretch="Fill"

Width="320" Source="/SLDragFile;component/Images/1_24573_f93ae69954e2e1d.jpg" />

</Grid>

在上面有一個TextBlock顯示讀取到的Txt文件內容,還有一個Image控件顯示讀取到的圖片內容。下面我們看MainPage.xaml.cs文件代碼如下:

using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Navigation; using System.IO; using System.Windows.Markup; using System.Windows.Media.Imaging;namespace SLDragFile {public partial class MainPage : UserControl{public MainPage(){InitializeComponent();}private void listBox1_Drop(object sender, DragEventArgs e){//獲取與拖放事件相關聯的元素IDataObject dataObject = e.Data as IDataObject;//返回被拖放的外部文件的 FileInfo 數組FileInfo[] files = dataObject.GetData(DataFormats.FileDrop) as FileInfo[];foreach (FileInfo file in files){//如果存在文件if (file.Exists){listBox1.Items.Add("文件名: " + file.Name);//如果是txt文件,讀取txt文件并且顯示出來if (file.Extension.ToLower() == ".txt"){StreamReader sreader = file.OpenText();string txtstr = "";string ReadStr = string.Empty;while ((txtstr = sreader.ReadLine()) != null){ReadStr += txtstr;}this.textBlock1.Text = ReadStr;}//如果是Jpg圖片,讀取圖片并且顯示出來if (file.Extension.ToLower() == ".jpg"){FileStream fs= file.OpenRead();BitmapImage image = new BitmapImage();image.SetSource(fs);image1.Source = image;}}else{listBox1.Items.Add("文件添加失敗!");}}}} }

下面我們來看看拖動一張jpg圖片文件的效果如下:

拖動一個UTF-8格式的txt文件的效果如下:

拖動多個文件到ListBox所出現的情況如下面三張圖片所示:

本文采用VS2010+Silverlight 4.0編寫,如需源碼請點擊 SLDragFile.zip 下載

總結

以上是生活随笔為你收集整理的Silverlight实用窍门系列:42.读取拖动到控件上的外部txt和jpg文件,多外部文件的拖动【附带实例源码】...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。