我一直在尝试将ModuleCatalog中的模块注入我的Shell的ViewModel,但我运气不好......
我正在我的Bootstrapper中创建ModuleCatalog,我的模块从其Initializer进入屏幕没有问题.但是,我希望能够将我的模块列表绑定到一个带有DataTemplate的容器,从而允许它们从菜单中启动!
这是我的Boostrapper文件,随着时间的推移,我将添加更多模块,但就目前而言,它只包含了我设计的"ProductAModule":
public class Bootstrapper : UnityBootstrapper
{
protected override void ConfigureContainer()
{
Container.RegisterType<IProductModule>();
base.ConfigureContainer();
}
protected override IModuleCatalog GetModuleCatalog()
{
return new ModuleCatalog()
.AddModule(typeof(ProductAModule));
}
protected override DependencyObject CreateShell()
{
var view = Container.Resolve<ShellView>();
var viewModel = Container.Resolve<ShellViewModel>();
view.DataContext = viewModel;
view.Show();
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,这是我的Shell的ViewModel:
public class ShellViewModel : ViewModelBase
{
public List<IProductModule> Modules { get; set; }
public ShellViewModel(List<IProductModule> modules)
{
modules.Sort((a, b) => a.Name.CompareTo(b));
Modules = modules;
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我正在尝试注入一个IProductModule列表(ProductAModule继承了它的一些属性和方法),以便它可以绑定到我的Shell的View.是否有一些非常简单的我缺少或者不能使用Unity IoC?(我已经看到它完成了StructureMap对Prism的扩展)
还有一件事......当运行应用程序时,在Bootstrapper中的Container正在解析ShellViewModel时,我收到以下异常:
依赖项的解析失败,type ="PrismBasic.Shell.ViewModels.ShellViewModel",name ="".异常消息是:当前构建操作(构建密钥构建密钥[PrismBasic.Shell.ViewModels.ShellViewModel,null])失败:尝试调用构造函数PrismBasic.Shell.ViewModels.ShellViewModel(System.Collections)时无法解析参数模块.Generic.List`1 [[PrismBasic.ModuleBase.IProductModule,PrismBasic.ModuleBase,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]] modules).(策略类型BuildPlanStrategy,索引3)
无论如何,简单吧......看起来很困惑......
任何帮助将不胜感激!
抢
我想我今天遇到了类似的问题,原来 PRISM 在初始化模块之前创建了 shell,因此您无法将模块中的任何服务注入到 shell 本身中。
尝试创建另一个依赖于所有其他模块并实现您想要的功能的模块,然后您可以将其添加到 shell 中的某个区域以显示您的服务列表。不幸的是我还没有机会尝试,但这是我计划实施的解决方案。
附带说明一下,我认为您需要使用属性来标记属性才能使用属性注入,但我可能是错误的(自从我直接使用 Unity 以来已经有一段时间了)。
编辑:您需要将 DependencyAttribute 应用于属性才能在 Unity 中使用 setter 注入;你可以在这里读到它。
| 归档时间: |
|
| 查看次数: |
4485 次 |
| 最近记录: |