2013-02-01 25 views
2

我有麻煩導航使用棱鏡4Viewinjection與團結失敗

我已經建立了一個測試解決方案,其中我有一個區域的殼叫MainRegion工作:

<Window x:Class="PrismNavigationTest.ShellView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:prism="http://www.codeplex.com/prism" 
     Title="ShellView" Height="350" Width="525"> 
    <Grid> 
     <ContentControl prism:RegionManager.RegionName="MainRegion"/> 
    </Grid> 
</Window> 

下一頁我有一個引導程序,增加了一個Module位於在不同的項目中我modulecatalog

protected override void ConfigureModuleCatalog() 
{ 
    Type module = typeof(Module.ModuleInit); 
    ModuleCatalog.AddModule(new ModuleInfo("Module", module.AssemblyQualifiedName)); 

    base.ConfigureModuleCatalog(); 
} 

在初始化方法上ModuleInit class我試圖更改mainregion的視圖。

public class ModuleInit : IModule 
{ 
    private IUnityContainer container; 
    private IRegionManager regionManager; 

    public ModuleInit(IUnityContainer container, IRegionManager regionManager) 
    { 
     this.container = container; 
     this.regionManager = regionManager; 
    } 

    public void Initialize() 
    { 
     // TODO This works 
     //regionManager.RegisterViewWithRegion("MainRegion",() => container.Resolve<View>()); 

     // TODO This doesn't work 
     container.RegisterType<object, View>("View1"); 
     regionManager.Regions["MainRegion"].RequestNavigate(new Uri("View1", UriKind.Relative)); 
    } 
} 

使用視圖發現工作但不能查看注入。

回答

0

問題出在我的Bootstrapper

Application.Current.MainWindow = (Window)ServiceLocator.Current.GetInstance<Shell>(); 

我改變這個改爲使用已初始化殼:

Application.Current.MainWindow = (Window)this.Shell; 

進出口仍然有點由混淆

InitializeShell方法我使用此代碼解決的殼牌從容器這個,但我認爲它與創建新對象的統一性有關,而不是在解析時默認爲默認值。

0

更改以下行;

regionManager.RequestNavigate("MainRegion", "View1"); 

to this;

var view1 = new Uri("View1", UriKind.Relative); 
regionManager.RequestNavigate("MainRegion", view1); 

而且,似乎還有另外一個問題,你用RegisterViewWithRegion顯示視圖時調用() => container.Resolve<View>()。那麼看起來你已經註冊了這個視圖。但在「不工作」的部分,你再次註冊View

+0

我將「View1」改爲Uri,但不幸的是它並沒有解決我的問題。關於註冊的觀點,即使我之前沒有註冊,Unity也能夠解決我的觀點。 – user1029697

+0

您可以檢查您的引導程序類是否從UnityBootstrapper派生,並且您正在ModuleInit類中使用相同的容器? – daryal

+0

你也可以嘗試區域本身的RequestNavigate方法; regionManager.Regions [「MainRegion」]。RequestNavigate(new Uri(「View1」,UriKind.Relative)); – daryal