再会。在研究 Stillman 和 Green 的书时,出现了一个问题:启动程序时没有错误,但是当按下按钮时,并没有生成敌人并将其放置在比赛场地上。
XAML 代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="Save_The_Humans" Height="700" Width="1000">
<Window.Resources>
<ControlTemplate x:Key="EmptyTemplate" TargetType="{x:Type ContentControl}">
<Grid Height="120" RenderTransformOrigin="0.5,0.5" Width="120" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="-1"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<Ellipse Fill="#FFFB0606" Stroke="Black" Width="90" Height="90" Margin="13,10,17,20" VerticalAlignment="Stretch"/>
</Grid>
</ControlTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1"/>
<RowDefinition/>
<RowDefinition Height="150"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140"/>
<ColumnDefinition/>
<ColumnDefinition Width="160"/>
</Grid.ColumnDefinitions>
<Canvas x:Name="playArea" Grid.Row="1" Grid.ColumnSpan="3" >
<Canvas.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FF00FFD1" Offset="1"/>
</LinearGradientBrush>
</Canvas.Background>
<StackPanel x:Name="human" Canvas.Left="430" Orientation="Vertical" Canvas.Top="184" Width="100">
<Ellipse Fill="White" Height="10" Width="10"/>
<Rectangle Fill="White" Height="25" Width="10"/>
</StackPanel>
<TextBlock x:Name="gameOverText" TextWrapping="Wrap" Text="GAME OVER" FontSize="100" FontFamily="Arial Black" Canvas.Left="175" Canvas.Top="274" FontStyle="Italic" FontWeight="Bold"/>
<Rectangle Height="50" Width="50" Canvas.Left="640" Canvas.Top="119" RadiusY="10" RadiusX="10" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="45"/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFDCFF00" Offset="0.359"/>
<GradientStop Color="#FFFF00F3" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Canvas>
<ProgressBar Grid.Column="1" Grid.Row="2" Height="20"/>
<ContentControl Content="ContentControl" Grid.Column="2" HorizontalAlignment="Left" Margin="10,30,0,0" Grid.Row="2" VerticalAlignment="Top" Template="{DynamicResource EmptyTemplate}" Height="100" Width="100"/>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="75" Margin="30,30,0,0" Grid.Row="2" Click="button_Click_1"/>
</Grid>
</Window>
代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Windows.Media.Animation;
namespace WpfApplication1
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Random random = new Random();
private void button_Click_1(object sender, RoutedEventArgs e)
{
AddEnemy();
}
private void AddEnemy()
{
ContentControl enemy = new ContentControl();
enemy.Template = Resources["EnemyTemplate"] as ControlTemplate;
AnimateEnemy(enemy, 0, playArea.ActualWidth - 100, "(Canvas.Left)");
AnimateEnemy(enemy, random.Next((int)playArea.ActualHeight - 100),
random.Next((int)playArea.ActualHeight - 100), "(Canvas.Top)");
playArea.Children.Add(enemy);
}
private void AnimateEnemy(ContentControl enemy, double from, double to, string propertyToAnimate)
{
Storyboard storyboard = new Storyboard()
{
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever
};
DoubleAnimation doubleanimation = new DoubleAnimation()
{
From = from,
To = to,
Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6)))
};
Storyboard.SetTarget(doubleanimation, enemy);
Storyboard.SetTargetProperty(doubleanimation, new PropertyPath(propertyToAnimate));
storyboard.Children.Add(doubleanimation);
storyboard.Begin();
}
}
}
当您写下以下内容时,为什么它们会出现:
和代码隐藏:
如果您还没有注意到,那么在第一种情况下您已经注意到了
x:Key="EmptyTemplate"
,但是您正试图退出EnemyTemplate
...PS 我还建议您学习
MVVM
和使用它。该应用程序看起来更具可读性和更简单......