控制不能以编程方式正确定位

时间:2016-04-08 13:06:01

标签: c# wpf xaml controls

所以我试图在网格内的随机位置放置Ellipse (named Dot) 但它只在网格的1/4(右下角)而不是整个网格上产生 示例:当我将点的边距设置为Dot.Margin = new Thickness(0, 0, 0, 0);时,点将在屏幕的中心生成。
当我将其更改为Dot.Margin = new Thickness(200, 0, 0, 0);时,它将具有非常小的偏移量对,但甚至不接近200像素 创建大量圆圈而不删除它们后的结果: http://i.imgur.com/3XCMYyC.png
红色矩形是产卵区域。

C#

//Gives Dot a position
public void placeDot()
{
    //Give Dot random position
    // The farthest left the dot can be
    double minLeft = 0;
    // The farthest right the dot can be without it going off the screen
    double maxLeft = spawnArea.ActualWidth - Dot.Width;
    // The farthest up the dot can be
    double minTop = 0;
    // The farthest down the dot can be without it going off the screen
    double maxTop = spawnArea.ActualHeight - Dot.Height;


    double left = RandomBetween(minLeft, maxLeft);
    double top = RandomBetween(minTop, maxTop);
    Dot.Margin = new Thickness(left, top, 0, 0);
}

//createEllipse method, used for creating new Dots
public void createEllipse()
{
    spawnArea.Children.Remove(Dot);

    //Create new Dot
    DotImg.ImageSource =
        new BitmapImage(new Uri(@"Images/hitcircle.png", UriKind.Relative));
    Dot = new Ellipse() { Width = hitcircleSettingPath, Height = hitcircleSettingPath, Fill = DotImg, };

    //Activates placeDot() method to give the Dot a random location
    placeDot();

    //Add Dot to the game area
    spawnArea.Children.Add(Dot);
}

XAML这是完整的XAML我认为XAML可能有问题,但下面是一个较短的片段,它可以按照我想要的方式工作,但没有其他东西。

<Window x:Name="Grid" x:Class="ReactieSnelheid_Game.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:ReactieSnelheid_Game"
        mc:Ignorable="d"
        Title="Dot Program" Height="768" Width="1366" WindowStartupLocation="CenterScreen" Cursor="Pen" Icon="Images/icon.ico" ResizeMode="NoResize" WindowState="Maximized" WindowStyle="None" Background="Black" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="{x:Null}" KeyDown="Grid_KeyDown">
    <Grid x:Name="backgroundImageGrid" Margin="0,0,0,0" MouseDown="LayoutRoot_MouseDown">
        <Grid.Background>
            <ImageBrush x:Name="backgroundBrush" ImageSource="Images/background.png" Opacity="0.25"/>
        </Grid.Background>
        <Grid x:Name="gameWrapper">
            <Grid.Background>
                <SolidColorBrush Color="Black" Opacity="0.25"/>
            </Grid.Background>
            <Label x:Name="reactionTime" Content="1000ms" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" FontSize="40" FontFamily="PMingLiU-ExtB" Foreground="White"/>
            <Label x:Name="circleSize" Content="150x150" HorizontalAlignment="Left" Margin="10,73,0,0" VerticalAlignment="Top" FontSize="26.667" FontFamily="PMingLiU-ExtB" Foreground="White" Background="{x:Null}"/>
            <Label x:Name="dotCount" Content="000000" HorizontalAlignment="Left" Margin="28,0,0,-1" Foreground="White" FontSize="80" RenderTransformOrigin="0.5,0.5" FontFamily="PMingLiU-ExtB" VerticalAlignment="Bottom" Background="{x:Null}">
                <Label.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform AngleX="-11.056"/>
                        <RotateTransform/>
                        <TranslateTransform X="-8.305"/>
                    </TransformGroup>
                </Label.RenderTransform>
            </Label>
            <Label x:Name="scoreCount" Content="9999999999999999" Margin="0,0,10,10" Foreground="White" FontSize="80" FontFamily="PMingLiU-ExtB" HorizontalAlignment="Right" Height="106" VerticalAlignment="Bottom"/>
            <Rectangle x:Name="startTimerBtn" RenderTransformOrigin="0.39,-0.75" Margin="144,10,0,0" Width="128" Height="64" HorizontalAlignment="Left" VerticalAlignment="Top" MouseDown="startTimerBtn_MouseDown">
                <Rectangle.Fill>
                    <ImageBrush ImageSource="Images/starttimer.png"/>
                </Rectangle.Fill>
            </Rectangle>
            <Grid x:Name="spawnArea" Margin="0,115,0,121" Background="Red"/>
        </Grid>
        <Grid x:Name="pauseScreen">
            <Grid.Background>
                <SolidColorBrush Color="#FF81D650" Opacity="0.25"/>
            </Grid.Background>
            <Grid x:Name="pauseScreenBtns" Margin="389,153,389,152" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Rectangle x:Name="Startbtn" Height="332" Width="332" MouseDown="Startbtn_MouseDown" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="129,0,127,76">
                    <Rectangle.Fill>
                        <ImageBrush ImageSource="Images/Start.png"/>
                    </Rectangle.Fill>
                </Rectangle>
                <Rectangle x:Name="settingsBtn" MouseDown="settingsBtn_MouseDown" Margin="-5,189,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="129" Height="63">
                    <Rectangle.Fill>
                        <ImageBrush ImageSource="Images/settings.png"/>
                    </Rectangle.Fill>
                </Rectangle>
                <Rectangle x:Name="resetBtn" MouseDown="resetBtn_MouseDown" Margin="0,190,-6,0" Width="128" Height="62" HorizontalAlignment="Right" VerticalAlignment="Top">
                    <Rectangle.Fill>
                        <ImageBrush ImageSource="Images/reset.png"/>
                    </Rectangle.Fill>
                </Rectangle>
                <Rectangle x:Name="quitBtn" RenderTransformOrigin="0.39,-0.75" Margin="232,399,0,0" Width="129" Height="64" HorizontalAlignment="Left" VerticalAlignment="Top" MouseDown="quitBtn_MouseDown">
                    <Rectangle.Fill>
                        <ImageBrush ImageSource="Images/quit.png"/>
                    </Rectangle.Fill>
                </Rectangle>
            </Grid>
        </Grid>
    </Grid>
