ViewModel未在View Injection(WPF PRISM 4 MVVM)上初始化

时间:2011-02-22 04:52:09

标签: wpf mvvm prism mef prism-4

我正在使用PRISM4 MVVM Pattern,并且视图会在应用程序启动时成功加载并显示在相应的区域上。应用程序启动时,ViewModel会在加载视图时自动初始化。但是,如果我尝试在新选项卡(新区域)上注入新视图,则不会初始化新视图的ViewModel。这是视图注入的代码:

IRegion region = regionManager.Regions["RegionNameGoesHere"];

var pane = new Views.ABCView() {Tag = id};
regionManager.Regions["RegionNameGoesHere"].Add(pane);

上面的代码打开一个新选项卡并加载新视图,但它不会初始化ViewModel。控件的每个选项卡都是一个新区域(选项卡控件有一个RegionAdapter)。

以下是视图背后的代码:

using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Docking;

namespace Company.Application.Module.Assembly.Views
{
    [Infrastructure.Behaviours.ViewExport("ABCView")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class ABCView : RadPane
    {
        public ABCView()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Sets the ViewModel.
        /// </summary>
        /// <remarks>
        /// This set-only property is annotated with the <see cref="ImportAttribute"/> so it is injected by MEF with
        /// the appropriate view model.
        /// </remarks>
        [Import]
        [SuppressMessage("Microsoft.Design", "CA1044:PropertiesShouldNotBeWriteOnly", Justification = "Needs to be a property to be composed by MEF")]
        ABCViewModel ViewModel
        {
            set
            {
                this.Decorator.DataContext = value;
                //this.DataContext = value;
            }
        }
    }
}

这是具有一些属性和事件的ViewModel。我在上面的代码中遗漏了一些东西来初始化下面的ViewModel。任何建议都非常感谢。

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Collections.Specialized;
 using System.ComponentModel.Composition;
 using System.Linq;
 using System.ServiceModel;
 using System.ServiceModel.Description;
 using System.Text;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Xml;
 using System.Xml.XPath;
 using System.Windows.Data;

 using Microsoft.Practices.Prism.Commands;
 using Microsoft.Practices.Prism.Events;
 using Microsoft.Practices.Prism.Regions;
 using Microsoft.Practices.Prism.ViewModel;

 namespace Company.Application.Module.Assembly.Views
 {
     [Export(typeof(ABCViewModel))]
     [PartCreationPolicy(CreationPolicy.NonShared)]
     public class ABCViewModel : NotificationObject
     {
         private readonly IRegionManager regionManager;

         [ImportingConstructor]
         public ABCViewModel(IRegionManager regionManager)
         {
             // Event Aggregator
             //this.eventAggregator = eventAggregator;

             // Region Manager
             this.regionManager = regionManager;

         }

         #region P R O P E R T I E S

         #region E V E N T S 

     }
 }

1 个答案:

答案 0 :(得分:2)

问题是您自己创建视图而不是让CompositionContainer为您创建视图。 CompositionContainer对您自己创建的对象一无所知,因此当您调用new Views.ABCView()时,导入不会神奇地满足。

使用原始MEF,您可以调用CompositionContainer.GetExports()从容器中获取视图。 Prism中可能有一些基础设施包围了这个电话,但我对Prism知之甚少,所以我不确切知道它会是什么。