[英]Windows Phone 7 Listbox
我在Windows Phone 7中有一個列表框,我想在列表框中顯示“隔離存儲”文件的列表,當我從列表框中選擇文件時,我應該能夠從文件中獲取內容。
這是我的代碼:
ListBoxItem lbi = (ListBoxItem)listBox1.SelectedItem;
string t = (string)lbi.Content;
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
using (var sr = new StreamReader(store.OpenFile(t, FileMode.Open, FileAccess.Read)))
{
textBlock2.Text = sr.ReadToEnd();
}
並將文件綁定到列表:
var appstorage = IsolatedStorageFile.GetUserStoreForApplication();
string[] filename = appstorage.GetFileNames();
listBox1.ItemsSource = filename;
但是,當我嘗試應用程序時,在SelectionChanged
事件上我得到一個錯誤:
ListBoxItem lbi = (ListBoxItem)listBox1.SelectedItem;
無效的ListBoxItem lbi = (ListBoxItem)listBox1.SelectedItem;
異常ListBoxItem lbi = (ListBoxItem)listBox1.SelectedItem;
現在,最終的問題是,當激發SelectionChanged
事件時,如何在列表框中檢索確切的文件名,以便流讀取器可以使用該文件名?
要綁定到IEnumerable<string>
( string[]
列表框不轉換這些項目中ListBoxItem
的他們磨弦的只是普通的舊運行。
因此,您應該執行以下操作:
string t = (string)listBox1.SelectedItem;
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
using (var sr = new StreamReader(store.OpenFile(t, FileMode.Open, FileAccess.Read)))
{
textBlock2.Text = sr.ReadToEnd();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.