[英]The type cannot be used as type parameter 'TTo' in the generic type or method. There is no implicit reference conversion
I'm currently trying to use Unity to inject ViewModels into my WPF Application.我目前正在尝试使用 Unity 将 ViewModel 注入到我的 WPF 应用程序中。
I tend to use a ViewModelLocator
class to store all of my ViewModels in one area, for quick navigation between them我倾向于使用
ViewModelLocator
类将我所有的 ViewModel 存储在一个区域中,以便在它们之间快速导航
I've split up my app into four projects:我已将我的应用程序拆分为四个项目:
KeystonePP.Models
- Holds my EF Model KeystonePP.Models
- 保存我的 EF 模型
KeystonePP.Startup
- Main startup logic KeystonePP.Startup
- 主要启动逻辑
KeystonePP.ViewModels
- ViewModels and their interfaces KeystonePP.ViewModels
- ViewModels 及其接口
KeystonePP.Views
- Views KeystonePP.Views
- 视图
I have an IViewModelLocator
interface in a separate project: KeystonePP.ViewModels
我在一个单独的项目中有一个
IViewModelLocator
接口: KeystonePP.ViewModels
public interface IViewModelLocator
{
// No code here. Just a contract
}
This is implemented by my ViewModelLocator
class这是由我的
ViewModelLocator
类实现的
public class ViewModelLocator : ObservableObject, IViewModelLocator
{
// Lots of code here
}
In my KeystonePP.Startup
project, I have adjusted my MainWindow.xaml.cs
constructor as such:在我的
KeystonePP.Startup
项目中,我已经调整了我的MainWindow.xaml.cs
构造函数:
public partial class MainWindow : Window
{
public MainWindow (IViewModelLocator viewModelLocator)
{
InitializeComponent();
DataContext = viewModelLocator;
}
}
However, when I attempt the following code in App.xaml.cs
, I'm getting an error warning:但是,当我在
App.xaml.cs
尝试以下代码时,我收到错误警告:
public partial class App : Application
{
/// <summary>
/// Startup Logic for App
/// </summary>
/// <param name="e"></param>
protected override void OnStartup (StartupEventArgs e)
{
base.OnStartup(e);
IUnityContainer container = new UnityContainer();
container.RegisterType<IViewModelLocator, MainWindow>();
}
}
I get the following error:我收到以下错误:
The type 'KeystonePP.Startup.MainWindow' cannot be used as type parameter 'TTo' in the generic type or method 'UnityContainerExtensions.RegisterType(IUnityContainer, params InjectionMember[])'.
类型“KeystonePP.Startup.MainWindow”不能用作泛型类型或方法“UnityContainerExtensions.RegisterType(IUnityContainer, params InjectionMember[])”中的类型参数“TTo”。 There is no implicit reference conversion from 'KeystonePP.Startup.MainWindow' to 'KeystonePP.ViewModels.Utility.Interfaces.IViewModelLocator'.
没有从“KeystonePP.Startup.MainWindow”到“KeystonePP.ViewModels.Utility.Interfaces.IViewModelLocator”的隐式引用转换。
Have I gone too complicated, or am I missing something simple setting up Unity?我是不是太复杂了,还是我错过了一些简单的 Unity 设置?
You want to register abstractions with their implementations.你想用它们的实现注册抽象。
IUnityContainer container = new UnityContainer();
container.RegisterType<IViewModelLocator, ViewModelLocator>();
container.RegisterType<MainWindow>();
that way when you call to resolve MainWindow
这样当你打电话来解决
MainWindow
var mainWindow = container.Resolve<MainWindow>();
mainWindow.Show();
the container will know how to inject the implementation based on the abstraction when resolving.容器将知道如何在解析时基于抽象注入实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.