如何在不同的线程上创建用户控件?

时间:2013-10-16 02:59:31

标签: c# wpf multithreading image user-controls

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

enter image description here

像素和像素颜色将在用户控件中显示为正方形网格。我已经创建了usercontrol来显示它,但是因为它在每个计时器上不断计算。它会大大减慢UI的其余部分并导致所有其他元素断断续续。

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

那么,我如何创建一个usercontrol ... name:display_control完全在一个单独的线程上并让它出现在另一个usercontrol ... name:unsercontrol1(在主线程中运行),?然后我可以使用usercontrol1 User_Input_Class实例对user_input数据进行数据绑定。

或者,有没有更好的方法来实现这一目标?只有我能想到这样做的其他方法是简单地为显示创建一个完全独立的程序,并共享一个包含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();            
    }
}

基本上,我使用usercontrols作为视图,2将一直显示,一个在左边一个。

1 个答案:

答案 0 :(得分:10)

确定。删除所有代码并从头开始。

如果你正在使用WPF,你真的需要拥抱The WPF Mentality

这是你在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));
    }
}

结果:

enter image description here

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