Silverlight 4 beta アプリへ Drop されたテキストファイルを読み込む

DragEventArgs を true にして Drop イベントを拾ってごにょごにょするだけ。GetFormats() とかのあたりの使い方が正しいのかあまり自信がない。

MainPage.xaml

<UserControl x:Class="SilverlightApplication2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">

    <Grid x:Name="LayoutRoot" Background="Gray"
          Drop="LayoutRoot_Drop" AllowDrop="True">
    </Grid>
</UserControl>

MainPage.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.IO;

namespace SilverlightApplication2
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void LayoutRoot_Drop(object sender, DragEventArgs e)
        {
            foreach (string format in e.Data.GetFormats())
            {
                if (format != DataFormats.FileDrop) return;
                foreach (FileInfo file in (FileInfo[])e.Data.GetData(format))
                {
                    if (file.Extension != ".txt") continue;
                    MessageBox.Show(file.OpenText().ReadToEnd());
                }
            }
        }
    }
}

ちなみに FileInfo.OpenText() は UTF-8 としてテキストを読み込むらしい。