2012-07-21 60 views
0

變化問題是隻有活動結束後,WPF不保存更改如何保存在WPF事件系

//code 
private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    for (int ii = 0; ii <= 5; ii++) 
    { 
     Rectangle rectr = (Rectangle)FindName("rect" + ii); 
     rectr.Fill = Brushes.Black; 
     // need some thing to save changes here 
     Thread.Sleep(100); 
    } 

} 

的問題是如何保存更改

所有我需要改變每個矩形的背景色和睡一段時間再改下矩形

+1

你在這裏試圖完成什麼?你想要保存什麼變化? – Bernard 2012-07-21 15:42:29

+0

我需要的所有東西 更改每個矩形的背景顏色並休息一段時間,然後更改下一個矩形 – 2012-07-21 15:51:35

+0

當您運行此操作時會發生什麼? – 2012-07-21 16:11:06

回答

0

嘗試類似的東西:

XAML文件:

<Window x:Class="RectangleNS.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="30" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 

     <Button Content="Change Color" Click="Button_Click" /> 

     <StackPanel Grid.Row="1" > 
      <Rectangle Name="rect1" Fill="Red" Height="50" /> 
      <Rectangle Name="rect2" Fill="Yellow" Height="50" /> 
      <Rectangle Name="rect3" Fill="Green" Height="50" /> 
      <Rectangle Name="rect4" Fill="AliceBlue" Height="50" /> 
      <Rectangle Name="rect5" Fill="DarkTurquoise" Height="50" /> 
     </StackPanel>   
    </Grid> 
</Window> 

代碼隱藏文件:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.ComponentModel; 
using System.Threading; 

namespace RectangleNS 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      BackgroundWorker bw = new BackgroundWorker(); 

      Grid grid = (Grid)(sender as Button).Parent; 

      bw.DoWork += (o, ee) => 
      { 
       for (int i = 1; i <= 5; i++) 
       { 
        Rectangle rectr = null; 
        Application.Current.Dispatcher.Invoke((Action)(() => { rectr = VTHelper.FindVisualChildByName<Rectangle>(grid, "rect" + i); })); 
        Application.Current.Dispatcher.Invoke((Action)(() => { rectr.Fill = Brushes.Black; }));     
        Thread.Sleep(100); 
       } 
      }; 

      bw.RunWorkerAsync(); 
     } 
    } 

    public static class VTHelper 
    { 
     public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject 
     { 
      for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) 
      { 
       var child = VisualTreeHelper.GetChild(parent, i); 
       string controlName = child.GetValue(Control.NameProperty) as string; 
       if (controlName == name) 
       { 
        return child as T; 
       } 
       else 
       { 
        T result = FindVisualChildByName<T>(child, name); 
        if (result != null) 
         return result; 
       } 
      } 
      return null; 
     } 
    } 
} 
+0

**謝謝**它運行成功:) – 2012-07-21 23:20:15

0

Button_Click由UI線程調用,你不應該做任何需要比在它幾毫秒以上,讓它成爲睡100毫秒。在此期間不處理消息,您的界面無響應,並且您的應用程序被系統視爲「掛起」。

那麼,那long processing task should be handled by another thread。用這個代替你的代碼,它應該適合你 -

for (int ii = 0; ii <= 5; ii++) 
      { 
       BackgroundWorker backgroundWorker = new BackgroundWorker(); 
       backgroundWorker.DoWork += (s, args) => 
        { 
         args.Result = (int)args.Argument; 
         Thread.Sleep(100); 
        }; 
       backgroundWorker.RunWorkerCompleted += (s, args) => 
       { 
        int value = (int)args.Result; 
        Rectangle rectr = (Rectangle)FindName("rect" + value); 
        rectr.Fill = Brushes.Black; 
       }; 
       backgroundWorker.RunWorkerAsync(ii); 
      } 
+0

對不起,它沒有運行成功 和**感謝您的幫助** – 2012-07-21 23:21:08

+0

您面臨什麼問題?因爲它在我的工作很好。 – 2012-07-22 05:28:41