WPF 等待对话框

1.创建ProgressBarFrom窗体。代码如下

<Window x:Class="CustomControl.ProgressBarFrom"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:customcontrol="clr-namespace:CustomControl" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800"
        Title="进度条" Height="300" Width="300" 
         AllowsTransparency="True"  WindowStartupLocation="CenterScreen" WindowStyle="None" Background="#7F696969"  WindowState="Maximized" Loaded="Window_Loaded" >
    <!--Background="#7F696969" WindowState="Maximized" -->
    <WrapPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Vertical">

        <Grid x:Name="LayoutRoot" Background="Transparent" 
              HorizontalAlignment="Center" VerticalAlignment="Center">

            <Grid.RenderTransform>

                <ScaleTransform x:Name="SpinnerScale" 
                                ScaleX="1.0" ScaleY="1.0" />

            </Grid.RenderTransform>

            <Canvas RenderTransformOrigin="0.5,0.5" 
                    HorizontalAlignment="Center" 
                    VerticalAlignment="Center" 
                    Width="120" Height="120" >

                <Ellipse Width="21.835" Height="21.862" 
                         Canvas.Left="20.1696" 
                         Canvas.Top="9.76358" 
                         Stretch="Fill" Fill="Orange" 
                         Opacity="1.0"/>

                <Ellipse Width="21.835" Height="21.862" 
                         Canvas.Left="2.86816" 
                         Canvas.Top="29.9581" Stretch="Fill" 
                         Fill="Black" Opacity="0.9"/>

                <Ellipse Width="21.835" Height="21.862" 
                         Canvas.Left="5.03758e-006" 
                         Canvas.Top="57.9341" Stretch="Fill" 
                         Fill="Black" Opacity="0.8"/>

                <Ellipse Width="21.835" Height="21.862" 
                         Canvas.Left="12.1203" 
                         Canvas.Top="83.3163" Stretch="Fill" 
                         Fill="Black" Opacity="0.7"/>

                <Ellipse Width="21.835" Height="21.862" 
                         Canvas.Left="36.5459" 
                         Canvas.Top="98.138" Stretch="Fill" 
                         Fill="Black" Opacity="0.6"/>

                <Ellipse Width="21.835" Height="21.862" 
                         Canvas.Left="64.6723" 
                         Canvas.Top="96.8411" Stretch="Fill" 
                         Fill="Black" Opacity="0.5"/>

                <Ellipse Width="21.835" Height="21.862" 
                         Canvas.Left="87.6176" 
                         Canvas.Top="81.2783" Stretch="Fill" 
                         Fill="Black" Opacity="0.4"/>

                <Ellipse Width="21.835" Height="21.862" 
                         Canvas.Left="98.165" 
                         Canvas.Top="54.414" Stretch="Fill" 
                         Fill="Black" Opacity="0.3"/>

                <Ellipse Width="21.835" Height="21.862" 
                         Canvas.Left="92.9838" 
                         Canvas.Top="26.9938" Stretch="Fill" 
                         Fill="Black" Opacity="0.2"/>

                <Ellipse Width="21.835" Height="21.862" 
                         Canvas.Left="47.2783" 
                         Canvas.Top="0.5" Stretch="Fill" 
                         Fill="Black" Opacity="0.1"/>

                <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:01" 
                                     RepeatBehavior="Forever" />

                            </Storyboard>

                        </BeginStoryboard>

                    </EventTrigger>

                </Canvas.Triggers>

            </Canvas>

        </Grid>
        <Label FontSize="19" Margin="0,20,0,0" Name="LbTitle" Foreground="#006265" Width="300" HorizontalContentAlignment="Center" >正在加载</Label>
    </WrapPanel>
</Window>
using CustomControl;
using DictionaryData;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CustomControl
{
    /// <summary>
    /// ProgressBarFrom.xaml 的交互逻辑
    /// </summary>
    public partial class ProgressBarFrom : Window
    {
        DispatcherTimer Timer;
        public ProgressBarFrom()
        {
            InitializeComponent();
            #region 多线程
            Timer = new DispatcherTimer();
            Timer.Interval = TimeSpan.FromSeconds(0.3d);
            Task task = new Task(() =>
            {
                DoSometing();
            });
            task.Start();
            string sss = "正在加载...";
            int let = 0;
            Timer.Tick += (object sender2, EventArgs eventArgs) =>
            {
                if (let >= sss.Length)
                {
                    let = 0;
                }
                let++;

                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    LbTitle.Content = sss.Substring(0, let);
                }));

            };
            Timer.Start();
            #endregion
        }
        public ProgressBarFrom(string Title)
        {
            InitializeComponent();
            #region 多线程
            Task task = new Task(() =>
            {
                DoSometing();
            });
            task.Start();
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                LbTitle.Content = Title;
            }));
            #endregion
        }
        void DoSometing()
        {
            try
            {
                Timeline.DesiredFrameRateProperty.OverrideMetadata(
                    typeof(Timeline),
                        new FrameworkPropertyMetadata { DefaultValue = 50 });
            }
            catch (Exception ex)
            {

            }
        }
        public void SetTitle(string Title)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                LbTitle.Content = Title;
            }));
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
        }
    }
}

2.调用

 ProgressBarFrom progressBarFrom = new ProgressBarFrom();
 progressBarFrom.Show();
            Thread thread2 = new Thread(() =>
            {

                LoadData();//加载数据,比如查询数据库什么的
                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    UIBinding();//绑定数据
                    progressBarFrom.Close();
                }));
            }
           );
           thread2.Start();

3.UI界面

WPF  等待对话框

 

上一篇:wpf之数据模板


下一篇:WPF样式与模板