如何使我的 DataService 与 ObservableCollection 保持同步?
我有一个名为 CustomerService 的类,它只是从文件中读取客户集合或创建一个客户集合并将其传递回主模型视图,在主模型视图中将其转换为 ObservableCollection。确保 CustomerService 和 ObservableCollection 中的项目同步的最佳实践是什么。我猜我可以连接 CustomerService 对象来响应 RaisePropertyChanged,但这不是只能与 WPF 控件一起使用吗?有更好的办法吗?
using System;
public class MainModelView
{
public MainModelView()
{
_customers = new ObservableCollection<CustomerViewModel>(new CustomerService().GetCustomers());
}
public const string CustomersPropertyName = "Customers"
private ObservableCollection<CustomerViewModel> _customers;
public ObservableCollection<CustomerViewModel> Customers
{
get
{
return _customers;
}
set
{
if (_customers == value)
{
return;
}
var oldValue = _customers;
_customers = value;
// Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
RaisePropertyChanged(CustomersPropertyName, oldValue, value, true);
}
}
}
public class CustomerService
{
/// <summary>
/// Load all persons from file on disk.
/// </summary>
_customers = new List<CustomerViewModel>
{
new CustomerViewModel(new Customer("Bob", "" )),
new CustomerViewModel(new Customer("Bob 2", "" )),
new CustomerViewModel(new Customer("Bob 3", "" )),
};
public IEnumerable<LinkViewModel> GetCustomers()
{
return _customers;
}
}
I have a class called CustomerService which simply reads a collection of customers from a file or creates one and passes it back to the Main Model View where it is turned into an ObservableCollection. What the best practice for making sure the items in the CustomerService and ObservableCollection are in sync. I'm guessing I could hookup the CustomerService object to respond to RaisePropertyChanged, but isn't this only for use with WPF controls? Is there a better way?
using System;
public class MainModelView
{
public MainModelView()
{
_customers = new ObservableCollection<CustomerViewModel>(new CustomerService().GetCustomers());
}
public const string CustomersPropertyName = "Customers"
private ObservableCollection<CustomerViewModel> _customers;
public ObservableCollection<CustomerViewModel> Customers
{
get
{
return _customers;
}
set
{
if (_customers == value)
{
return;
}
var oldValue = _customers;
_customers = value;
// Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
RaisePropertyChanged(CustomersPropertyName, oldValue, value, true);
}
}
}
public class CustomerService
{
/// <summary>
/// Load all persons from file on disk.
/// </summary>
_customers = new List<CustomerViewModel>
{
new CustomerViewModel(new Customer("Bob", "" )),
new CustomerViewModel(new Customer("Bob 2", "" )),
new CustomerViewModel(new Customer("Bob 3", "" )),
};
public IEnumerable<LinkViewModel> GetCustomers()
{
return _customers;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
处理“Customers”的CollectionChanged 事件。当它发生变化时,请致电您的服务以保持同步。
绑定“客户”时,请确保在 xaml 中指定“Mode=TwoWay”。
Handle the CollectionChanged event of "Customers". When it changes, call your service to keep it synced.
When binding your "Customers" make sure you specify "Mode=TwoWay" in your xaml.