gpt4 book ai didi

c# - 缺少使用指令或程序集引用 WPF 和 XAML

转载 作者:太空宇宙 更新时间:2023-11-03 18:35:07 26 4
gpt4 key购买 nike

<分区>

我遇到一个问题,无法找到 INotifyPropertyChanged、PropertyChanagedEventHandler 和 PropertyEventChangedArgs:

"The type or namespace name 'PropertyChangedEventArgs' could not be found (are you missing a using directive or an assembly reference?)"

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>

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com