我遇到了一个问题,无法找到 INotifyPropertyChanged、PropertyChangedEventHandler 和 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>