</Window>

更短的XAML

<Window x:Class="TESTPROJECTEN.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:TESTPROJECTEN"
        mc:Ignorable="d"
        Title="MainWindow" Height="768" Width="1366">
    <Grid x:Name="gameWrapper">
        <Grid x:Name="spawnArea"/>
    </Grid>
</Window>

2 个答案:

答案 0 :(得分:1)

默认情况下,Ellipse具有Alignment = Stretch。但是你设置了一个固定的宽度和高度,所以它看起来像椭圆居中。尝试相对于左上角设置位置

Dot = new Ellipse() { Width = hitcircleSettingPath, Height = hitcircleSettingPath, Fill = DotImg, };

Dot.HorizontalAlignment = HorizontalAlignment.Left;
Dot.VerticalAlignment = VerticalAlignment.Top;

答案 1 :(得分:0)

  

右边有一个非常小的偏移但是甚至不接近   200像素。

首先,据我记得,WPF不会将这些数字用作像素,而是用作某些内部值。我不记得那些单位的名称,但是底线,它们不是像素。

现在针对您的实际问题:

Wpf网格默认将所有子控件放在中心位置,因此当您将椭圆放入网格并设置边距200时,它会设置边距从中心

考虑到这一点,您可以更改随机算法以使气泡出现在整个网格中:

//Gives Dot a position
public void placeDot()
{
    //Give Dot random position
    double halfSide = (spawnArea.ActualWidth - Dot.Width) / 2;

    // The farthest left the dot can be
    double minLeft = -(halfSide - (Dot.ActualWidth / 2));
    // The farthest right the dot can be without it going off the screen
    double maxLeft = halfSide - (Dot.ActualWidth / 2);
    // The farthest up the dot can be
    double minTop = -(halfSide - (Dot.ActualHeight / 2));
    // The farthest down the dot can be without it going off the screen
    double maxTop = halfSide - (Dot.ActualHeight / 2);

    double left = RandomBetween(minLeft, maxLeft);
    double top = RandomBetween(minTop, maxTop);
    Dot.Margin = new Thickness(left, top, 0, 0);
}

我希望我没有错过任何东西。我没有运行此代码,因此可能需要调整,但这是要遵循的主线。

快乐的编码! :)

相关问题