2016-04-01 17 views
1

Pencere boyutunu bağlı olarak rastgele bir yere nesne yerleştirmeye çalışıyorum. LayoutRoot içeri yerleştirmiş ızgara adıdır LayoutRoot.MinWidth & MaxWidth üzerindePencere boyutuna bağlı olarak bir nesneyi rastgele yerleştirme

 //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); 

Hata:. Cannot convert double to int

NextDouble üzerinde

//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); 

Hata çalıştı: Method NextDouble takes 2 arguments

+0

Bir 'Grid' kullanmak istediğinizden emin misiniz? Ekrana rastgele bir şey koymaya çalışıyorsanız, bir Canvas kullanmak isteyeceğinizi düşünürdüm. 'Canvas.SetX (x)' yi kullanırsınız; Canvas.SetY (y); 'Canvas' kontrolü, bu tür bir şey için tasarlanırken, 'Izgara', düzgün satır ve sütunlarda dizilmesi için tasarlanmıştır. –

cevap

0

Bunu biraz yukarı çıkacaksınız. Kodunuzu değiştirdiniz. Bu kod önceden tanımlanmış herhangi bir yükseklik veya genişlikte olmadığını varsayar, LayoutRoot'un geçerli boyutuna göre alır. Ayrıca Dot boyutunu da korur, böylece ekrandan düşmez.

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

Belki
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; 
     } 
    } 
} 
+0

Hata yok, ancak uygulamayı başlattığımda, nesne hiçbir yerde bulunamıyor mu? – Simon

+0

Bu işe yaramalı. –

+0

Bu, teşekkürler! – Simon

0

bu çalışabilir.

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); 
} 
+0

"NextDouble Yöntemi 2 argümanını tekrar alır" hatasını aldınız. – Simon

0

bu yardımcı olacaktır, bu bir tuval üzerine rastgele noktayı ayarlama hakkında bir soru, belki de tweaked olabilir ihtiyaçlarınızı karşılayın wpf-rendering-a-canvas-at-random-points

İlgili konular