简体   繁体   English

多个视图绑定到多个ViewModels

[英]Multiple views binding to multiple ViewModels

I want to create multiple instances of a SearchTagView and bind each of those to its own SearchTagViewModel . 我想创建一个SearchTagView多个实例,并将每个实例绑定到其自己的SearchTagViewModel

I'm using the WAF framework for WPF which follows the MVVM-concept and furthermore Dependency injection (IoC). 我使用的是WPF的WAF框架,该框架遵循MVVM概念以及依赖注入(IoC)。 The application is developed for the SUR40 using the SurfaceSDK. 该应用程序是使用SurfaceSDK为SUR40开发的。

This means that the views are instantiated like this: 这意味着视图是这样实例化的:

TagVisualizationDefinition tagDefinition = new TagVisualizationDefinition();
tagDefinition.Source = new Uri("Views/SearchTagView.xaml", UriKind.Relative);
tagVisualizer.Definitions.Add(tagDefinition);

tagVisualizer is a control element of type TagVisualizer in SearchView . tagVisualizerSearchView TagVisualizer类型的控件元素。 So multiple SearchTagViews are placed in one SearchView . 因此,将多个SearchTagViews放在一个SearchView This works. 这可行。

The problem is that because of dependency injection all SearchTagViews use the same SearchTagViewModel : 问题是,由于依赖项注入, 所有 SearchTagViews 使用相同的 SearchTagViewModel

xmlns:vm="clr-namespace:Applications.ViewModels;assembly=Applications"

How can I use a different ViewModel for each View following the MVVM? 如何跟随MVVM的每个视图使用不同的ViewModel

Every IoC framework that I've seen allows you to register types in two ways: 我见过的每个IoC框架都允许您以两种方式注册类型:

  1. As a single instance (the same instance of the class is returned each time) 作为单个实例(每次都返回该类的相同实例)
  2. As a new instance each time it is resolved / injected 作为新实例,每次解决/注入

You'll need to figure out how to do #2 within your IoC framework. 您需要弄清楚如何在IoC框架中执行#2。

The other MVVM-centric option is to declare a DataTemplate in your UI and add the ViewModels directly to the UI and have WPF automatically create the Views for it. 另一个以MVVM为中心的选项是在您的UI中声明一个DataTemplate并将ViewModels直接添加到UI中,并使WPF自动为其创建视图。

edit: 编辑:

It looks like WAF is using MEF for IoC. 看起来WAF正在将MEF用于IoC。 You'll need to supply the [Export] attribute and specify it as non-shared. 您需要提供[Export]属性并将其指定为非共享。

Here's how to set a PartCreationPolicy with MEF from a previous question 以下是使用上一个问题的MEF设置PartCreationPolicy的方法

You can use a view model locator for this purpose. 您可以为此使用视图模型定位器。 Check out this . 看看这个

Configure the view model locator to return a new instance of view model every time. 配置视图模型定位器,使其每次都返回一个新的视图模型实例。

An example is given below, using mvvm light(even though you need not use mvvm light to use view model locator). 下面给出了一个使用mvvm light的示例(即使您不需要使用mvvm light来使用视图模型定位器)。

 public class ViewModelLocator
{
  public ViewModel1 VM1
        {
            get
            {
                return new ViewModel1();
            }
        }
}

In app.xaml, define a key for view model locator.If you nuget mvvmlight, this will happen automatically 在app.xaml中,为视图模型定位器定义一个键。如果您对mvvmlight进行nuget,它将自动发生

<Application.Resources>
    <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:WpfApplicationmvvmlight.ViewModel" />
  </Application.Resources>

In the view, use the locator 在视图中,使用定位器

<UserControl x:Class="WpfApplicationmvvmlight.View2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
              DataContext="{Binding Source={StaticResource Locator}, Path=VM1}">
</UserControl>

Alan's hint concerning the non-shared attribute was good, but I couldn't use it to solve my problem. 艾伦(Alan)关于非共享属性的暗示很好,但是我不能用它来解决我的问题。 The problem was that the MEF is working before I init my TagVisualizationDefinitions . 问题是MEF在我初始化TagVisualizationDefinitions之前就已经工作了。

The only solution was to set the binding in code-behind of the parent user control in the method for the event TagVisualization_Loaded 唯一的解决方案是在事件TagVisualization_Loaded的方法中的父用户控件的代码背后设置绑定。

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

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