在WPF便携式类库ICommand的编译错误 - 不知道如何妥善解决这个? [英] Portable Class Library ICommand compilation error in WPF -- Not sure how to resolve this appropriately?

查看:536
本文介绍了在WPF便携式类库ICommand的编译错误 - 不知道如何妥善解决这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景资料:

  • 在我使用VS 2013
  • 在我创建了一个可移植类库(目标:.NET 4.5 / Silverlight的5 /运电话8 /运店8)
  • 在我实现了我的类名为 MyCommand 的ICommand 接口。

MyCommand.cs:

 公共类MyCommand:ICommand的
{
    公共BOOL CanExecute(对象参数)
    {
        抛出新的NotImplementedException();
    }

    公共无效执行(对象参数)
    {
        抛出新的NotImplementedException();
    }

    公共事件的EventHandler CanExecuteChanged;
}
 

我发现,当我试图引用使用 MyCommand 类在WPF 4.5应用程序时,我收到以下错误:

  

类型System.Windows.Input.ICommand在装配定义   未引用。您必须添加一个引用组件   'System.Windows,版本= 2.0.5.0,文化=中立,   公钥= 7cec85d7bea7798e,重定目标=是。

我真的不知道为什么的这种情况正在发生,或者我能做些什么,以妥善解决它。冲刷互联网后,我发现答案在谈论我的WPF应用程序添加一个引用 System.Windows.dll中4.0.0.0 。这样做可以让我来编译和运行应用程序,但我的IDE抱怨说:

  

接口成员无效   System.Windows.Markup.IComponentConnector.Connect(在,对象)'不是   实现。

这会发生我MainWindow.cs类。有没有比一个更好的修正,只是处理这个错误,所以我不添加引用 System.Windows.dll中在一个WPF应用程序,它真的不需要它?我说,它并没有真正需要它,因为如果我复制/粘贴 MyCommand 类到WPF应用程序,它工作正常。它只有一个问题,因为我试图把它作为一个便携式类库。


更新:

似乎有更多的这种比满足眼睛。我创建了一个新的WPF应用程序和使用的NuGet引用MvvmCross解决方案(因为它有一个 MvxCommand )对象。当我这样做,我得到了同样的错误。

大卫基恩的回答以一个类似的问题,表明,这应通过VS2012 / VS2013是固定的并且如果没有,只是重新安装/修复。我完全打消了我的VS安装,从头开始(VS2013),重新安装它,仍然有这个问题。所以,这是一个真正的错误还是我做错什么了吗?


更新2:

我尝试看看我是否能分配 MyCommand 类的一个实例的ICommand 的ICommand CMD =新MyCommand(); 并收到以下错误:

  

无法隐式转换类型'PortableClassLibrary1.MyCommand来   System.Windows.Input.ICommand。一个显式转换存在(是   缺少强制转换?)

由于某种原因,PCL似乎并不键入推进 System.Windows.dll中[2.0.5.0] 的ICommand到 System.dll中[ 4.0.0.0] 的ICommand我的桌面应用程序...唉!


更新3:

我上传我的解决方案,使其以更加醒目什么问题是,


更新4:

我已经开了一个<一个href="https://connect.microsoft.com/VisualStudio/feedback/details/811512/portable-class-library-forces-me-to-add-system-windows-dll-to-my-wpf-project"相对=nofollow>微软连票这个问题。


更新5:

这是心烦这不工作...和其他人告诉我,他们没有得到相同的接口的错误平静下来之后。我认识到,加入 System.Windows.dll中的副作用是一个ReSharper的8.1错误。我想是时候抱怨ReSharper的,而不是微软。 叹息

解决方案
  

您必须添加一个引用程序集System.Windows,版本= 2.0.5.0

的建议是优秀的,该消息并没有很大的。显然,你有莫名其妙的版本号。右键单击您的WPF项目,添加引用,并勾选 System.Windows 。解决了你的问题。

缺少神奇的是这条线在System.Windows.dll中引用程序集:

  [总成:TypeForwardedTo(typeof运算(ICommand的))]
 

