c# - PRISM + MEF -- 无法让区域正常工作

标签 c# wpf mef regions prism-4

Prismv4 和 MEF 有点新。

我浏览了 QuickStarts 并尝试将其中两个组合在一起,但我似乎无法让它工作。

首先,我有一个 Bootstrap 来加载 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 窗口中创建一个区域,以便我可以将 View 加载到该区域中。我什至没有让它工作,甚至考虑将它移动到外部组件。

[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 View (只是一个包含 TextBlock 的简单 UserControl)没有被加载到区域中!我想我对如何在我的区域加载有点迷茫。

最佳答案

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

[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));

    }

关于c# - PRISM + MEF -- 无法让区域正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4950957/

相关文章:

c# - 从具有字符串值的列中删除坏字符

c# - 在 XAML 中使用枚举值

c# - 应自定义 MEF 的哪些部分以在分布式应用程序中动态加载插件

c# - 如何在 MEF 中对不同的目录进行优先级排序?

c# - 由于程序集名称而导致 MEF 组合错误?

c# - ObservableCollection 也监视集合中元素的变化

C# 右键单击​​ TreeView 节点

c# - 确定类是否在不引用接口(interface)的情况下实现接口(interface)

WPF 数据网格 : How do I set columns to TextWrap?

C# WPF 设置 F 热键