缺少Using指令或汇编参考WPF &XAML

本文关键字:WPF ampXAML 参考 汇编 Using 指令 缺少 | 更新日期: 2023-09-27 18:14:14

我遇到了一个问题,INotifyPropertyChanged, propertychangagedeventhandler和PropertyEventChangedArgs,找不到:

"类型或命名空间名称'PropertyChangedEventArgs'不能发现(您是否缺少using指令或程序集引用?)"

c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WhackaMole_MVVM
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new WhackAMoleViewModel();
        }
    }
    public class WhackAMoleViewModel : PropertyChangedBase
    {
        private List<Mole> _moles;
        public List<Mole> Moles
        {
            get { return _moles; }
        }
        private System.Threading.Timer timer;
        private System.Random random = new Random();
        public WhackAMoleViewModel()
        {
            _moles = Enumerable.Range(1, 9).Select(x => new Mole()).ToList();
            timer = new Timer(x => RaiseRandomMole(), null, 0, 300);
        }
        private void RaiseRandomMole()
        {
            //If random number is less than 5 skip this iteration
            if (random.Next(1, 10) > 5)
                return;
            //Choose a random mole
            var mole = Moles[random.Next(0, 8)];
            //If it's already raised, do nothing
            if (mole.IsUp)
                return;
            //Raise it
            mole.IsUp = true;
            //Then Get it down somewhere between 1 and 2 seconds after
            Task.Factory.StartNew(() => Thread.Sleep(random.Next(1000, 2000)))
                        .ContinueWith(x => mole.IsUp = false);
        }
    }

    public class Mole : PropertyChangedBase
    {
        private bool _isUp;
        public bool IsUp
        {
            get { return _isUp; }
            set
            {
                _isUp = value;
                OnPropertyChanged("IsUp");
            }
        }
    }
    public class PropertyChangedBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            Application.Current.Dispatcher.BeginInvoke((Action)(() =>
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }));
        }
    }
}
XAML:

<Window x:Class="WhackaMole_MVVM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <ItemsControl ItemsSource="{Binding Moles}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="3" Columns="3" IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Image x:Name="Mole" Height="0" Width="100" 
                       Source="C:'Users'MonAmi'Documents'Visual Studio 2012'Projects'WhackaMole'WhackaMole'mole2.png"
                       Stretch="Fill"
                       VerticalAlignment="Bottom"/>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding IsUp}" Value="True">
                        <DataTrigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard TargetName="Mole">
                                    <DoubleAnimation Storyboard.TargetProperty="Height" 
                                                     From="0" To="77"
                                                     Duration="00:00:00.3"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </DataTrigger.EnterActions>
                        <DataTrigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard TargetName="Mole">
                                    <DoubleAnimation Storyboard.TargetProperty="Height"
                                                     From="77" To="0"
                                                     Duration="00:00:00.3"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </DataTrigger.ExitActions>
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

缺少Using指令或汇编参考WPF &ampXAML

编译器完全正确。你需要:

using System.ComponentModel;

…在您的using指令中,因为PropertyChangedEventArgs在该命名空间中。(当然,也可以完全限定对它的每个引用。)

当你得到这个错误消息时,要做的第一件事就是找出相关类型所在的名称空间和汇编,并检查你的using指令和汇编引用…就像编译器提示的那样,基本上。