2014-06-12 71 views
1

我的應用程序是一個IDE插件,因此該項目是一個wpf用戶控件。我現在想用棱鏡來反射它。當我調試應用程序,它在MainView的構造InitializeComponent();拋出這樣的例外:使用棱鏡5.0開發wpf插件

"ServiceLocationProvider must be set." 

我也發現了類似的主題在這裏:Strange exception in Prism application 但沒有解決方案。

這是我的引導程序類:

class Bootstrapper : UnityBootstrapper 
{ 
    protected override System.Windows.DependencyObject CreateShell() 
    { 
     return ServiceLocator.Current.GetInstance<MainView>(); 
    } 

    protected override void ConfigureModuleCatalog() 
    { 
     base.ConfigureModuleCatalog(); 
     ModuleCatalog catalog = (ModuleCatalog)this.ModuleCatalog; 
     catalog.AddModule(typeof(ModuleInit)); 
    } 
} 

這是我ModuleInit類:

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

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

    public void Initialize() 
    { 
     this.container.RegisterType<ViewModelA>(new ContainerControlledLifetimeManager()); 
     this.container.RegisterType<MainViewModel>(new ContainerControlledLifetimeManager()); 
     this.regionManager.RegisterViewWithRegion(Models.RegionNames.ARegion,() => this.container.Resolve<ViewModelA>()); 
    } 
} 

因爲這是用戶控制項目,所以沒有的App.xaml和App.xaml.cs文件。我可以引導程序的運行方法的MainView的構造函數:

public MainView() 
{ 
     InitializeComponent();  
     Bootstrapper strapper = new Bootstrapper(); 
     strapper.Run(); 
} 

但例外是在構造函數的第一行拋出。任何人都可以幫忙

此外,有沒有辦法使用區域沒有模塊化?我的應用程序只包含一個項目,它不需要模塊化。如果是,我可以在哪裏註冊視圖,服務和視圖模型?

+0

你爲什麼要使用該服務定位找到外殼的實例?爲什麼不能團結解決呢? –

+0

@GayotFow使用這段代碼有相同的問題:protected override System.Windows.DependencyObject CreateShell() { return this.Container.TryResolve (); } – Allen4Tech

+0

這就是要解決的問題。如果團結無法做到,那麼事情就是嚴重錯誤的。使用服務管理器是因爲容器引發異常只是隱藏了問題。 –

回答

0

某處,例如在init代碼之前的引導程序中,您必須定義您的IoC容器。 這是它是如何在做ViewModelLocator默認:

ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);