网站开发制作的流程,中阔浩潮建设工程有限公司网站,交通门户网站建设,无锡seo网站推广费用可能不是最好的办法#xff0c;但是用起来效果也还是可以的。
原理#xff1a;通过IsEnabled属性来控制按钮状态。btnConfirm.IsEnabled / this.IsEndbled 这两种方式是等价的。
案例比较简单#xff0c;如果后期做开发的话代码量变大#xff0c;只在结尾添加 this.IsEn…可能不是最好的办法但是用起来效果也还是可以的。
原理通过IsEnabled属性来控制按钮状态。btnConfirm.IsEnabled / this.IsEndbled 这两种方式是等价的。
案例比较简单如果后期做开发的话代码量变大只在结尾添加 this.IsEndbled true 逻辑上是有缺陷的。如果txtName为空是不是又要给IsEndbled True 这样重新赋值。 如果是多个if条件呢这样的话会很繁琐。
所以我就用上了异常捕获
使用 try{ 要执行的代码 }catch(Exception ex){ 异常 }finally{ 最后执行的代码 }
finally
当一个异常抛出时它会改变程序的执行流程。因此不能保证一个语句结束后它后面的语句一定会执行在 C# 中这个问题可以用 finally 解决。
为了确保一个语句总是能执行不管是否抛出异常需要将该语句放到一个 finally 块中finally 要么紧接在 try 块之后要么紧接在 try 块之后的最后一个 catch 处理程序之后。只要程序进入与一个 finally 块关联的 try 块则 finally 块始终都会运行 -- 即使发生了一个异常。
Xaml代码
Window x:ClassWpfApplication1.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlTitleMainWindow Height350 Width525GridStackPanelTextBox NametxtName Height30 /ProgressBar NameprogressBar1 Maximum100 Height42 /Label Namelabel1/LabelButton NamebtnConfirm ClickButton_Click_1 Height30 Content添加 //StackPanel/Grid
/Window
C#代码
namespace WpfApplication1
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void Button_Click_1(object sender, RoutedEventArgs e){this.IsEnabled false;try{if (!string.IsNullOrEmpty(txtName.Text)){MessageBox.Show(用户名称不能为空);//this.IsEnabled true;return;}for (int i 0; i 100; i){progressBar1.Value i;System.Windows.Forms.Application.DoEvents();Thread.Sleep(10);label1.Content 数据正在加载中 i ;}}catch (Exception ex){MessageBox.Show(ex.Message,错误,MessageBoxButton.OK,MessageBoxImage.Error);}finally{this.IsEnabled true;}}}
}