C# 具有错误实例的Prism被传递到构造函数中
我正试图统一地理解/学习棱镜 我创建了以下类: ========================================== 包含“模块”的单独组件: ========================================== 主机应用程序(其他程序集):C# 具有错误实例的Prism被传递到构造函数中,c#,unity-container,prism,ioc-container,C#,Unity Container,Prism,Ioc Container,我正试图统一地理解/学习棱镜 我创建了以下类: ========================================== 包含“模块”的单独组件: ========================================== 主机应用程序(其他程序集): 公共部分类应用程序:PrismApplication { 私有Shell mShell; 私有ShellViewModel mShellViewModel; 受保护的覆盖IModuleCatalog CreateModule
公共部分类应用程序:PrismApplication
{
私有Shell mShell;
私有ShellViewModel mShellViewModel;
受保护的覆盖IModuleCatalog CreateModuleCatalog()
{
返回新的DirectoryModuleCatalog(){ModulePath=@.\..\..\..\..\ModuleOutput“};
}
受保护的覆盖无效注册表类型(IContainerRegistry containerRegistry)
{
containerRegistry.Register();
containerRegistry.Register();
}
受保护的覆盖窗口CreateShell()
{
mShellViewModel=Container.Resolve();
mShell=(Shell)mShellViewModel.View;
返回mShell;
}
(...)
现在我的问题是:
- 如何告诉Prism解析传递给 ViewModel的构造函数是否正确
我在网上找到了一些源代码,但它们使用了容器的“RegisterType”方法。目前,我的ModuleA中没有Unity的依赖项,我不知道如何让容器调用“RegisterType”。所有源在web中都已过时。默认情况下,它会解析默认注册,在您的情况下是
Shell
使用名称注册类型并不意味着自动使用该名称来解析依赖关系。您必须手动执行此操作,包括参数重写、注入工厂等。但我会尽量避免这样做,因为这会使事情变得有点脆弱和乏味。为什么要将视图传递给视图模型?Plurasigh中的那个家伙t online training也这么做了。我认为这是因为如果你选择ViewModel first方法,你只需创建你的ViewModel,框架就会自动解析你的视图。我的想法和我看到的一样。我该如何避免?避免什么?它总是解析默认注册,除非你告诉它使用特定的命名注册h参数覆盖。
using GlobalContracts;
using Prism.Ioc;
using Prism.Modularity;
namespace ModuleA
{
[Module(ModuleName = MyModuleA.NAME, OnDemand = true)]
public class MyModuleA : IModule
{
public const string NAME = "MyModuleA";
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.Register<MyControlA>();
containerRegistry.Register<IView, MyControlA>(NAME);
containerRegistry.Register<PluginViewModelBase, MyControlViewModel>(NAME);
}
public void OnInitialized(IContainerProvider containerProvider)
{
}
}
}
using GlobalContracts;
namespace ModuleA
{
public class MyControlViewModel : PluginViewModelBase
{
public MyControlViewModel(IView view) : base(view)
{
}
}
}
public partial class App : PrismApplication
{
private Shell mShell;
private ShellViewModel mShellViewModel;
protected override IModuleCatalog CreateModuleCatalog()
{
return new DirectoryModuleCatalog(){ModulePath = @"..\..\..\..\ModulesOutput"};
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.Register<IView, Shell>();
containerRegistry.Register<IViewModel, ShellViewModel>();
}
protected override Window CreateShell()
{
mShellViewModel = Container.Resolve<ShellViewModel>();
mShell = (Shell)mShellViewModel.View;
return mShell;
}
(...)