从 MainViewModel 调用或发送值到其他 ViewModel mvvm mvvm light
我们正在使用 mvvm light 框架来构建我们的应用程序。在我们的 ViewModel 文件夹中,根据应用程序的不同功能,我们有许多 Viewmodel,如 orderSupplyViewModel、HouseholdSupplyViewModel 等文件。我们通过将 MainViewModel 设置为分部类,将所有这些 viewModel 包含在 MainViewModel 中。所以你可以说我们有一个 MainViewModel。现在我们已经完成了应用程序的 40%,我们需要将视图模型分离为一个分部类。这样我们就可以从主视图模型中调用不同的视图模型。该怎么办呢?以下是与名为 - 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()
{
}
We are using mvvm light framework to build our application. In our ViewModel folder, we have number of Viewmodel like orderSupplyViewModel,HouseholdSupplyViewModel etc files as per the different functionality of the application. We have included all these viewModels in the MainViewModel by making the MainViewModel as partial class. So you can say we have one MainViewModel. Now we have completed 40% of the application and we need to separate the view Models as one partial class. So that we can call the different viewmodels from the mainviewModel. How to go about it? Following is the code realted to one viewmodel named - 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()
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该有一堆 MainViewModel 部分定义。您应该为每种类型的 ViewModel 创建单独 ViewModel 类。
在 Visual Studio、解决方案资源管理器、<您的项目> 中,右键单击 [ViewModel] 文件夹 ->添加->新项目... -> Mvvm Light ViewModel。这应该添加一个继承 ViewModelBase 的新 ViewModel 类。
You should NOT have a bunch of MainViewModel partial definitions. You should create individual ViewModel classes for each type of ViewModel.
In Visual Studio, Solution Explorer, <Your Project>, right-click on [ViewModel] folder -> Add -> New Item... -> Mvvm Light ViewModel. This should add a new ViewModel class which inherits ViewModelBase.
为了以解耦的方式进行通信,MVVM Light工具包提供了messenger类。
它还可以启动对话框并提供回调。非常方便非常轻非常有用。
Messenger 的描述如下:http://www.galasoft.ch/mvvm/getstarted/
To communicate in a decoupled way MVVM Light toolkit provides the messenger Class.
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/