WPF- 사용자 정의 컨트롤의 동적 추가 및 BackgroundWorker 사용

15341 단어 WPF
어제 인터넷에서 한 블로거가 Back ground Worker를 쓴 것을 보았는데 매우 좋았다.공부하고 수정한 후에 잊어버리지 않도록 기록하세요!창의 동일한 위치에 다음과 같은 코드로 회전 효과도를 추가합니다.
Grid.Row="2" Grid.Column="1" Name="cavLoading" Visibility="Collapsed" RenderTransformOrigin="0.5,0.5"  HorizontalAlignment="Center" VerticalAlignment="Center" Width="120" Height="120">
                "15" Height="15" Canvas.Left="55" Canvas.Top="31" Stretch="Fill" Fill="#FFD1D1F7" Opacity="1.0"/>
                "15" Height="15" Canvas.Left="38" Canvas.Top="39" Stretch="Fill" Fill="Blue" Opacity="0.8"/>
                "15" Height="15" Canvas.Left="36" Canvas.Top="58" Stretch="Fill" Fill="#FF0000FE" Opacity="0.7"/>
                "15" Height="15" Canvas.Left="52" Canvas.Top="67" Stretch="Fill" Fill="Blue" Opacity="0.6"/>
                "15" Height="15" Canvas.Left="68" Canvas.Top="61" Stretch="Fill" Fill="#FF2E2EFF" Opacity="0.5"/>
                "15" Height="15" Canvas.Left="69" Canvas.Top="42" Stretch="Fill" Fill="#FF6F6FFF" Opacity="0.4"/>
                
                    "SpinnerRotate" Angle="0" />
                
                
                    "ContentControl.Loaded">
                        
                            
                                "SpinnerRotate" Storyboard.TargetProperty="(RotateTransform.Angle)" From="0" To="360" Duration="0:0:0.8" RepeatBehavior="Forever" />
                            
                        
                    
                
            

그리고 다음과 같은 코드로 회전된 프리젠테이션 그림을 보여 줍니다.
<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>

사용자 정의 컨트롤 백그라운드에서 동적 추가를 수행하기 때문에 레이아웃에 직접 추가하지 않았습니다. 동적 추가 코드는 다음과 같습니다.
 public void ShowStruct()
 {
            var controls = new List();
            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;
                        //  Grid        
                        gNewBox.Children.Add(bitkyPole);
                        //      Grid     
                        Grid.SetRow(bitkyPole, 5 - i);
                        Grid.SetColumn(bitkyPole, j);
                        //         ,        
                        controls.Add(bitkyPole);
                        //               
                        // bitkyPole.setContent(id);                      
                        id++;
                   }
            }
 }

그리고 기다리는 시간이 긴 label:
 <Label Grid.Row="1" Grid.Column="1" Width="120" Height="40" VerticalAlignment="Top" HorizontalAlignment="Left" Content="    :" 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>

그런 다음 BackgroundWorker를 구현합니다. 코드는 다음과 같습니다.
 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 = "  ";
            }));
        }
        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);
            }
        }

좋은 웹페이지 즐겨찾기