2011-02-09 22 views
4

對Prismv4和MEF有點新鮮。PRISM + MEF - 無法讓區域正常工作

我經歷了快速入門,並試圖將它們兩個結合在一起,但我似乎無法得到它的工作。

首先,我得到了一個Bootstrapper來加載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)沒有被加載到區域!我想我在這裏有點迷失在如何加載我的地​​區。

回答

0

根據您分享的信息很難說出什麼問題。 我會建議看一下PRISM4附帶的示例和參考實現。我想參考實現和一點調試應該可以幫助你找到問題。

1

它看起來像你試圖使用視圖發現方法?在bootstrapper需求

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

ConfigureAggregateCatalog方法來添加組件您view中添加線以下的方法結束應該讓你過去你當前:

你試過以下。問題。

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

這也可以在XAMLModuleCatalog中完成。

您的Views.HelloWorld類還需要添加[Export]屬性。

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"); 
    } 
} 
+1

這篇文章的「答案」部分在哪裏? – 2011-04-27 14:43:43

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

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> 

你會n eed

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

    }