5

我正在创建一个 UI,它将显示类似于分形和细胞自动化的生成模式,但它们将不断生成和自动化。

在此处输入图像描述

像素和像素颜色将在用户控件中显示为正方形网格。我已经创建了用户控件来显示它,但由于它在每个 timer.tick 不断计算,它会大大减慢 UI 的其余部分并导致所有其他元素结结巴巴。

所以我研究了线程并在 BackgroundWorker DoWork() 中设置了“计算”部分,但最终并没有按照我想要的方式运行。BackgroundWorker 正在使用来自主线程(Rectangle[400])的数据,所以我不得不使用Dispatcher.Invoke(new Action(() => { }));它,这完全违背了目的,程序仍然运行得很“卡顿”。

那么,我怎样才能完全在一个单独的线程上创建一个 usercontrol...name:display_control 并让它出现在另一个 usercontrol...name:unsercontrol1 (在主线程中运行),?然后我可以将 user_input 数据与 usercontrol1User_Input_Class实例进行数据绑定。

或者,有没有更好的方法来实现这一目标?我能想到的唯一其他方法是简单地为显示创建一个完全独立的程序并共享一个包含 user_input 数据的文件,这是非常不专业的。

public partial class Fractal_Gen_A : UserControl
{
    byte W_R = 0;
    byte W_G = 255;
    byte W_B = 0;

    int Pixel_Max_Width = 20;
    int Pixel_Max_Height = 20;

    Color[] Pixel_Color = new Color[20 * 20]; //Width_Max * Canvas_Height_Count
    Rectangle[] Pixel = new Rectangle[20 * 20];
    Color[] Temp_Color = new Color[20 * 20];

    BackgroundWorker worker = new BackgroundWorker();
    private void Timer_Tick(object sender, EventArgs e)
    {
        try { worker.RunWorkerAsync(); }
        catch {}

    }

    Function_Classes.Main_Binder.FGA LB = new Function_Classes.Main_Binder.FGA(); //LB = local Binder
    DispatcherTimer Timer = new DispatcherTimer();

    public Fractal_Gen_A()
    {   
        LB.Brush = new SolidColorBrush[Pixel_Max_Width * Pixel_Max_Height];
        InitializeComponent();
        DataContext = LB;

        Set_Up_Binded_Brushes();
        worker.DoWork += Worker_Work;

        Timer.Tick += new EventHandler(Timer_Tick);
        Timer.Interval = new TimeSpan(0, 0, 0, 0, 300);            
    }             

    private void Set_Up_Binded_Brushes()
    {            
        for (int i = 0; i < Pixel_Max_Width *Pixel_Max_Height; i++)
        {
            LB.Brush[i] = new SolidColorBrush(Color.FromRgb(255, 0, 0));
        }
    }

    private delegate void UpdateMyDelegatedelegate(int i);
    private void UpdateMyDelegateLabel(int i){}

    private void Worker_Work(object sender, DoWorkEventArgs e)
    {
            try
            {
                BackgroundWorker Worker = sender as BackgroundWorker;
                SolidColorBrush[] Temp_Brush = new SolidColorBrush[Pixel_Max_Height * Pixel_Max_Width];                    

                for (int h = 0; h < Pixel_Max_Height - 1; h++)
                {
                    for (int w = 0; w < Pixel_Max_Width; w++)
                    {                            
                       Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() => { 
                            Temp_Brush[((h + 1) * Pixel_Max_Width) + w] = new SolidColorBrush();
                            Temp_Brush[((h + 1) * Pixel_Max_Width) + w].Color = LB.Brush[(h * Pixel_Max_Width) + w].Color; 
                        }));
                    }
                } 

                W_R += 23;
                for (int w = 0; w < Pixel_Max_Width; w++)
                {
                    W_B += 23 % 255;
                    W_R += 23 % 255;

                    Temp_Brush[w].Color = Color.FromRgb(W_R, W_B, W_G);                        
                }

                 Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() => {
                    Array.Copy(Temp_Brush, 0, LB.Brush, 0, Pixel_Max_Height * Pixel_Max_Width);
                 }));

                UpdateMyDelegatedelegate UpdateMyDelegate = new UpdateMyDelegatedelegate(UpdateMyDelegateLabel);
            }

            catch { }           
    }

    private void Worker_Done(object sender, RunWorkerCompletedEventArgs e)
    {

    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {            
        double X_Set = Pixel_Canvas.ActualWidth / Pixel_Max_Width;
        double Y_Set = Pixel_Canvas.ActualHeight / Pixel_Max_Height;

        for (int h = 0; h < Pixel_Max_Height; h++)
        {
            for (int w = 0; w < Pixel_Max_Width; w++)
            {
                    Pixel_Color[(h * Pixel_Max_Width) + w] = Color.FromRgb(255, 0, 0);
                    Pixel[(h * Pixel_Max_Width) + w] = new Rectangle();
                    Pixel[(h * Pixel_Max_Width) + w].Width = Pixel_Canvas.ActualWidth / Pixel_Max_Width;
                    Pixel[(h * Pixel_Max_Width) + w].Height = Pixel_Canvas.ActualHeight / Pixel_Max_Height;
                    Pixel[(h * Pixel_Max_Width) + w].Stroke = new SolidColorBrush(Color.FromRgb(100, 100, 100)); //Pixel_Color[(h * Pixel_Max_Width) + w] 
                    Pixel[(h * Pixel_Max_Width) + w].StrokeThickness = .5;    
                    Pixel[(h * Pixel_Max_Width) + w].Fill = new SolidColorBrush(Color.FromRgb(255, 0, 0)); //Pixel_Color[(h * Pixel_Max_Width) + w]
                    Canvas.SetLeft(Pixel[(h * Pixel_Max_Width) + w], (X_Set * w) + 0);
                    Canvas.SetTop(Pixel[(h * Pixel_Max_Height) + w], (Y_Set * h) + 0);
                    Pixel_Canvas.Children.Add(Pixel[(h * Pixel_Max_Height) + w]);

                    int Temp_Count = (h * Pixel_Max_Width) + w;
                   string Temp_Bind = "Brush[" + Temp_Count.ToString() + "]";
                   Binding Bind = new Binding(Temp_Bind);
                   Pixel[(h * Pixel_Max_Height) + w].SetBinding(Rectangle.FillProperty, Bind ); 

                // Dispatcher.Invoke(new Action(() => { }));
                   Timer.Start();
            }
        }
        Timer.Start();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Window pw = new PopUp_Window();            
        pw.Show();            
    }
}

基本上,我使用用户控件作为视图,2 个将始终显示,一个在左侧,一个在右侧。

4

1 回答 1

10

行。删除所有代码并重新开始。

如果你正在使用 WPF,你真的需要接受WPF 心态

这就是您在 WPF 中执行此操作的方式:

<Window x:Class="MiscSamples.Fractals"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Fractals" Height="300" Width="300">
    <ItemsControl ItemsSource="{Binding Cells}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="{Binding Size}" Columns="{Binding Size}"/>            
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Gray" BorderThickness="2">
                    <Border.Background>
                        <SolidColorBrush Color="{Binding Color,Mode=OneTime}"/>
                    </Border.Background>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

代码背后:

public partial class Fractals : Window
{
    public Fractals()
    {
        InitializeComponent();
        DataContext = new FractalViewModel();
    }
}

视图模型:

public class FractalViewModel:PropertyChangedBase
{
    private ObservableCollection<FractalCell> _cells;
    public int Rows { get; set; }

    public int Columns { get; set; }

    public ObservableCollection<FractalCell> Cells
    {
        get { return _cells; }
        set
        {
            _cells = value;
            OnPropertyChanged("Cells");
        }
    }

    public FractalViewModel()
    {
        var ctx = TaskScheduler.FromCurrentSynchronizationContext();

        Task.Factory.StartNew(() => CreateFractalCellsAsync())
                    .ContinueWith(x => Cells = new ObservableCollection<FractalCell>(x.Result), ctx);
    }

    private List<FractalCell> CreateFractalCellsAsync()
    {
        var cells = new List<FractalCell>();
        var colors = typeof(Colors).GetProperties().Select(x => (Color)x.GetValue(null, null)).ToList();
        var random = new Random();

        for (int i = 0; i < 32; i++)
        {
            for (int j = 0; j < 32; j++)
            {
                cells.Add(new FractalCell() { Row = i, Column = j, Color = colors[random.Next(0, colors.Count)] });
            }
        }

        return cells;
    }
}

数据项:

public class FractalCell:PropertyChangedBase
{
    public int Row { get; set; }

    public int Column { get; set; }

    public Color Color { get; set; }
}

PropertyChangedBase 类:

public class PropertyChangedBase:INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) 
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

结果:

在此处输入图像描述

  • 请注意我没有在过程代码中操作任何 UI 元素。一切都通过简单的 Properties 和 INotifyPropertyChanged完成。这就是您在 WPF 中编程的方式。
  • 我正在使用ItemsControl带有UniformGrid和简单DataTemplate的单元格。
  • 该示例正在生成随机颜色,但您可以从您喜欢的任何数据源中获取它。请注意,数据与 UI 完全分离,因此您可以更轻松地操作自己的简单类,而不是复杂而神秘的 WPF 对象模型。
  • 它还使您更容易实现多线程方案,因为您再次处理的不是 UI,而是数据。请记住,UI 只能在 UI Thread 中进行操作
  • WPF 摇滚。只需将我的代码复制并粘贴到 a 中File -> New Project -> WPF Application,然后自己查看结果。
  • 如果您需要进一步的帮助,请告诉我。
于 2013-10-16T04:03:15.990 回答