我有一个文本框,其中的文本内容具有数据绑定到视图模型。 我需要将不同的字符串从TextBox保存到一个集合,但似乎总是将最后一个当前的TextBox文本复制到所有项目。虽然每次我输入不同的文字。将项目添加到ObservableCollection
这里是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);
}
}
我编辑了你的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。另见[帮助/标记]。 –
为什么你的方法AddNote是“异步”? –