WPF -- dynamic addition of custom control and use of BackgroundWorker

Keywords: Windows

I saw a blogger write BackgroundWorker on the Internet yesterday, and I feel very good. After learning and modification, keep records to prevent forgetting!
In the same position in the window, add the rotation effect picture, the code is as follows:

<Canvas Grid.Row="2" Grid.Column="1" Name="cavLoading" Visibility="Collapsed" RenderTransformOrigin="0.5,0.5"  HorizontalAlignment="Center" VerticalAlignment="Center" Width="120" Height="120">
                <Ellipse Width="15" Height="15" Canvas.Left="55" Canvas.Top="31" Stretch="Fill" Fill="#FFD1D1F7" Opacity="1.0"/>
                <Ellipse Width="15" Height="15" Canvas.Left="38" Canvas.Top="39" Stretch="Fill" Fill="Blue" Opacity="0.8"/>
                <Ellipse Width="15" Height="15" Canvas.Left="36" Canvas.Top="58" Stretch="Fill" Fill="#FF0000FE" Opacity="0.7"/>
                <Ellipse Width="15" Height="15" Canvas.Left="52" Canvas.Top="67" Stretch="Fill" Fill="Blue" Opacity="0.6"/>
                <Ellipse Width="15" Height="15" Canvas.Left="68" Canvas.Top="61" Stretch="Fill" Fill="#FF2E2EFF" Opacity="0.5"/>
                <Ellipse Width="15" Height="15" Canvas.Left="69" Canvas.Top="42" Stretch="Fill" Fill="#FF6F6FFF" Opacity="0.4"/>
                <Canvas.RenderTransform>
                    <RotateTransform x:Name="SpinnerRotate" Angle="0" />
                </Canvas.RenderTransform>
                <Canvas.Triggers>
                    <EventTrigger RoutedEvent="ContentControl.Loaded">
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="SpinnerRotate" Storyboard.TargetProperty="(RotateTransform.Angle)" From="0" To="360" Duration="0:0:0.8" RepeatBehavior="Forever" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Canvas.Triggers>
            </Canvas>

As well as the display after rotation, the code is as follows:

<Grid Grid.Row="2" Grid.Column="1"  Name="gNewBox"  Margin="21,0,0,0" Visibility="Collapsed"  >
                <Grid.RowDefinitions>
                    <RowDefinition Height="106"/>
                    <RowDefinition Height="106"/>
                    <RowDefinition Height="106"/>
                    <RowDefinition Height="106"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="100"/>
                </Grid.ColumnDefinitions>

            </Grid>

Because I am a user-defined control that implements dynamic adding in the background, I did not add it directly in the layout. The dynamic adding code is as follows:

 public void ShowStruct()
 {
            var controls = new List<Drawer.Drawer>();
            controls.Clear();
            gNewBox.Children.Clear();           
                var id = 1;
                for (var i = 0; i < 6; i++)
                {
                    for (var j = 0; j < 4; j++)
                    {
                        var bitkyPole = new Drawer.Drawer();
                        bitkyPole.Name = "drawer" + id;
                        bitkyPole.Width = 145;
                        bitkyPole.Height = 65;
                        //Add control dynamically in Grid
                        gNewBox.Children.Add(bitkyPole);
                        //Set the position of the control in the Grid
                        Grid.SetRow(bitkyPole, 5 - i);
                        Grid.SetColumn(bitkyPole, j);
                        //Add the control to the collection for further use
                        controls.Add(bitkyPole);
                        //Use custom methods to initialize controls
                        // bitkyPole.setContent(id);                      
                        id++;
                   }
            }
 }

And the label of waiting time:

 <Label Grid.Row="1" Grid.Column="1" Width="120" Height="40" VerticalAlignment="Top" HorizontalAlignment="Left" Content="Waiting time:" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"></Label>
            <Label Grid.Row="1" Grid.Column="1" Width="120" Height="40" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="130,0,0,0" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Name="lab_pro"></Label>

Then implement the BackgroundWorker with the following code:

 BackgroundWorker bgWl;
        private void btnLoading_Click(object sender, RoutedEventArgs e)
        {
            bgWl = new BackgroundWorker();
            bgWl.WorkerReportsProgress = true;
            bgWl.DoWork += new DoWorkEventHandler(bgWl_DoWork);
            bgWl.ProgressChanged += new ProgressChangedEventHandler(bgWl_ProgressChanged);
            bgWl.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWl_RunWorkerCompleted);
            bgWl.RunWorkerAsync();
        }
        void bgWl_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
           cavLoading.Visibility = System.Windows.Visibility.Collapsed;
            gNewBox.Visibility = System.Windows.Visibility.Visible;
            this.Dispatcher.Invoke(new Action(() =>
            {
                this.lab_pro.Content = "complete";
            }));
        }
        void bgWl_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                this.lab_pro.Content = e.ProgressPercentage;
            }));
        }

        void bgWl_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
             gNewBox.Visibility = System.Windows.Visibility.Collapsed;
             cavLoading.Visibility = System.Windows.Visibility.Visible;
             ShowStruct();
            }));
            GetData();
        }
        public void GetData()
        {
            for (int i = 0; i < 11; i++)
            {
                bgWl.ReportProgress(i);
                System.Threading.Thread.Sleep(500);
            }
        }

Posted by RockinPurdy on Sun, 31 May 2020 08:54:43 -0700