3 つの異なるリージョンで MEF と Prism を使用して、WPF で MVVM アプリを作成しました。コードは 2 つのモジュールにまたがっており、App.Config で検出されます。
私はすべてのナビゲーション コマンドと構造を完全に正常に動作させていますが、混乱しているのは、アプリの起動時に各領域に読み込まれる初期ビューを設定する方法です。これを行うことができる場所がないようです。さらに、MainViewModel コンストラクターの最後に何かを追加して、スクリーン セット A に明示的に移動すると、別の何かがそれをオーバーライドし、別のビュー セットを読み込んでいるように見えます。
また、望ましくないように見える app.config にモジュールをロードする順序にも依存しているようです。管理モジュールを最後にロードすると、管理モジュールから一連の画面がロードされます。検索モジュールを最後にロードすると、検索モジュールからビューのセットがロードされます。この場合、ビューを見つけることさえできません。メイン地域。
MEF と構成検出を使用する場合、アプリの起動時に各リージョンに読み込まれるビューを指定する方法は何ですか?
using System;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Regions;
namespace CRM.GUI.WPF.Shared.Infrastructure.Behaviour
{
[Export(typeof(AutoPopulateExportedViewsBehavior))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class AutoPopulateExportedViewsBehavior : RegionBehavior, IPartImportsSatisfiedNotification
{
protected override void OnAttach()
{
AddRegisteredViews();
}
public void OnImportsSatisfied()
{
AddRegisteredViews();
}
private void AddRegisteredViews()
{
if (Region != null)
{
foreach (var viewEntry in RegisteredViews)
{
if (viewEntry.Metadata.RegionName == Region.Name)
{
var view = viewEntry.Value;
if (!Region.Views.Contains(view))
{
Region.Add(view);
}
}
}
}
}
[ImportMany(AllowRecomposition = true)]
public Lazy<object, IViewRegionRegistration>[] RegisteredViews { get; set; }
}
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
[MetadataAttribute]
public class ViewExportAttribute : ExportAttribute, IViewRegionRegistration
{
public ViewExportAttribute()
: base(typeof(object))
{ }
public ViewExportAttribute(string viewName)
: base(viewName, typeof(object))
{ }
public string RegionName { get; set; }
}
使用済み
[ViewExport(RegionName = RegionNames.MainRegion)]
public partial class ReportView