简体   繁体   中英

GalaSoft.MvvmLight.WP71 Compilation error , require to add reference to System.Windows for the RelayCommand

I am using V3 of the MVVM Light Toolkit in VS2010 and target .NET FW of 4.0 As part of the MVVM Light toolkit usage I am using the RelayCommand class which implements the ICommand interface. I am also referencing the Presentation.Core assembly for the ICommand interface. Somehow at compilation I am getting the following error which not occur at previous versions of MVVM Light...

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'.

Now, I cannot find any System.Windows assembly and why it doesn't use the 'System.Windows.Input.ICommand' type from the PresentationCore assembly...

What's wrong here ?

This is the View code

<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>

This is the 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(); }
        }
    }

Thanks in advance

While I accept that Visual Studio is occasionally wrong, when it tells me that I need to add a reference, I usually just add it. I tend not to question it, as it usually tells you why you need it as well. In your case, it says The type 'System.Windows.Input.ICommand' is defined in an assembly that is not referenced . It doesn't come much simpler than that.

Now regarding your question about where is System.Windows , a quick online search would have told you that it is in the WindowsBase dll.

Finally, why is your referenced assembly using the System.Windows.Input from the WindowsBase dll instead of the PresentationCore dll? Who knows... all you need to know is that it doesn't... it uses the one from the WindowsBase dl, so that is the one that you need to reference to fix your problem.


UPDATE >>>

If you are only using that MVVM Toolkit so that you can use the RelayCommand then you might as well create your own class based on (or even copied from) the original. You can find the original implementation of the RelayCommand in Josh Smith's WPF Apps With The Model-View-ViewModel Design Pattern article on MSDN. That way, you won't need to reference that dll at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM