【发布时间】:2015-07-13 04:24:36
【问题描述】:
我有一个文本框,其文本内容具有与视图模型的数据绑定。 我需要将 TextBox 中的不同字符串保存到一个集合中,但似乎它总是将最后一个当前 TextBox 的文本复制到所有项目中。虽然每次我都输入不同的文字。
这是 XAML 中的代码:
<TextBox HorizontalAlignment="Left"
Background="Transparent"
Margin="0,81,0,0"
TextWrapping="Wrap"
Text="{Binding Note.NoteTitle, Mode=TwoWay}"
VerticalAlignment="Top"
Height="50" Width="380"
Foreground="#FFB0AEAE" FontSize="26"/>
在视图模型中,我有:
public Note Note
{
get { return _note; }
set { Set(() => Note, ref _note, value); }
}
private ObservableCollection<Note> _notes;
public async void AddNote(Note note)
{
System.Diagnostics.Debug.WriteLine("AddNote Called...");
_notes.Add(note);
}
我的页面有一个按钮,点击后会调用AddNote。
有没有办法可以将不同的项目保存到 _notes 中?
编辑: 更多信息: AddNote 异步的原因是我需要在里面调用另一个任务来保存笔记数据:
private async Task saveNoteDataAsync()
{
var jsonSerializer = new DataContractJsonSerializer(typeof(ObservableCollection<Note>));
using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(fileName,
CreationCollisionOption.ReplaceExisting))
{
jsonSerializer.WriteObject(stream, _notes);
}
}
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。另请参阅 [帮助/标记]。
-
为什么你的 AddNote 方法是“异步”的?
标签: c# xaml data-binding windows-phone-8.1 observablecollection