PRISM 4 - RegisterViewWithRegion and Custom Export Attributes

I am using Prism 4 with the MEF extensions and the MVVM pattern. During initialization in the module, I call RegisterViewWithRegion (RegionNames.MyRegion, typeof (MyView)), which works fine when the view is constructed as follows:

[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class MyView : UserControl
{
    public MyView()
    {
     ....

The view is registered, and everything is in order. As soon as I change the Export to a custom export attribute, the view can no longer be found, although it is still in the container. This custom export attribute is taken from an exchange trader RI:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
[MetadataAttribute]
public class ViewExportAttribute : ExportAttribute, IViewRegionRegistration
{
    public ViewExportAttribute()
        : base(typeof(object))
    { }

    public ViewExportAttribute(string viewName)
        : base(viewName, typeof(object))
    {
        ViewName = viewName;
    }

    public string RegionName { get; set; }
    public string ViewName { get; set; }

}

and interface

public interface IViewRegionRegistration
{
    string RegionName { get; }
    string ViewName { get; }
}

By changing the export attribute to

[ViewExport(ViewName = "MyView", RegionName = RegionNames.MyRegion)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class MyView : UserControl
{
    public MyView()
    {
    ....

when calling RegisterViewWithRegion it gives an error: an activation error occurred while trying to get an instance of type MyView, the ""

Any tips? I looked at this part of the code all day without finding a solution.

+5
4

, ... , PRISM. : .

, RW Stock Trade, AutoPopulateExportedViewsBehavior. , Export , . "", ServiveLocator. /. Custom Export Attribute "" :

MyView view;
var myList = container.GetExports<object, IViewRegionRegistration>();
foreach (Lazy<object, IViewRegionRegistration> lazy in myList)
{
    if (lazy.Metadata.ViewName == "MyView")
    {
        view = lazy.Value as MyView;
        region.Add(view);
        break;
    }
}

, ViewInjection Prism Navigation [Export], .

+4

MEF? , , ViewExportAttribute AutoPopulateExportedViewsBehavior? , StockTraderRI :

this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(StockTraderRICommands).Assembly));

StockTraderRICommands , ViewExportAttribute AutoPopulateExportedViewsBehavior.

+4

typeof(object) , , . , .

, . , , -, , InnerException.

0

, MEF/PRISM. okieh , , StocktraderUI:

(/, , ), - .., .

1. ViewExport

[Export]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
[MetadataAttribute]
public sealed class ViewExportAttribute : ExportAttribute, IViewRegionRegistration
{
    public ViewExportAttribute()
        : base(typeof(UserControl))
    { }

    public string ViewName { get { return base.ContractName; } }

    public string RegionName { get; set; }
}

[], UserControl object. , MEF.

2. AutoPopulateExportedViewsBehavior

[ImportMany(typeof(UserControl))]
public Lazy<UserControl, IViewRegionRegistration>[] RegisteredViews { get; set; }

[ImportMany], Lazy UserControl. UserControl IViewRegionRegistration - MetaData .

This is basically it. You can use [ViewExport]as before. Note that views are limited to (sub) types UserControl. I suppose this can be changed if you want. And make sure your shared directory imports ViewExportAttributeand AutoPopulateExportedViewsBehavioras Nikolaus said ...

Thus, you do not need additional interfaces for your views and you can still find everything without a hard registration.

I hope this helps and let me know if I missed any flaws in my solution.

0
source

All Articles