1

もぐらたたきタイプのゲームの作成中に問題が発生しました。もぐらが動的に表示される画像を作成しようとしていますが、スタック パネルがある空白の白い画面しかありません。私は初心者であると言っても過言ではありません。

これは、これらの画像を作成しようとしている私のループです:

        Image[] ImageArray = new Image[50];
        InitializeComponent();
        //string ImageName = "Image";
        for (int i = 0; i <= 8; i++)
        {
            Image Image = new Image();
            ImageArray[i] = Image;
            Image.Name = "Image" + i.ToString();
            StackPanel1.Children.Add(ImageArray[i]);
        }

        //Random Number Generator
        Random rnd = new Random();
        int num = rnd.Next(1, 9);

        //If Random Number is "1" Then Image will display
        if (num == 1)
        {
            ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole));
            ImageArray[1].Source = MoleImage;
        }

これは StackPanel XAML です。

    <Window x:Name="Window1" x:Class="WhackaMole.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="468.843" Width="666.045" OpacityMask="#FFF70D0D"                         Icon="mole2.png" Cursor="" >
<Grid OpacityMask="#FF5D1313">
    <Image Margin="422,191,-185,-69" Source="mole2.png" Stretch="Fill" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>

    <TextBlock HorizontalAlignment="Left" Margin="35,31,0,0" TextWrapping="Wrap"             VerticalAlignment="Top" Height="52" Width="595" FontSize="50" FontFamily="SimHei"><Run Language="en-ca" Text="Can You Catch the Mole?"/></TextBlock>
    <Button x:Name="NewGameBttn" Content="New Game" HorizontalAlignment="Left" Margin="77,0,0,16" VerticalAlignment="Bottom" Width="139" Height="50" FontSize="25" Click="NewGameBttn_Click"/>
    <Button x:Name="CloseBttn" Content="Close" HorizontalAlignment="Left" Margin="245,365,0,0" VerticalAlignment="Top" Width="76" Height="50" FontSize="29" Click="CloseBttn_Click"/>
    <StackPanel x:Name="StackPanel1" HorizontalAlignment="Left" Height="231" Margin="35,112,0,0" VerticalAlignment="Top" Width="525"/>

</Grid>
</Window>
4

2 に答える 2

3

私が知る限り、タイプの新しいオブジェクトを作成していますImageが、Image実際には表示するものは何もありません。の を設定する必要がSourceありますImageMSDNから盗んだ例を次に示します。

Image myImage = new Image();
myImage.Source = new BitmapImage(new Uri("myPicture.jpg", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(myImage);

townsean が指摘したように、などの一般的なプロパティを設定できるStylefor を作成する必要があります。ImageHeightWidth

于 2013-05-07T17:46:47.393 に答える
2

私の推測では、アイテムを aStackPanelに追加しているStackPanelため、画像の高さと幅のデフォルトの最小値 (おそらく 0) を選択しているため、何も表示されません。

画像の高さと幅の値を設定してみて、何か表示されるかどうかを確認してください。

また、Tejasが指摘したように、画像ソースを設定していません。

編集:画像の幅を次のように設定します:

Image myImage = new Image();
myImage.Width = 25;
myImage.Height = 25;

最初に画像を作成する for ループでこのようなことを行います。

于 2013-05-07T17:47:32.090 に答える