缺少使用指令或程序集参考 WPF 和 XAML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16614055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing a Using Directive or an Assembly Reference WPF & XAML
提问by Andrew
I am having a problem where, INotifyPropertyChanged, PropertyChanagedEventHandler, and PropertyEventChangedArgs, can not be found:
我遇到了找不到 INotifyPropertyChanged、PropertyChangedEventHandler 和 PropertyEventChangedArgs 的问题:
"The type or namespace name 'PropertyChangedEventArgs' could not be found (are you missing a using directive or an assembly reference?)"
“找不到类型或命名空间名称‘PropertyChangedEventArgs’(您是否缺少 using 指令或程序集引用?)”
C#:
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:
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>
回答by Jon Skeet
The compiler's absolutely right. You need:
编译器是完全正确的。你需要:
using System.ComponentModel;
... in your usingdirectives, as PropertyChangedEventArgsis in that namespace. (Or you could fully-qualify every reference to it, of course.)
...在您的using指令中,就像PropertyChangedEventArgs在该命名空间中一样。(当然,或者您可以完全限定对它的每个引用。)
The first thing to do whenever you get this error message is to find out what namespace and assembly the relevant type is in, and check your usingdirectives and assembly references... just as the compiler message is suggesting, basically.
每当您收到此错误消息时,首先要做的是找出相关类型所在的命名空间和程序集,并检查您的using指令和程序集引用……就像编译器消息所暗示的那样,基本上。

