在VS 2015中运行C#app时的空白表格?

时间:2016-12-08 14:05:33

标签: c# visual-studio xaml

我正在尝试学习C#并从书中做示例(我使用的是Microsoft Visual Studio社区2015),但不幸的是,第一个给了我一个问题。任务是创建一个简单的游戏,但是当我运行它时,我看到一个空白的白色表单,没有任何内容。请帮助我,我很乐意继续学习。

MainPage.xaml.cs中:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Animation;

namespace Ratowanie_ludzi
   {

public sealed partial class MainPage : Page
{
    Random random = new Random();

    private void startButton_Click(object sender, RoutedEventArgs e)
    {
        AddEmeny();
    }

    private void AddEmeny()
    {
        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 animation = new DoubleAnimation()
        {
            From = from,
            To = to,
            Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6)))
        };
        Storyboard.SetTarget(animation, enemy);
        Storyboard.SetTargetProperty(animation, propertyToAnimate);
        storyboard.Children.Add(animation);
        storyboard.Begin();
        }
    }
}

MainPage.xaml中:

<Page
    x:Class="Ratowanie_ludzi.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Ratowanie_ludzi"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.Resources>
        <ControlTemplate x:Key="EnemyTemplate" TargetType="ContentControl">
            <Grid>
                <Ellipse Fill="Red" Height="100" Stroke="Black" Width="100"/>
            </Grid>
        </ControlTemplate>
    </Page.Resources>

    <Grid Background="{ThemeResource AppBarButtonBackground}" Height="1060" Width="1900" Margin="0" VerticalAlignment="Center" HorizontalAlignment="Center">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="160"/>
            <ColumnDefinition/>
            <ColumnDefinition Width="158.419"/>
            <ColumnDefinition Width="1.581"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition/>
            <RowDefinition Height="160"/>
        </Grid.RowDefinitions>
        <Button x:Name="startButton" Content="Start!" HorizontalAlignment="Center" Margin="53,64,52,64" Grid.Row="2" VerticalAlignment="Center" Click="startButton_Click"/>
        <StackPanel Grid.Column="2" Orientation="Vertical" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="14,7,11.419,7">
            <TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="Ich unikaj" Style="{StaticResource SubheaderTextBlockStyle}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <ContentControl Content="ContentControl" HorizontalAlignment="Center" VerticalAlignment="Center" Template="{StaticResource EnemyTemplate}"/>
        </StackPanel>
        <ProgressBar Grid.Column="1" Grid.Row="2" Height="20" Margin="0,92,0,48"/>
        <Canvas x:Name="playArea" Grid.Row="1" Grid.ColumnSpan="4">
            <Canvas.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="#FFA640EE" Offset="1"/>
                </LinearGradientBrush>
            </Canvas.Background>
            <StackPanel x:Name="human" Orientation="Vertical" Canvas.Left="10" Canvas.Top="10">
                <Ellipse Fill="Red" Height="30" Stroke="Black" Width="30"/>
                <Rectangle Fill="Red" Height="55" Stroke="Black" Width="30"/>
            </StackPanel>
            <TextBlock x:Name="gameOverText" Canvas.Left="686" TextWrapping="Wrap" Canvas.Top="355" FontFamily="Arial Black" FontSize="100" FontStyle="Italic" FontWeight="Bold" FocusVisualPrimaryBrush="Black" Foreground="White">
                <Run Text="Koniec "/>
                <Run Text="gry"/>
            </TextBlock>
            <Rectangle x:Name="target" Height="80" Stroke="Black" Width="80" Canvas.Left="894" Canvas.Top="162">
                <Rectangle.Fill>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </Rectangle.Fill>
                <Rectangle.Projection>
                    <PlaneProjection RotationZ="45"/>
                </Rectangle.Projection>
            </Rectangle>
        </Canvas>
    </Grid>
</Page>

由于

1 个答案:

答案 0 :(得分:1)

你错过了我认为的窗口的构造函数:尝试这样的事情:     公共MainPage()             {                 的InitializeComponent();     }