在访问MVVM光在其他的ViewModels属性 [英] Accessing Properties in other ViewModels in MVVM Light

查看:150
本文介绍了在访问MVVM光在其他的ViewModels属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主视图模型包含我使用一定量用户控件的其中一个项目列表,这是在主视图显示在 ContentControl中。我当前的交换之间的数据的方式的ViewModels 存在的主要决策参考,每个的ViewModels 视图模型,并在每用户控件视图模型之一。不过,我不知道的是,这是这样做的正确的方式,因为我有一个 ViewModelLocator 和我种希望这个类有一些功能来支持这样的事情。



谁能告诉我,如果我在做什么这行,或者如果在MVVM光这样做呢?

$ b $更好的办法b

编辑:



我发现这个时候我正在寻找不同的东西,这会是一个更好的解决方案。

 私人ViewModelLocator定位器=新ViewModelLocator(); 



,然后使用定位器属性引用每个ViewModel的?



EDIT2:



显然,我以为会工作没有,一开始我还只在主视图模型和这个工作,但是当我尝试这在用户控件它种崩溃我的应用程序。目前,我想要首先编辑的可能的解决方案。



MainViewModel.cs(其他都是相似的,仅供参考主视图模型)

 公共类MainViewModel:ViewModelBase 
{
私人itemAddViewModel itemAddViewModel;
私人ItemsViewModel itemsViewModel;

///<总结>
///初始化MainViewModel类的新实例。
///< /总结>
公共MainViewModel()
{
ItemsList = Item.GetItemsList();

itemAddViewModel = ServiceLocator.Current.GetInstance< ItemAddViewModel>();
itemsViewModel = ServiceLocator.Current.GetInstance< ItemsViewModel>();

ShowItemsView();
}

私人无效ShowItemsView()
{
CurrentControl = itemsViewModel;
}
...


解决方案

你可以实际使用的ViewModelLocator。 Defaultly是采用控制容器的反转,所以即使你创建定位器的新实例,你会得到从容器单身的ViewModels的同一个实例。



Locator类:

 静态ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(()=> SimpleIoc 。默认);
SimpleIoc.Default.Register< ViewModel1>();
SimpleIoc.Default.Register< ViewModel2>();
SimpleIoc.Default.Register< ViewModel3>();
}

公共ViewModel1 ViewModel1
{
得到
{
返回ServiceLocator.Current.GetInstance< ViewModel1>();
}
}

公共ViewModel2 ViewModel2
{
得到
{
返回ServiceLocator.Current.GetInstance< ViewModel2>( );
}
}

公共ViewModel3 ViewModel3
{
得到
{
返回ServiceLocator.Current.GetInstance< ViewModel3>( );
}
}



然后从代码,你可以访问它。



  VAR VM1 =(新ViewModelLocator())ViewModel1。 



你得到一个和视图模型的唯一实例。



在XAML:
资源(defaultly是在App.xaml中Application.Resources)

 < VM:ViewModelLocator X:键=定位器D:IsDataSource =真/> 

和DataContext的用于视图(用户控件或窗口或其他)

 <用户控件
...
的DataContext ={结合ViewModel1源= {静态资源定位符}
。 ..>


I have a main ViewModel containing a List of items which I'm using in a certain amount of UserControls, which are displayed in a ContentControl on the main view. My current way of exchanging data between the ViewModels exists of making a reference to each of the ViewModels in the main ViewModel, and one of the main ViewModel in every UserControl. However I do not know is this is the right way of doing this, since I have a ViewModelLocator and I kind of expect this class to have some functionality to support something like this.

Can anyone please tell me if what I'm doing this OK, or if there is a better way of doing this in MVVM Light?

EDIT:

I found this when I was searching for something different, would this be a better solution?

private ViewModelLocator locator = new ViewModelLocator();

And then using the locator Properties to reference each ViewModel?

EDIT2:

Apparently what I thought would work doesn't, at first I had only references in the main ViewModel and this worked, but when I try this in the UserControls it kind of crashes my app. I'm currently trying the possible solution of first edit.

MainViewModel.cs (others are similar, with reference only to the main ViewModel)

public class MainViewModel : ViewModelBase
{
    private ItemAddViewModel itemAddViewModel;
    private ItemsViewModel itemsViewModel;

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        ItemsList = Item.GetItemsList();

        itemAddViewModel = ServiceLocator.Current.GetInstance<ItemAddViewModel>();
        itemsViewModel = ServiceLocator.Current.GetInstance<ItemsViewModel>();

        ShowItemsView();
    }
...
    private void ShowItemsView()
    {
        CurrentControl = itemsViewModel;
    }
...

解决方案

You can actually use the ViewModelLocator. Defaultly is uses the Inversion of Control Container, so even if you create a new instance of the Locator, you will get the same instance of singleton viewmodels from the container.

The Locator class:

static ViewModelLocator()
{
    ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
    SimpleIoc.Default.Register<ViewModel1>();
    SimpleIoc.Default.Register<ViewModel2>();
    SimpleIoc.Default.Register<ViewModel3>();
}

public ViewModel1 ViewModel1
{
    get
    {
        return ServiceLocator.Current.GetInstance<ViewModel1>();
    }
}

public ViewModel2 ViewModel2
{
    get
    {
        return ServiceLocator.Current.GetInstance<ViewModel2>();
    }
}

public ViewModel3 ViewModel3
{
    get
    {
        return ServiceLocator.Current.GetInstance<ViewModel3>();
    }
}

then from code you can access it as

var vm1 = (new ViewModelLocator ()).ViewModel1;

you get the one and only instance of viewmodel.

from xaml: Resources (defaultly is in Application.Resources in App.xaml)

<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

and DataContext for views (either user controls or windows or whatever)

<UserControl
    ... 
    DataContext="{Binding ViewModel1, Source={StaticResource Locator}}"
    ...    >

这篇关于在访问MVVM光在其他的ViewModels属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