2017-09-11 8 views
0

グリッドの背景をr、g、b値で0.5秒ごとに変更し、グリッド内のボタンとやりとりできるようにします。 問題は、定期的な間隔で背景が変わることも、グリッド内の2つのボタンと対話することもできないことです。グリッドの色は、他のアプリケーションまたはタスクマネージャに切り替えるときにのみ変化します。達成する方法?WPF:グリッドのバックグラウンドを半分ごとに変更しても、他のコントロールと相互作用します。

<Grid Name="MainGrid" Height="{Binding ElementName=MainWindow1, Path=ActualHeight}" Loaded="MainGrid_Loaded"> 
    <StackPanel VerticalAlignment="Center"> 
     <Label Height="50" Name="xCoordinate" /> 
     <Label Height="50" Name="yCoordinate" /> 
     <StackPanel HorizontalAlignment="Center" Height="50" Orientation="Horizontal"> 
      <Button Content="Red" Width="100" Name="xBtn" Click="xBtn_Click" Margin="0,0,10,0"/> 
      <Button Content="Blue" Width="100" Name="yBtn" Click="yBtn_Click" /> 
     </StackPanel> 
    </StackPanel> 
</Grid> 

public partial class MainWindow : Window 
{ 
    Thread thread; 
    public MainWindow() 
    { 
     InitializeComponent(); 
     thread = new Thread(new ThreadStart(ChangeGridColor)); 
     thread.Start(); 

    } 

    private void xBtn_Click(object sender, RoutedEventArgs e) 
    { 
     xCoordinate.Background = new SolidColorBrush(Colors.Red); 
    } 

    private void yBtn_Click(object sender, RoutedEventArgs e) 
    { 
     yCoordinate.Background = new SolidColorBrush(Colors.Blue); 
    } 

    private void MainWindow_MouseMove(object sender, MouseEventArgs e) 
    { 
     Point point = Mouse.GetPosition(Application.Current.MainWindow); 
     xCoordinate.Content = point.X; 
     yCoordinate.Content = point.Y; 

    } 

    byte r=0,g=0,b = 0; 
    public void ChangeGridColor() 
    { 
     while (true) 
     { 
      this.Dispatcher.Invoke((Action)(() => 
      {//this refer to form in WPF application 
       MainGrid.Background = new SolidColorBrush(Color.FromRgb(r, g, b)); 
       r += 1; 
       g += 1; 
       b += 1; 
      })); 

      Thread.Sleep(1000); 
     } 
    } 
} 
+0

コードでそれを行うのはなぜ? XAMLの数行でWPFのストーリーボードを使用する – MickyD

答えて

0

あなたはただ眠るために2番目のスレッドは必要ありません。あなたはタイマーを使うことができます。タイマーは、一定の間隔でコールバックを呼び出します。

public partial class MainWindow : Window 
{ 
    private readonly System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer() 

    public MainWindow() 
    { 
     InitializeComponent(); 
     _timer.Interval = 500; // 500ms. 
     _timer.Elapsed += ElapsedCallback; 
     _timer.Start(); 
    } 

    private void ElapsedCallback(object sender, EventArgs e) 
    { 
     MainGrid.Background = new SolidColorBrush(Color.FromRgb(r, g, b)); 
     r = ++r % 0xFF; // prevent overlfow. 
     g = ++g % 0xFF; 
     b = ++g % 0xFF; 
    } 

    private void xBtn_Click(object sender, RoutedEventArgs e) 
    { 
     xCoordinate.Background = new SolidColorBrush(Colors.Red); 
    } 

    private void yBtn_Click(object sender, RoutedEventArgs e) 
    { 
     yCoordinate.Background = new SolidColorBrush(Colors.Blue); 
    } 

    private void MainWindow_MouseMove(object sender, MouseEventArgs e) 
    { 
     Point point = Mouse.GetPosition(Application.Current.MainWindow); 
     xCoordinate.Content = point.X; 
     yCoordinate.Content = point.Y; 

    } 
} 
-1

あなたのコードは、あなたのループがバックグラウンドスレッドで実行する場合は、あなたが期待するように動作する必要があります

byte r = 0, g = 0, b = 0; 
public void ChangeGridColor() 
{ 
    Task.Run(() => 
    { 
     while (true) 
     { 
      this.Dispatcher.Invoke((Action)(() => 
      { 
       MainGrid.Background = new SolidColorBrush(Color.FromRgb(r, g, b)); 
      })); 

      r += 1; 
      g += 1; 
      b += 1; 
      Thread.Sleep(1000); 
     } 
    }); 
} 
関連する問題