2011-02-09 18 views
4

Prismv4 및 MEF의 새로운 기능입니다.PRISM + MEF - 영역이 제대로 작동하지 않습니다.

나는 퀵 스타트를 통해 두 개를 결합하려고 시도했지만 작동시키지 못했습니다.

먼저 쉘 스크립트를로드하기 위해 부트 스트 래퍼가 있습니다.

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

이렇게하면 정상적으로 작동합니다. 셸 창이 표시되었고 멋진 Hello World 메시지가 나타납니다. 그런 다음 뷰를 해당 영역에로드 할 수 있도록 셸 창 안에 영역을 만들려고했습니다. 나는 심지어 이것을 외부의 집회로 옮기는 것을 보려고 노력하지도 못했다.

[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는 뷰 (a TextBlock을 포함 단순한의 UserControl)의 영역에로드되지 않고! 내 지역에로드하는 방법에 대해 조금 잃어버린 것 같아요.

답변

0

공유하는 정보를 기반으로 잘못된 정보를 말하는 것은 어렵습니다. PRISM4와 함께 제공되는 예제 및 참조 구현을 살펴볼 것을 제안합니다. 참조 구현 및 약간의 디버깅 문제를 찾는 데 도움이 될 것 같아요.

1

보기 검색 접근법을 사용하려는 것 같습니다. 당신의 view가에있는 어셈블리를 추가 할 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 방법 현재 과거 당신을 얻을하는 메소드의 끝 부분에 아래 라인을 추가 :

는 다음을 시도했습니다. 문제.

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

또한 ModuleCatalogXAML에서 수행 할 수 있습니다.

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

이 게시물의 "답변"부분은 어디에 있습니까? –

+1

이 질문은 MEF 구현에 대해 묻습니다. 불행히도 여기에 제공된 대답은 Unity를 사용하는 것입니다. –

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

    } 
관련 문제