I have written an MVVM app in WPF using MEF and Prism with three different regions. Code is across two modules, being discovered in the App.Config.
I have all the navigation commands and structure working perfectly fine, but the one thing I am confused on is how to set the initial Views loaded into each region at app startup as there seems to be nowhere I can do this. Furthermore if I add something to the end of the MainViewModel constructor to explicitly navigate to screen set A, something else seems to be overriding it and loading a different set of views to start with.
It also seems dependent on what order I load the modules in on the app.config which seems undesirable. If I load the Admin module last, it loads a set of screens from the admin module, if I load the search module last, it loads a set of views from the search module and in this case, it is not even finding a View for the main region.
What is the method for specifying what Views are loaded into each region at app startup when using MEF and config discovery?
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; }
}
Used
[ViewExport(RegionName = RegionNames.MainRegion)]
public partial class ReportView
RegisteredViews
contain? Can you post the shell, too? – Marc