网站系统建设需要什么资质,做模特网站,wordpress 自己的数据库,wordpress侧边栏文件【 声明#xff1a;版权所有#xff0c;欢迎转载#xff0c;请勿用于商业用途。 联系信箱#xff1a;feixiaoxing 163.com】 这两天在编写代码的时候#xff0c;正好遇到一个棘手的问题#xff0c;解决之后感觉挺有意义的#xff0c;所以先用blog记录一下#xff0c;后…【 声明版权所有欢迎转载请勿用于商业用途。 联系信箱feixiaoxing 163.com】 这两天在编写代码的时候正好遇到一个棘手的问题解决之后感觉挺有意义的所以先用blog记录一下后面可以当成经验来参考。问题是这样的本身软件在加载子窗口的时间可能会比较长这么长的时间的加载会给用户造成一个错觉会以为这个软件是不是真的卡死了所以在子窗口加载的过程中我们希望子窗口根据不同的加载进度更新父窗口中的进度条至少让用户觉得软件还在跑没有卡死。整个需求就是这么一个想法。 为了解决这个问题想了很多办法最后还是通过BackgroundWorker和参数传递的方式才解决的。 1、首先设计主窗口界面 主窗口界面不复杂主要就两个控件。一个是按钮用户弹出子窗口一个是进度条子窗口中的按钮按下去的时候这个进度条就会慢慢更新到100%。
Window x:ClassWpfApp.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfAppmc:IgnorabledTitleMainWindow Height450 Width800GridButton x:NameButton1 ContentButton1 ClickButton1_Click HorizontalAlignmentLeft Margin335,135,0,0 VerticalAlignmentTop Width75/ProgressBar NameprogressBar HorizontalAlignmentLeft VerticalAlignmentTop Width300 Height20 Margin230,205,0,0//Grid
/Window整个显示效果是这样的 2、主窗口的代码 有了界面下面开始设计主窗口的代码。整个代码其实有两块。一块是按钮Button1的回调函数这部分就是弹出子窗口还要给子窗口传递一个参数。另外一块就是注册BackgroundWorker变量以及它的回调函数。注意这个BackgroundWorker的变量是在构造函数里面进行设置的。而且刚刚谈到的子窗口传递参数说的也就是这个BackgroundWorker变量。 有兴趣的同学可以观察一下BackgroundWorker的回调函数ProgressChanged。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;namespace WpfApp
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{private BackgroundWorker worker;public MainWindow(){InitializeComponent();worker new BackgroundWorker();worker.WorkerReportsProgress true;worker.ProgressChanged Worker_ProgressChanged;}private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e){progressBar.Value e.ProgressPercentage;}private void Button1_Click(object sender, RoutedEventArgs e){ChildWindow childWindow new ChildWindow(worker);childWindow.ShowDialog();}}
}3、设计子窗口界面 子窗口界面部分就比较简单了就是一个按钮而已
Window x:ClassWpfApp.ChildWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfAppmc:IgnorabledTitleChildWindow Height450 Width800GridButton x:NameButton2 ContentButton2 ClickButton2_Click HorizontalAlignmentLeft Margin355,150,0,0 VerticalAlignmentTop Width75//Grid
/Window转成图形的话它的界面是这样的 4、子窗口代码设计 有了子窗口下面就要开始实现按钮的回调函数了。既然在主窗口中已经定义好了BackgroundWorker那么回调函数需要做的就是有了进展之后去ReportProgress就好了。这样有了这个ReportProgress就会进一步触发主窗口中的回调函数这样主窗口中的进度条也会得到更新。并且所有操作都完毕之后还会弹出一个MessageBox。大概就是这么一个过程。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.ComponentModel;namespace WpfApp
{/// summary/// ChildWindow.xaml 的交互逻辑/// /summarypublic partial class ChildWindow : Window{public int add_flag 0;private BackgroundWorker worker;public ChildWindow(BackgroundWorker worker){InitializeComponent();this.worker worker;}private void Button2_Click(object sender, RoutedEventArgs e){if (add_flag 0){add_flag 1;worker.DoWork (s, args) {for (int i 1; i 100; i){worker.ReportProgress(i);Thread.Sleep(50);}};worker.RunWorkerCompleted (s, args) {MessageBox.Show(Task finished);};}worker.RunWorkerAsync();}}
} 细心的同学应该还发现了代码中还多了一个add_flag这主要是为了防止在子窗口中重复按钮之后反复进行进度条的更新。 5、编译和测试 编译无误之后就可以开始测试。测试的方式和之前说的一样首先打开主窗口利用按钮Button1再打开子窗口进一步单击子窗口中的按钮Button2如果发现主窗口的中的进度条可以正常更新那说明一切流程都是ok的否则就要去check一下问题同时debug一下软件失败的原因。