1

我正在使用带有 Unity Extensions 和 MVVM 模式的 Prism 4。

public class Bootstrapper : UnityBootstrapper {
    protected override void InitializeShell() {
        Application.Current.RootVisual = (UIElement) Shell;
    }

    protected override DependencyObject CreateShell()
    {
        return ServiceLocator.Current.GetInstance<Shell>();
    }
}

ServiceLocator.Current.GetInstance() 抛出以下异常:

尝试获取 Shell 类型的实例时发生激活错误,密钥“”

4

2 回答 2

0

您可能在 shell 构造函数中有错误。

例如,如果您在 Shell 构造函数中有参数,请不要忘记在Bootstrapper的ConfigureContainer中声明:

引导程序.cs

...
protected override void ConfigureContainer()
{
  base.ConfigureContainer();
  this.Container.RegisterType<IMyService, MyService>(new ContainerControlledLifetimeManager());
}

壳牌.cs

...
public Shell(IMyService container)
{
    ...
}

您可以阅读此文档: http: //msdn.microsoft.com/en-us/library/gg430868 (v=pandp.40).aspx

于 2013-04-25T10:20:32.303 回答
0

我有两个单独的建议:

1)确保shell.xaml的name属性不为空

2)如果 1 不能像这样改变 InitializeShell:

 IShellView shell = new Shell();
 shell.ShowView();
 return shell as DependencyObject;

我希望这些能有所帮助。

于 2013-05-03T13:47:27.333 回答