【问题标题】:PRISM + MEF -- Can't get regions to work properlyPRISM + MEF - 无法让区域正常工作
【发布时间】:2011-02-09 21:53:03
【问题描述】:

对 Prismv4 和 MEF 有点陌生。

我浏览了快速入门并尝试将其中两个组合在一起,但似乎无法正常工作。

首先,我有一个引导程序来加载 Shell 窗口。

public sealed class ClientBootstrapper : MefBootstrapper
{
    protected override void ConfigureAggregateCatalog()
    {
        base.ConfigureAggregateCatalog();

        //Add this assembly to export ModuleTracker (Shell is in this Assembly).
        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
    }

    protected override DependencyObject CreateShell()
    {
        return Container.GetExportedValue<Shell>();
    }

    protected override void InitializeShell()
    {
        base.InitializeShell();

        Application.Current.MainWindow = (Window)Shell;
        Application.Current.MainWindow.Show();
    }
}

这很好用。显示了 Shell 窗口,并出现了一条漂亮的 Hello World 消息。然后我尝试在 Shell 窗口内创建一个区域,以便将视图加载到该区域中。我什至还没有完成这项工作,甚至没有考虑将其移至外部程序集。

[ModuleExport(typeof(HelloWorldModule), InitializationMode = InitializationMode.OnDemand)]
public class HelloWorldModule : IModule
{
    [Import(AllowRecomposition = true)]
    private IRegionViewRegistry regionViewRegistry;

    [ImportingConstructor()]
    public HelloWorldModule(IRegionViewRegistry registry)
    {
        this.regionViewRegistry = registry;
    }

    public void Initialize()
    {
        regionViewRegistry.RegisterViewWithRegion("PrimaryRegion", typeof(Views.HelloWorldView));
    }
}

HelloWorld 视图(只是一个包含 TextBlock 的简单 UserControl)没有被加载到该区域中!我想我对如何在我的地区加载有点迷茫。

【问题讨论】:

    标签: c# wpf mef regions prism-4


    【解决方案1】:

    你可以试试这个,它对我有用,模块类如下:

    [ModuleExport(typeof(HelloWorldModule))]
    public class HelloWorldModule : IModule
    {
    
        [Import]
        private IRegionManager regionManager;
    
        public void Initialize()
        {
            Uri viewNav = new Uri("HelloWorldView", UriKind.Relative);
            regionManager.RequestNavigate("PrimaryRegion", viewNav);
        }
    }
    

    View类如下:

    [Export("HelloWorldView")]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public partial class HelloWorldView : UserControl
    {
        public HelloWorldView()
        {
            InitializeComponent();
        }
    }
    

    HelloWorldView 中的 xaml

    <UserControl x:Class="HelloWorldModule.HelloWorldView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock>Hello World</TextBlock>
    </Grid>
    </UserControl>
    

    你需要

    protected override void ConfigureAggregateCatalog()
        {
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstapper).Assembly));
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(HelloWorldModule.HelloWorldModule).Assembly));
    
        }
    

    【讨论】:

      【解决方案2】:

      您似乎正在尝试使用视图发现方法?

      您是否尝试过以下方法:

          [ModuleExport(typeof(HelloWorldModule), InitializationMode = InitializationMode.OnDemand)]
      public class HelloWorldModule : IModule
      {
          private IRegionManager regionManager;      
      
          [ImportingConstructor]
          public HelloWorldModule(IRegionManager regionManager)
          {
              this.regionManager = regionManager;
          } 
      
          public void Initialize()
          {
              this.regionManager.RegisterViewWithRegion("PrimaryRegion", typeof(Views.HelloWorldView));
          }
      }
      

      【讨论】:

        【解决方案3】:

        bootstrapper 中的 ConfigureAggregateCatalog 方法需要添加 view 所在的程序集。将下面的行添加到方法的末尾应该可以解决当前的问题。

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

        这也可以在XAML 中的ModuleCatalog 中完成。

        您的Views.HelloWorld 类还需要添加一个[Export] 属性。

        【讨论】:

          【解决方案4】:

          根据您分享的信息,很难判断出了什么问题。 我建议查看 PRISM4 附带的示例和参考实现。我想参考实现和一些调试应该可以帮助您找到问题。

          【讨论】:

            【解决方案5】:
            public class ModuleC : IModule
            {
                #region IModule Members
            
                /// <summary>
                /// Initializes the module.
                /// </summary>
                public void Initialize()
                {
                    /* We register always-available controls with the Prism Region Manager, and on-demand 
                     * controls with the DI container. On-demand controls will be loaded when we invoke
                     * IRegionManager.RequestNavigate() to load the controls. */
            
                    // Register task button with Prism Region
                    var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
                    regionManager.RegisterViewWithRegion("TaskButtonRegion", typeof(ModuleBTaskButton));
            
                    /* View objects have to be registered with Unity using the overload shown below. By
                     * default, Unity resolves view objects as type System.Object, which this overload 
                     * maps to the correct view type. See "Developer's Guide to Microsoft Prism" (Ver 4), 
                     * p. 120. */
            
                    // Register other view objects with DI Container (Unity)
                    var container = ServiceLocator.Current.GetInstance<IUnityContainer>();
                    container.RegisterType<Object, ModuleBRibbonTab>("ModuleBRibbonTab");
                    container.RegisterType<Object, ModuleBNavigator>("ModuleBNavigator");
                    container.RegisterType<Object, ModuleBWorkspace>("ModuleBWorkspace");
                }
            }
            

            【讨论】:

            • 这篇文章的“答案”部分在哪里?
            • 这个问题是关于 MEF 实现的。不幸的是,这里提供的答案是使用 Unity。
            猜你喜欢
            • 2013-10-31
            • 1970-01-01
            • 2012-09-05
            • 2017-11-06
            • 2011-09-06
            • 1970-01-01
            • 2023-03-10
            • 2020-10-16
            • 2014-01-15
            相关资源
            最近更新 更多