GetView problems

Topics: Prism v4 - WPF 4
Feb 17, 2011 at 3:25 PM

Hi,

in my project i want to display some views (using PRISM with MEF).
They are registered via ViewExport with contract name and region to display.

[ViewExport("GridView", RegionName = "Content")] [PartCreationPolicy(CreationPolicy.NonShared)] public partial class GridView : UserControl { [ImportingConstructor] public GridView() {  InitializeComponent(); } [Import] GridViewModel ViewModel { get { return this.DataContext as GridViewModel; } set { this.DataContext = value; } } }

As i want to deciede when to show a view i don`t use AutoPopulateExportedViewsBehavior.

Now on loading a Module a single view should be activated.

    [Module(ModuleName="Module A")]
    [ModuleExport(typeof(ModuleA), InitializationMode=InitializationMode.WhenAvailable)]
    public class ModuleA : IModule
    {
        [Import]
        ILoggerFacade logger;

        [Import]
        IRegionManager regionManager;

        public void Initialize()
        {
            this.logger.Log("Module A initialized.", Category.Info, Priority.Low);

            IRegion r = this.regionManager.Regions["Content"];
            object view = r.GetView("GridView");

            r.Activate(view);
        }
    }

The problem is, that GetView doesn`t return a view (always null).

It looks like all the registered views are present but without contract name. Hope you can help me to figure out how to get this working.

Thanks in advance

 

-ralf

Feb 17, 2011 at 5:40 PM

Hi Ralf,

Take into account that if you decide to decorate your views with the ViewExport attribute, you need to use the AutoPopulateExportedViewsBehavior which is responsible for inject those views into the specified region at the time they are created.

As the ViewExportAttribute attribute inherits from ExportAttribute, a view decorated with it will be exported in MEF, regardless of whether the AutoPopulateExportedViewsBehavior is attached to your regions. However, if you don't attach that behavior to your regions, the views decorated with the attribute won't be added to the region specified at the time it is created.

On the other hand, if you need to have explicit programmatic control of when your views are added to your reginos, it could be easier to use view injection.

Please let me know if this information helps you.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Feb 18, 2011 at 6:55 AM

Hi Miguel,

thank you very much! I will give ExportAttribute a try!

Seems like this will work!

Ralf