简体   繁体   English

从MainViewModel调用值或将值发送到其他ViewModel mvvm mvvm light

[英]Calling or sending values from MainViewModel to other ViewModels mvvm mvvm light

We are using mvvm light framework to build our application. 我们正在使用mvvm light框架来构建我们的应用程序。 In our ViewModel folder, we have number of Viewmodel like orderSupplyViewModel,HouseholdSupplyViewModel etc files as per the different functionality of the application. 在我们的ViewModel文件夹中,根据应用程序的不同功能,我们有许多Viewmodel,例如orderSupplyViewModel,HouseholdSupplyViewModel等文件。 We have included all these viewModels in the MainViewModel by making the MainViewModel as partial class. 通过将MainViewModel设为部分类,我们将所有这些viewModels包括在MainViewModel中。 So you can say we have one MainViewModel. 所以可以说我们有一个MainViewModel。 Now we have completed 40% of the application and we need to separate the view Models as one partial class. 现在,我们已经完成了40%的应用程序,我们需要将视图模型作为一个局部类进行分离。 So that we can call the different viewmodels from the mainviewModel. 这样我们就可以从mainviewModel调用不同的viewmodel。 How to go about it? 怎么做呢? Following is the code realted to one viewmodel named - OrderSuppliesViewModel 以下是重新编码到一个名为-OrderSuppliesViewModel的视图模型的代码

namespace ParentalHealthClient.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// </summary>
    public partial class MainViewModel : ViewModelBase
    {
        private List<UserMedicalSupplyBO> _selectedFavouriteMedicalItems;
        private List<HouseholdItemsBO> _selectedFavouriteHouseHoldItems;
        private List<OrderSuppliesBO> _selectedOrderItems;
        private HouseholdSuppliesDAO _dataAccessForOrder;

        /// <summary>
        /// Initializes a new instance of the OrderSuppliesViewModel class.
        /// </summary>
        public void OrderSuppliesViewModel()
        {


        }

To communicate in a decoupled way MVVM Light toolkit provides the messenger Class. 为了以分离的方式进行通信,MVVM Light工具包提供了Messenger类。 It can also launch dialogs and provide callbacks. 它还可以启动对话框并提供回调。 Very handy very light very useful. 很方便很轻很实用。

The Messenger is discribed here: http://www.galasoft.ch/mvvm/getstarted/ Messenger在此处进行了描述: http ://www.galasoft.ch/mvvm/getstarted/

You should NOT have a bunch of MainViewModel partial definitions. 您不应该有一堆MainViewModel部分定义。 You should create individual ViewModel classes for each type of ViewModel. 您应该为每种ViewModel类型创建单独的 ViewModel类。

In Visual Studio, Solution Explorer, <Your Project>, right-click on [ViewModel] folder -> Add -> New Item... -> Mvvm Light ViewModel. 在Visual Studio解决方案资源管理器的<您的项目>中,右键单击[ViewModel]文件夹->添加->新建项...-> Mvvm Light ViewModel。 This should add a new ViewModel class which inherits ViewModelBase. 这应该添加一个新的ViewModel类,该类继承ViewModelBase。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM