4

对 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)没有被加载到该区域中!我想我对如何在我的地区加载有点迷失了。

4

5 回答 5

8

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

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

视图类如下:

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

HelloWorldView 中的 xml

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

    }
于 2011-09-16T03:51:32.727 回答
1

看起来您正在尝试使用视图发现方法?

您是否尝试过以下方法:

    [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));
    }
}
于 2011-02-12T00:16:00.627 回答
1

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

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

这也可以XAMLModuleCatalog.

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

于 2011-03-13T03:50:09.430 回答
0

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

于 2011-02-11T00:12:15.120 回答
0
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");
    }
}
于 2011-04-27T11:58:00.157 回答