私は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
}
}
ダニエルありがとうございます。私はいくつかの研究をするつもりですが、解決策が見つかったらここに投稿します。喜んで –
解決策は見つかりましたか? – JohnC
@JohnC誰かがあなたのコメントを見る可能性を高めるために、私はあなたの名前でここでやっているように、あなたのコメントの中に名前の前に@を入れてください。 –