WPF中处理大批量数据防止界面假死

首先,你需要一个提示窗体,当后台处理数据时执行窗体程序,上代码。

这个是WPF界面的前台,里面有一个动画,和一句提示。

<Grid>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="100"></ColumnDefinition>
    <ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
//动画
<Image x:Name="GifImg" Visibility="Hidden" gif:ImageBehavior.RepeatBehavior="Forever" Grid.Column="0" Margin="10" />
<pu:Loading  Width="60" Height="60" LoadingStyle="Classic"  Stroke="#1860d1" IsRunning="True" HorizontalAlignment="Center" />
//提示
<TextBlock Grid.Column="1" x:Name="txtMsg" Margin="0,0,0,0" TextAlignment="Left"  Foreground="#1860d1" VerticalAlignment="Center" HorizontalAlignment="Left" TextWrapping="Wrap" FontFamily="微软雅黑">正在执行,请稍后...</TextBlock>
如下图 ![请添加图片描述](https://img-blog.csdnimg.cn/33acee64249e4fb7b76d304ed6ac6ee9.jpg?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAcXFfNDAxMzExOTk=,size_20,color_FFFFFF,t_70,g_se,x_16)

后台代码

  public string Message
        {
            get { return txtMsg.Text; }
            set
            {
                this.Dispatcher.Invoke(() =>
                {
                    if (string.IsNullOrEmpty(value))
                    {
                        txtMsg.Text = "正在执行,请稍后..."; 
                    }
                    else
                        txtMsg.Text = value;
                });
            }
        }

   public Loading(Action<Loading> _callBack_loading)
        {
            InitializeComponent();
            this.Loaded += Loading_Loaded;
            var image = new BitmapImage();
            image.BeginInit();
            image.UriSource = new Uri($"pack://siteoforigin:,,,/Resources/{ImagePath}");// new Uri("Images/loading.gif", UriKind.Relative);
            image.EndInit();
            ImageBehavior.SetAnimatedSource(GifImg, image);
            this.CallBack_Loading = _callBack_loading;
        }

        private void Loading_Loaded(object sender, RoutedEventArgs e)
        {
            new Action(() =>
            {
                try
                {
                    this.CallBack?.Invoke();
                    this.CallBack_Loading?.Invoke(this);
                }
                catch (LogTool.ExceptionWithWriteLog err)
                {
                    ErrMessage = err.Message;
                }
                catch (Exception err)
                {
                    LogTool.LogHelper.WriteLog(err.ToString());
                    ErrMessage = err.Message;
                }

            }).BeginInvoke(this.OnComplate, null);
            //this.CallBack.BeginInvoke(this.OnComplate, null);
        }
        private void OnComplate(IAsyncResult ar)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                this.Close();
                if (!string.IsNullOrEmpty(ErrMessage))
                {
                    MsgBox.Show(ErrMessage, "提示", MessageBoxButton.OK, Panuon.UI.Silver.MessageBoxIcon.Info);
                }
            }));
        }

以上就是Loding界面的主要代码了,下面写如何调用

首先实例化Loding界面

 /// <summary>
        /// 执行指定方法,并显示等待框
        /// </summary>
        /// <param name="owner">调用的窗体</param>
        /// <param name="callback">执行的方法</param>
        /// <param name="message">自定义提示内容(默认为:正在执行,请稍后...)</param>
        public static void ExecActionWithLoading(System.Windows.FrameworkElement owner, Action callback, string message = "")
        {
            Loading win = new Loading(callback);
            win.Message = message;
            Window pwin = Window.GetWindow(owner);
            win.Owner = pwin;
            var loc = owner.PointToScreen(new Point());
            win.Left = loc.X + (owner.ActualWidth - win.Width) / 2;
            win.Top = loc.Y + (owner.ActualHeight - win.Height) / 2;
            win.ShowInTaskbar = false;
            if (string.IsNullOrEmpty(message))
            {
                win.Height = 0;
                win.Width = 0;
            }
            win.ShowDialog();
        }

        public static void ExecActionWithLoading(System.Windows.FrameworkElement owner, Action<Loading> callback, string message = "")
        {
            Loading win = new Loading(callback);
            win.Message = message;
            Window pwin = Window.GetWindow(owner);
            win.Owner = pwin;
            var loc = owner.PointToScreen(new Point());
            win.Left = loc.X + (owner.ActualWidth - win.Width) / 2;
            win.Top = loc.Y + (owner.ActualHeight - win.Height) / 2;
            win.ShowInTaskbar = false;
            if (string.IsNullOrEmpty(message))
            {
                win.Height = 0;
                win.Width = 0;
            }
            win.ShowDialog();
        }

然后就是使用了

ExecActionWithLoading(MainWindow, (_) =>{这个里面就写处理数据的代码},“”)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值