亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Caliburn.Micro MVVM 從 WPF 中同一視圖上的另一個視圖模型更新列表

如何使用 Caliburn.Micro MVVM 從 WPF 中同一視圖上的另一個視圖模型更新列表

C#
蕭十郎 2023-09-16 15:15:29
我是 WPF 新手,我想使用 Caliburn Micro 遵循 MVVM 框架。我無法從另一個視圖模型中更新視圖模型中的列表。我有3個觀點:POSView :包含另外 2 個視圖的兩個內容控件產品視圖:所有產品的列表CartView :購物車中添加的所有產品的列表單擊產品視圖中的產品后,產品應添加到購物車視圖中POSViewModel.cspublic 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)        {        }    }我希望將商品添加到購物車。
查看完整描述

1 回答

?
婷婷同學_

TA貢獻1844條經驗 獲得超8個贊

您可以使用 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,您需要修改一些編碼


請參閱事件聚合器


單擊時,您在 Add 方法中執行 EventAggregator.publish(new Addevent)


在 PosviewModel 中你可以捕捉到事件......


但為此你必須修改一些代碼行,但閱讀鏈接并不復雜


查看完整回答
反對 回復 2023-09-16
  • 1 回答
  • 0 關注
  • 119 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號