第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

C#
蕭十郎 2023-09-16 15:15:29
我是 WPF 新手,我想使用 Caliburn Micro 遵循 MVVM 框架。我無法從另一個視圖模型中更新視圖模型中的列表。我有3個觀點(diǎn):POSView :包含另外 2 個視圖的兩個內(nèi)容控件產(chǎn)品視圖:所有產(chǎn)品的列表CartView :購物車中添加的所有產(chǎn)品的列表單擊產(chǎn)品視圖中的產(chǎn)品后,產(chǎn)品應(yīng)添加到購物車視圖中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) 上,我想將單擊的產(chǎn)品添加到 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 回答

?
婷婷同學(xué)_

TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個贊

您可以使用 CartViewModel 作為參數(shù):


    public POSViewModel()

    {   

        CartViewModel = new CartViewModel();    

        ProductsViewModel = new ProductsViewModel(CartViewModel);

    }

并在 ProductsViewModel 的構(gòu)造函數(shù)中使用它


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


請參閱事件聚合器


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


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


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


查看完整回答
反對 回復(fù) 2023-09-16
  • 1 回答
  • 0 關(guān)注
  • 116 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號