天天看點

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圖檔檔案的效果如下:

<a target="_blank" href="http://blog.51cto.com/attachment/201204/223343557.jpg"></a>

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

<a target="_blank" href="http://blog.51cto.com/attachment/201204/223215637.jpg"></a>

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

<a target="_blank" href="http://blog.51cto.com/attachment/201204/223126185.jpg"></a>

本文轉自程興亮 51CTO部落格,原文連結:http://blog.51cto.com/chengxingliang/826451

繼續閱讀