它得到了ICommand向前System.Windows.dll中到System.dll中

请记住,System.Windows.dll中从C:\ Program Files文件(x86)的\引用程序集子目录的只是的参考组装。它不包含任何code或类型的所有,只是[TypeForwardedTo]属性。它作为一个调节器,弥合不同的.NET架构平台之间的差距。最终的可执行文件实际上并没有一个依赖于System.Windows.dll中的。

Some background information:

  • I am using VS 2013
  • I created a Portable Class Library (Targets: .NET 4.5/Silverlight 5/Win Phone 8/Win Store 8)
  • I implemented the ICommand interface in my class named MyCommand.

MyCommand.cs:

public class MyCommand : ICommand
{
    public bool CanExecute(object parameter)
    {
        throw new NotImplementedException();
    }

    public void Execute(object parameter)
    {
        throw new NotImplementedException();
    }

    public event EventHandler CanExecuteChanged;
}

I found that when I attempted to reference use the MyCommand class in a WPF 4.5 application, I get the following error:

The type 'System.Windows.Input.ICommand' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'.

I'm not really sure why this is happening, or what I can do to resolve it appropriately. After scouring the internet, I found answers talking about adding a reference to System.Windows.dll 4.0.0.0 in my WPF application. Doing this allows me to compile and run the application, but my IDE complains that:

Interface member 'void System.Windows.Markup.IComponentConnector.Connect(in, object)' is not implemented.

This occurs for my MainWindow.cs class. Is there a better fix than to just deal with this bug so I'm not adding a reference to System.Windows.dll in a WPF application which really doesn't need it? And I say that it doesn't really need it because if I copy/paste the MyCommand class into the Wpf app it works fine. It only has an issue because I am attempting to use it as a portable class library.


UPDATE:

It seems that there is more to this than meets the eye. I created a new Wpf Application and used NuGet to reference the MvvmCross solution (since it has an MvxCommand object). When I do this, I get the same error.

David Kean's answer to a similar question suggests that this should be fixed via VS2012/VS2013 and if not, just reinstall/repair. I fully removed my VS installation and reinstalled it from scratch (VS2013) and still have this issue. So, is this truly a bug or did I do something wrong here?


UPDATE 2:

I attempted to see if I could assign an instance of the MyCommand class to ICommand: ICommand cmd = new MyCommand(); and received the following error:

Cannot implicitly convert type 'PortableClassLibrary1.MyCommand' to 'System.Windows.Input.ICommand'. An explicit conversion exists (are you missing a cast?)

For some reason the PCL does not seem to type forward the System.Windows.dll [2.0.5.0] ICommand to System.dll [4.0.0.0] ICommand for my desktop application... Ugh!


Update 3:

I uploaded my solution so that it is more visible as to what the problem is.


Update 4:

I've opened a Microsoft connect ticket on this issue.


Update 5:

After calming down from being upset about this not working... and others telling me that they don't get the same "interface" error. I realize that the "side-effect" of adding System.Windows.dll was a ReSharper 8.1 error. I guess it's time to complain to ReSharper instead of Microsoft. sigh

解决方案

You must add a reference to assembly 'System.Windows, Version=2.0.5.0

The advice is excellent, the message is not that great. Clearly you got baffled by the version number. Right-click your WPF project, Add Reference and tick System.Windows. Which solves your problem.

The missing magic is this line in the System.Windows.dll reference assembly:

  [assembly: TypeForwardedTo(typeof(ICommand))]

Which gets ICommand forward from System.Windows.dll to System.dll

Keep in mind that System.Windows.dll from the c:\program files (x86)\reference assemblies subdirectory is just a reference assembly. It doesn't contain any code or types at all, just [TypeForwardedTo] attributes. It acts as an "adapter", bridging the gap between different .NET framework platforms. The final executable doesn't actually have a dependency on System.Windows.dll at all.

这篇关于在WPF便携式类库ICommand的编译错误 - 不知道如何妥善解决这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