C# 在主页上将文件内容显示为标题,而不是文件名

C# 在主页上将文件内容显示为标题,而不是文件名,c#,windows-phone-8,listbox,isolatedstorage,mvvm,C#,Windows Phone 8,Listbox,Isolatedstorage,Mvvm,我想创建一个应用程序,其中你有一个显示所有注释的主页,你可以创建一个新注释或从列表中选择注释 现在,它显示所有文件名,如Sample1.txt、Sample2.txt 我希望它像这样显示: // put this in your view class private List<Note> _notes; protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {

我想创建一个应用程序,其中你有一个显示所有注释的主页,你可以创建一个新注释或从列表中选择注释

现在,它显示所有文件名,如Sample1.txt、Sample2.txt

我希望它像这样显示:

// put this in your view class
private List<Note> _notes;

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
{ 
    _notes = new List<Note>();
    var filesNames = store.GetFileNames();
    foreach (var fileName in fileNames)
    {
        using (var sr = new StreamReader(new IsolatedStorageFileStream(fileName, FileMode.Open, isf)))
        {
            var note = new Note();
            note.FileName = fileName;
            note.Title = sr.ReadLine();
            note.Body = sr.ReadToEnd();
            _notes.Add(note);
        }
    }
    this.NotesListBox.ItemsSource = _notes;
} 

这是一款很棒的应用程序。 提醒我买牛奶 不是这样的:

// put this in your view class
private List<Note> _notes;

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
{ 
    _notes = new List<Note>();
    var filesNames = store.GetFileNames();
    foreach (var fileName in fileNames)
    {
        using (var sr = new StreamReader(new IsolatedStorageFileStream(fileName, FileMode.Open, isf)))
        {
            var note = new Note();
            note.FileName = fileName;
            note.Title = sr.ReadLine();
            note.Body = sr.ReadToEnd();
            _notes.Add(note);
        }
    }
    this.NotesListBox.ItemsSource = _notes;
} 

Sample1.txt Sample2.txt Sample3.txt Sample4.txt 以下是显示列表的代码:

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
        using (var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication()) 
        { 
            this.NotesListBox.ItemsSource = store.GetFileNames(); 
        } 
    } 
这是主xaml上的绑定

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox x:Name="NotesListBox" SelectionChanged="Notes_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBlock Text="{Binding}" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>


我很想知道这件事。。谢谢

非常快且非常脏的解决方案是更换

this.NotesListBox.ItemsSource = store.GetFileNames(); 

您可以使用此模型加载所有注释并将其放入列表框,如下所示:

// put this in your view class
private List<Note> _notes;

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
{ 
    _notes = new List<Note>();
    var filesNames = store.GetFileNames();
    foreach (var fileName in fileNames)
    {
        using (var sr = new StreamReader(new IsolatedStorageFileStream(fileName, FileMode.Open, isf)))
        {
            var note = new Note();
            note.FileName = fileName;
            note.Title = sr.ReadLine();
            note.Body = sr.ReadToEnd();
            _notes.Add(note);
        }
    }
    this.NotesListBox.ItemsSource = _notes;
} 
更好的解决办法是使用。但这可能是因为现在的压力太大了


祝你好运

那文本从哪里来?它存储在便笺中吗?它存储在每个文本文件中的独立存储中。但是标题从何而来?文本文件只是文本数据。除非你在文件格式中引入了一个标题,否则没有“标题”。我想从文件中获取注释第一行的标题。好的,我会尝试一下,今晚与你联系。谢谢这就是我如何使用(var-stream=new-IsolatedStorageFileStream(tbxFilename.Text,FileMode.Create,FileAccess.Write,store)){StreamWriter=new-StreamWriter(stream);writer.Write(tbxNote.Text);writer.Close();}
<TextBox Text="{Binding}"/>
<TextBox Text="{Binding Title}"/>
private void Notes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    foreach (Note note in e.AddedItems)
    {
        MessageBox.Show(note.Body);
    }
}