.net GalaSoft.MvvmLight.WP71编译错误,需要为RelayCommand添加对System.Windows的引用
我正在VS2010中使用MVVM Light Toolkit的V3和4.0版的target.NET FW 作为MVVM Light toolkit使用的一部分,我正在使用实现ICommand接口的RelayCommand类。 我还引用了ICommand接口的Presentation.Core程序集。 不知何故,在编译时,我得到了以下错误,这在以前版本的MVVM Light中是不会发生的 在未引用的程序集中定义了类型“System.Windows.Input.ICommand”。您必须添加对程序集“System.Windows,Version=2.0.5.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e”的引用。 现在,我找不到任何System.Windows程序集以及为什么它不使用PresentationCore程序集中的“System.Windows.Input.ICommand”类型 这里怎么了 这是视图代码.net GalaSoft.MvvmLight.WP71编译错误,需要为RelayCommand添加对System.Windows的引用,.net,wpf,visual-studio-2010,mvvm,.net,Wpf,Visual Studio 2010,Mvvm,我正在VS2010中使用MVVM Light Toolkit的V3和4.0版的target.NET FW 作为MVVM Light toolkit使用的一部分,我正在使用实现ICommand接口的RelayCommand类。 我还引用了ICommand接口的Presentation.Core程序集。 不知何故,在编译时,我得到了以下错误,这在以前版本的MVVM Light中是不会发生的 在未引用的程序集中定义了类型“System.Windows.Input.ICommand”。您必须添加对程序集
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP71"
Height="300" Width="300" ResizeMode="NoResize"
WindowStyle="None" Background="Transparent" AllowsTransparency="True">
<Window.DataContext>
<local:Window1ViewModel/>
</Window.DataContext>
<Window.Resources>
<Style x:Key="CloseWndButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle Fill="Transparent"
Stroke="Transparent"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Border BorderBrush="BlanchedAlmond" BorderThickness="6" CornerRadius="8" Background="BlanchedAlmond">
<Grid Background="BlanchedAlmond">
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="25"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Style="{StaticResource CloseWndButton}" Grid.Row="0" Grid.Column="1" Margin="3" Content="X" Command="{Binding CloseWindowCommand}" FontSize="14" FontStyle="Normal" FontWeight="ExtraBold" Foreground="#FFBE3636"></Button>
</Grid>
</Border>
</Window>
这是ViewModel
public class Window1ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected static Window1ViewModel viewModelInstance = null;
public RelayCommand CloseWindowCommand { get; set; }
public Window1ViewModel()
{
CloseWindowCommand = new RelayCommand(CloseWindow);
}
public static Window1ViewModel Instance
{
get
{
lock (typeof(Window1ViewModel))
{
if (viewModelInstance == null)
{
viewModelInstance = new Window1ViewModel();
}
}
return viewModelInstance;
}
}
protected void CloseWindow()
{
//Messenger.Default.Send<ScaleAreaWindowClosedMessage>(new ScaleAreaWindowClosedMessage());
}
event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
{
add { throw new NotImplementedException(); }
remove { throw new NotImplementedException(); }
}
}
公共类Window1ViewModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的静态Window1ViewModel viewModelInstance=null;
公共RelayCommand CloseWindowCommand{get;set;}
公共窗口1ViewModel()
{
CloseWindowCommand=新的RelayCommand(关闭窗口);
}
公共静态Window1ViewModel实例
{
得到
{
锁(类型(Window1ViewModel))
{
如果(viewModelInstance==null)
{
viewModelInstance=新的Window1ViewModel();
}
}
返回viewModelInstance;
}
}
受保护的void CloseWindow()
{
//Send(新的ScaleAreaWindowClosedMessage());
}
事件属性ChangedEventHandler INotifyPropertyChanged.PropertyChanged
{
添加{抛出新的NotImplementedException();}
删除{抛出新的NotImplementedException();}
}
}
提前感谢虽然我承认Visual Studio偶尔会出错,但当它告诉我需要添加引用时,我通常只添加它。我倾向于不去质疑它,因为它通常会告诉你为什么你也需要它。在您的例子中,它表示“System.Windows.Input.ICommand”类型是在未引用的程序集中定义的。没有比这更简单的了 现在关于
System.Windows
在哪里的问题,a会告诉您它在WindowsBase
dll中
最后,为什么引用的程序集使用System.Windows.Input
来自WindowsBase
dll而不是PresentationCore
dll?谁知道。。。你需要知道的是它不会。。。它使用了WindowsBase
dl中的一个,因此您需要引用它来解决问题
更新>>>
如果您只使用该MVVM工具包,以便可以使用
RelayCommand
,那么您最好基于原始类创建自己的类(甚至从原始类复制)。您可以在Josh Smith关于MSDN的文章中找到RelayCommand
的原始实现。这样,您根本不需要引用该dll。应该有系统。Windows是否签入了扩展?否,当我添加引用时,我找不到它。但为什么需要它呢?我的意思是在PresentationCore中定义的ICommand…System.Windows.Input需要它,而不是在PresentationCore中定义ICommand。Core,它看起来像是在.net 2.0上运行,在可执行项目上按properties,然后检查它针对的是什么框架。谢谢答案。我引用了所有DLL,但问题仍未解决。我转到了.NET 4.5,但仍然存在相同的问题…只要引用PresentationCore和System.Windows,就会发生此错误。这只有在升级到VS 2013后,我使用MVVM工具包lightOk的RelayCommand时才会出现。谢谢大家