Created
April 15, 2012 03:16
-
-
Save biac/2389711 to your computer and use it in GitHub Desktop.
Demo1Page.OpenTxtButton_Click_1()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async void OpenTxtButton_Click_1(object sender, RoutedEventArgs e) | |
{ | |
var thisButton = sender as Button; | |
thisButton.IsEnabled = false; | |
// FileOpenPicker を用意する | |
var picker = new FileOpenPicker(); | |
picker.CommitButtonText = "テキストファイルを開く"; | |
picker.FileTypeFilter.Add(".txt"); // 1つは必須!! | |
picker.FileTypeFilter.Add(".cs"); | |
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; | |
picker.ViewMode = PickerViewMode.List; | |
// FileOpenPicker を出して、ファイルを選択してもらう 【非同期!】 | |
StorageFile file = await picker.PickSingleFileAsync(); | |
// 選択されたファイルの内容を TextBox に読み込む | |
if (file != null) { | |
using(Stream s = await file.OpenStreamForReadAsync()) // 選択してもらった StorageFile から Stream を作る 【非同期!】 | |
using(StreamReader r = new StreamReader(s)) // Stream から StreamReader を作る | |
this.TextArea.Text = await r.ReadToEndAsync(); // StreamReader にファイルを尻尾まで読み込んでもらう 【非同期!】 | |
} | |
thisButton.IsEnabled = true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment