如何使用 Caliburn.Micro MVVM 从 WPF 中同一视图的另一个视图模型更新列表<>?

How to update list<> from another View model on same View in WPF using Caliburn.Micro MVVM?

我是 WPF 的新手,我想使用 Caliburn Micro 遵循 MVVM 框架。我在从另一个视图模型更新列表时遇到问题。

我有 3 个浏览量:

在产品视图中点击产品,产品应该被添加到购物车视图中

POSViewModel.cs

public class POSViewModel : Conductor<object>.Collection.AllActive
    {
        #region Private Variables
        private ProductsViewModel _ProductsViewModel;
        private CartViewModel _CartViewModel;
        #endregion

        #region Public Variables
        public ProductsViewModel ProductsViewModel
        {
            get { return _ProductsViewModel; }
            set { _ProductsViewModel = value; }
        }

        public CartViewModel CartViewModel
        {
            get { return _CartViewModel; }
            set { _CartViewModel = value; }
        }
        #endregion

        #region Public Methods
        public POSViewModel()
        {
            ProductsViewModel = new ProductsViewModel();
            CartViewModel = new CartViewModel();
        }
        #endregion
    }

ProductsViewModel.cs: 在 AddProdClick(ProductModel productModel) 上,我想将点击的产品添加到 CartView。

public class ProductsViewModel : Conductor<object>
    {
        public BindableCollection<ProductModel> Products { get; set; }
        public ProductsViewModel()
        {
            Products = new BindableCollection<ProductModel>();
            for (int i = 0; i < 25; i++)
            {
                Products.Add(new ProductModel
                {
                    ProductName = "Product" + i.ToString(),
                    Qty = i + 2,
                    Rate = i * 10
                }); ;
            }

        }

        public void AddProdClick(ProductModel productModel)
        {

        }

    }

ProductView.xaml:它是一个具有产品列表的用户控件。带有产品的按钮绑定到视图模型中的 AddProdClick。

 <Button Content="Add To Cart" cal:Message.Attach="AddProdClick($datacontext)" />

CartViewModel.cs

public class CartViewModel : Conductor<object>
    {
        private BindableCollection<ProductModel> _CartProducts;
        public BindableCollection<ProductModel> CartProducts
        {
            get
            {
                return _CartProducts;
            }
            set
            {
                NotifyOfPropertyChange(() => CartProducts);
                _CartProducts = value;
            }
        }

        public CartViewModel()
        {
            CartProducts = new BindableCollection<ProductModel>();
        }
    }

我希望将商品添加到购物车。

您可以使用 CartViewModel 作为参数:

    public POSViewModel()
    {   
        CartViewModel = new CartViewModel();    
        ProductsViewModel = new ProductsViewModel(CartViewModel);
    }

并在 ProductsViewModel 的构造函数中使用它

public class ProductsViewModel : Conductor<object>
{
    public BindableCollection<ProductModel> Products { get; set; }
    public CartViewModel CVM { get; set; }
    public ProductsViewModel(CartViewModel CVM)
    {
                    this.CVM = CVM;
    }

    public void AddProdClick(ProductModel productModel)
    {
                    CVM.Add(productModel)
    }
}

您有另一个解决方案:使用 PosViewModel:

 public POSViewModel()
{   
    CartViewModel = new CartViewModel();    
    ProductsViewModel = new ProductsViewModel(this);
}

public class ProductsViewModel : Conductor<object>
{
    public BindableCollection<ProductModel> Products { get; set; }
    public CartViewModel CVM { get; set; }
    public ProductsViewModel(POSViewModel PVM)
    {
                    this.CVM = PVM.CartViewModel;
    }

    public void AddProdClick(ProductModel productModel)
    {
                    CVM.Add(productModel)
    }
}

第三种解决方案是使用 EventAggregator 你需要修改一些代码

EventAggregator

当你点击的时候,你在Add方法中做EventAggregator.publish(new Addevent)

并且在 PosviewModel 中您捕捉到事件...

但是为此你必须修改一些代码行,但是阅读 link 它并不复杂