簡體   English   中英

GalaSoft.MvvmLight.WP71編譯錯誤,需要為RelayCommand添加對System.Windows的引用

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

我正在VS2010中使用MVVM Light工具包的V3,目標是.NET FW 4.0。作為MVVM Light工具包用法的一部分,我正在使用實現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”類型...

怎么了

這是查看代碼

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

提前致謝

盡管我承認Visual Studio有時是錯誤的,但是當它告訴我需要添加引用時,通常只添加它即可。 我傾向於不質疑它,因為它通常會告訴您為什么也需要它。 在您的情況下,它說類型'System.Windows.Input.ICommand'是在未引用的程序集中定義的 沒有比這更簡單的了。

現在關於您的問題在哪里System.Windows ,一個快速的在線搜索將告訴您它在WindowsBase dll中。

最后,為什么引用的程序集使用WindowsBase dll而不是PresentationCore dll的System.Windows.Input 誰知道...您所需要知道的是,它沒有...它使用WindowsBase dl中的一個,因此您需要參考它來解決問題。


更新>>>

如果您僅使用該MVVM Toolkit,以便可以使用RelayCommand則還可以基於原始(甚至從原始副本復制)創建自己的類。 您可以在RelayCommand上的Josh Smith的“ 帶有Model-View-ViewModel設計模式WPF應用程序RelayCommand中找到RelayCommand的原始實現。 這樣,您根本不需要引用該dll。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM