2011-02-22 11 views
2

私はPRISM4 MVVM Patternを使用しており、ビューが正常に読み込まれ、アプリケーションの起動時に適切な領域に表示されます。アプリケーションが起動すると、ViewModelは、ビューがロードされると自動的に初期化されます。しかし、新しいタブ(新しい領域)に新しいビューを挿入しようとすると、新しいビューのViewModelは初期化されません。ここでビューの注入のためのコードは次のとおりです。ViewModelがビューインジェクション(WPF PRISM 4 MVVM)で初期化されていません

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 

    } 
} 

答えて

2

問題は、CompositionContainerで作成するのではなく、自分でビューを作成することです。 CompositionContainerは自分で作成したオブジェクトについて何も知らないので、new Views.ABCView()を呼び出すと、インポートは魔法のようには満足しません。

生のMEFでは、CompositionContainer.GetExports()を呼び出してコンテナからビューを取得します。プリズムにはおそらくこの呼び出しを囲むインフラストラクチャがありますが、プリズムについてはあまりよく分かりませんので、私はそれが何であるか正確にはわかりません。

+0

ダニエルありがとうございます。私はいくつかの研究をするつもりですが、解決策が見つかったらここに投稿します。喜んで –

+0

解決策は見つかりましたか? – JohnC

+0

@JohnC誰かがあなたのコメントを見る可能性を高めるために、私はあなたの名前でここでやっているように、あなたのコメントの中に名前の前に@を入れてください。 –

関連する問題