根据窗口大小随机放置对象

时间:2016-04-01 17:13:39

标签: c# wpf

我试图根据窗口大小将对象放在随机位置。 LayoutRoot是其所在网格的名称。

     //Give Dot a random position
     int left = random.Next(LayoutRoot.MinWidth, LayoutRoot.MaxWidth);
     int top = random.Next(-900, 900);
     Dot.Margin = new Thickness(left, top, 0, 0);

LayoutRoot.MinWidth&的错误MaxWidth:Cannot convert double to int

尝试

    //Give Dot a random position
    double left = random.NextDouble(LayoutRoot.MinWidth, LayoutRoot.MaxWidth);
    double top = random.Next(-900, 900);
    Dot.Margin = new Thickness(left, top, 0, 0);

NextDouble出错:Method NextDouble takes 2 arguments

4 个答案:

答案 0 :(得分:0)

这可能有效。

double mWidth = LayoutRoot.MinWidth;
double mxWidth = LayoutRoot.MaxWidth;
double left = random.NextDouble(mWidth, mxWidth);
double top = random.Next(-900, 900);
Dot.Margin = new Thickness(left, top, 0, 0);
}

答案 1 :(得分:0)

好好再试一下这个。修改了你的代码。此代码不假定任何预定义的高度或宽度,它根据LayoutRoot的当前大小得到。它还会抵消Dot大小,因此它不会从屏幕上掉下来。

MainWindow.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="MainWindow" Height="350" Width="525">
    <Grid x:Name="LayoutRoot">
        <Ellipse Width="2" Height="2" Fill="Black" x:Name="Dot"></Ellipse>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow
    {
        private static Random random = new Random();
        public MainWindow()
        {
            InitializeComponent();
            // Don't move dot until the window is loaded, not necessary but generally you don't want to hold up the window from displaying.
            this.Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            // 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 = LayoutRoot.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 = LayoutRoot.ActualHeight - Dot.Height;


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

        private double RandomBetween(double min, double max)
        {
            return random.NextDouble() * (max - min) + min;
        }
    }
}

答案 2 :(得分:0)

也许这会有所帮助,这是一个关于在画布上设置随机点的问题,也许可以调整以满足您的需求wpf-rendering-a-canvas-at-random-points

答案 3 :(得分:0)

因此,在查看了凯利给出的答案后,我遇到了类似的问题,即对象在加载后将不会显示。我的目标是让应用程序的欢迎屏幕每10秒随机移动一次。因此,按照他的设计,存在一个问题,因为由于缺乏列/行定义,网格没有实际长度,因此我们不得不在每次尝试随机化时都强制应用重新计算大小。

这是我使用的XAML:

<Grid x:Name="LayoutRoot">
    <TextBlock x:Name="WelcomeTextBlock" 
               Height="200" 
               Style="{StaticResource WelcomeTextBlock}">
        Welcome!
    </TextBlock>
    <Ellipse Width="2" Height="2" x:Name="Dot"/>
</Grid>

更重要的是后面的实际代码:

public partial class WelcomePage : Page
    {
        private static Random Rnd = new Random();

        private static DispatcherTimer _timer;

        public WelcomePage()
        {
            InitializeComponent();
            this.Loaded += OnLoaded;
            this.Unloaded += OnClosing;
        }

        private void OnLoaded(object sender, RoutedEventArgs args)
        {
            this.LayoutRoot.Margin = GetNewMargin();
            _timer = new DispatcherTimer {Interval = TimeSpan.FromSeconds(5)};
            _timer.Tick += MoveWelcome;
            _timer.Start();
        }

        private void MoveWelcome(object sender, EventArgs e)
        {
            this.LayoutRoot.Margin = GetNewMargin();
        }

        private Thickness GetNewMargin()
        {
            this.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            this.Arrange(new Rect(0, 0, this.DesiredSize.Width, this.DesiredSize.Height));
            var maxLeft = this.LayoutRoot.ActualWidth - Dot.Width;
            var maxTop = LayoutRoot.ActualHeight - Dot.Height;
            var left = RandomBetween(0, maxLeft);
            var top = RandomBetween(0, maxTop);
            return new Thickness(left, top, 0 ,0);
        }

        private static void OnClosing(object sender, RoutedEventArgs args)
        {
            _timer.Stop();
        }

        private static double RandomBetween(double min, double max) => Rnd.NextDouble() * (max - min) + max;

除计时器功能外,这里的主要变化是,在计算页边距厚度时,我们强制窗口,或者在我的情况下,页面会更新屏幕尺寸并正确安排项目。

相关问题