将项目添加到 ObservableCollection
Add Item to ObservableCollection
我有一个 TextBox,其中的文本内容具有与视图模型的数据绑定。
我需要将不同的字符串从 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);
}
}
您正在尝试一次又一次地推送同一个 Note 对象实例。 AddNote 命令应该只接受字符串参数并在添加之前创建一个新笔记。
<TextBox HorizontalAlignment="Left"
Background="Transparent"
Margin="0,81,0,0"
TextWrapping="Wrap"
Text="{Binding NoteTitle, Mode=TwoWay}"
VerticalAlignment="Top"
Height="50" Width="380"
Foreground="#FFB0AEAE" FontSize="26"/>
private string _NoteTitle;
public string NoteTitle
{
get { return _NoteTitle; }
set { _NoteTitle= value; }
}
private ObservableCollection<Note> _notes;
public async void AddNote(string NoteName)
{
System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
{
System.Diagnostics.Debug.WriteLine("AddNote Called...");
_notes.Add(new Note() {NoteTitle = NoteName});
});
// your async calls etc.
}
我有一个 TextBox,其中的文本内容具有与视图模型的数据绑定。 我需要将不同的字符串从 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);
}
}
您正在尝试一次又一次地推送同一个 Note 对象实例。 AddNote 命令应该只接受字符串参数并在添加之前创建一个新笔记。
<TextBox HorizontalAlignment="Left"
Background="Transparent"
Margin="0,81,0,0"
TextWrapping="Wrap"
Text="{Binding NoteTitle, Mode=TwoWay}"
VerticalAlignment="Top"
Height="50" Width="380"
Foreground="#FFB0AEAE" FontSize="26"/>
private string _NoteTitle;
public string NoteTitle
{
get { return _NoteTitle; }
set { _NoteTitle= value; }
}
private ObservableCollection<Note> _notes;
public async void AddNote(string NoteName)
{
System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
{
System.Diagnostics.Debug.WriteLine("AddNote Called...");
_notes.Add(new Note() {NoteTitle = NoteName});
});
// your async calls etc.
}