两种观点 - 一个视图模型 [英] Two views - one ViewModel

查看:137
本文介绍了两种观点 - 一个视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一定误解了的ViewModels 和视图的概念。但就在这个时刻,我不能重建地面应用,这一次做的更好。我的情况是,我有一个观点,用户可以将文件加载和阅读它们,绘图显示图形和一些操作来实现。我希望能够生成数据报表(如摘要),但我想它在其他视图。我使用 ModernUI ,这另一种观点是在另一个选项卡。

I must have misunderstood the concept of ViewModels and Views. But at this moment I can't rebuild the application from ground and this time doing it better. My situation is that I have a view where the user can load files and read them, a plotter shows the graphs and some operations are implemented. I want to be able to generate reports (like summary) of the data but I want it in other view. I'm using ModernUI, this other view is in another tab.

我要的是有两个选项卡同步的,当我在绘图仪选项卡加载一个文件,该文件必须在其他视图中装入了。为此,我想我需要的是观点相同的视图模型,在那里我有,例如结合 LoadedFiles =列表<文件> ,所以我就能够实现这一目标。现在的问题是,如果我绑定,要么

What I want is have that two tabs synchronized, when I load a file in the "plotter tab", the file must be loaded in the other view too. For that I think what I need is to bind the view to the same ViewModel, where I have for example LoadedFiles = List<File>, so I will be able to achieve it. The problem is that if I bind it either

MainViewModel vm = new MainViewModel();
DataContext = vm;

或XAML

<UserControl.Resources>
<UserControl.DataContext=local:MainViewModel/>
</UserControl.Resources>



实际上,我结合不同MainViewModels并且数据不再被共享。我需要从MVVM库的一些类,定位器等?怎么可以这样做?我可以在将来做什么,以便为每个视图,但是相同(或不同)的数据独立的ViewModels?

I'm actually binding to different MainViewModels and the data is not shared anymore. Do I need some classes from MVVM libraries such Locator and so? How this could be done? What can I do in the future in order to have separate ViewModels for each View but the same (or different) data?

推荐答案

你可以创建一个具有您LoadedFiles财产一个新的类,然后每一个独特的视图模型可以参考这个类。您可以使用多个视图模型之间的这些共享属性共享一个类。我的使用MVVMLight的定位与Autofac容器这一类注入我的每个视图模型(基本上使用控制和依赖注入的反转)的。

You could create a new class that has your LoadedFiles property and then each unique view model can reference this class. You can share the one class with these shared properties between multiple view models. I am using MVVMLight's Locator with an Autofac container to inject this class into each of my view models (basically using Inversion of Control and Dependency Injection).

您可以在控制和依赖注入这里

You can read up on Inversion of Control and Dependency Injection here.

一些示例代码 -

public MyClass
{
    public List<File> LoadedFiles{get; set;}
}

public ViewModelOne
{
    public MyClass MyClassInstance {get; set;}
    public ViewModelOne(MyClass myclass)
    {
        MyClassInstance = myclass
    }
}

public ViewModelTwo
{
    public MyClass MyClassInstance {get; set;}
    public ViewModelTwo(MyClass myclass)
    {
        MyClassInstance = myclass
    }
}

您也可以使用 。MVVMLight的地点每个视图的DataContext的设置为相应的视图

You could also use MVVMLight's Locator to set each View's DataContext to the appropriate View.

<UserControl x:Class="View1"             
             DataContext="{Binding ViewModel1, Source={StaticResource Locator}}"...>

这篇关于两种观点 - 一个视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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