简体   繁体   English

该类型不能用作泛型类型或方法中的类型参数“TTo”。 没有隐式引用转换

[英]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.

相关问题 该类型不能在泛型类型或方法中用作类型“T”。 没有隐式的引用转换 - The type cannot be used as type 'T' in the generic type or method. There is no implicit reference conversion from to 类型&#39;&#39;不能用作通用类型或方法&#39;&#39;中的类型参数&#39;&#39;。 没有从“到”的隐式引用转换 - The type '' cannot be used as type parameter '' in the generic type or method ''. There is no implicit reference conversion from '' to '' 类型 &#39;&#39; 不能用作泛型类型或方法 &#39;&#39; 中的类型参数 &#39;T&#39;。 没有从 &#39;&#39; 到 &#39;&#39; 的隐式引用转换 - The type '' cannot be used as type parameter 'T' in the generic type or method ''. There is no implicit reference conversion from '' to '' 类型“int”不能用作泛型方法中的类型参数“T”。 'int' 没有拳击转换 - The type 'int' cannot be used as type parameter 'T' in the generic method. There is no boxing conversion from 'int' 该类型不能在泛型类型或方法'BaseController <T>'中用作类型参数'T'。没有隐含的参考 - The type cannot be used as type parameter 'T' in the generic type or method 'BaseController<T>'. There is no implicit reference 当我从Ioc配置中使用时,我得到关于的错误(不能在通用类型或方法中用作类型参数&#39;TTo&#39;) - When I Use from Ioc Configuration I Get Error About (cannot be used as type parameter 'TTo' in the generic type or method ) CS0311 C# 该类型不能用作泛型类型或方法中的类型参数“TContext”。 EntityFrameworkCore - CS0311 C# The type cannot be used as type parameter 'TContext' in the generic type or method. EntityFrameworkCore 错误:“该类型不能用作方法的通用类型中的类型参数。”这可以通过约束或强制转换来解决吗? - Error: “The type cannot be used as type parameter in the generic type of method.” Can this be solved with constraints or casting? 通用方法-类型不能用作类型参数 - Generic method - Type cannot be used as Type Parameter 通用参数基类型:“没有从B到A的隐式引用转换” - Generic parameter base type: “There is no implicit reference conversion from B to A”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM