【发布时间】:2020-12-06 22:18:22
【问题描述】:
这里是新开发人员,正在开发我的第二个应用程序,第一个使用 Prism 7.1。
我希望在正确访问在 Shell.xaml.cs 中注册的 ViewModel 方面获得一些帮助。
这是我正在使用的:
App.xaml.cs
public partial class App
{
protected override Window CreateShell()
{
return Container.Resolve<Shell>();
}
protected override void RegisterTypes(IContanerRegistry containerRegistry)
{
containerRegistry.Register<ShellViewModel>();
}
}
Shell.xaml.cs
public partial class Shell : MetroWindow
{
public Shell()
{
InitializeComponent();
}
}
通过执行以下操作,我可以正常访问 ViewModel 的属性:
var shellVM_Instance = containerProvider.Resolve<ShellViewModel>();
shellVM_Instance.IsBusy = false;
代码会编译,但不会运行。当我运行代码时,它告诉我,上述 var shellVM_Instance 中的 ShellViewModel 正在引用 ShellViewModel 类型的空对象。这让我认为我没有正确地向 IContainerRegistry 注册 ViewModel。
谁能提供帮助?
我想避免使用 Bootstrapper 类,并利用 Prism 7.1 提供的功能 (Brian's Release Notes)
感谢您在此提供的任何指导,感谢您在我努力思考 Prism 及其真正潜力时的耐心等待。
编辑:
我看到 IContainerRegistry 有一个 RegisterInstance 方法..
void RegisterInstance(Type type, object instance);
我终其一生都无法弄清楚语法。我的尝试:
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
ShellViewModel shell_VM = new ShellViewModel();
containerRegistry.RegisterInstance<ShellViewModel, shell_VM>();
}
谢谢!
-克里斯
【问题讨论】:
-
@bakunet 谢谢。我将看看是否可以在我的应用程序中实现类似的功能。
-
不客气。我使用 Autofac IoC,并将 Prism 仅用于 EventAggregator,但在此链接下,您可以找到如何将其用于 Prism 的不错示例。
标签: c# wpf instance unity-container prism