如何使我的 DataService 与 ObservableCollection 保持同步?

发布于 2024-08-31 10:42:45 字数 1737 浏览 1 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泡沫很甜 2024-09-07 10:42:45

处理“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文