WPF ProgressBar显示进度(五)
2014-06-26 14:53
323 查看
在这里,我们将控制进度条的进度显示,功能很简单,通过两个按钮“Start”和“Stop”分别控制进度条的加载与暂停,当Start可用时,Stop不可用;反之,当Start不可用时,Stop可用。
我们知道,当UI绑定Command时会订阅CanExecuteChanged事件,而该事件会一直监测UI的状态并实时调用CanExecute方法返回其状态,同样的,我们在之前MVVM的基础上来实现这一功能。
1、在Model里添加Progress类
2、在Command里添加两个类,RelayCommandGo和RelayCommandStop
3、在ViewModel里添加ProgressViewModel类
4、在View里添加xaml
程序运行如下图所示:
(1)、程序启动时
(2)、点击Start
(3)、点击Stop
我们知道,当UI绑定Command时会订阅CanExecuteChanged事件,而该事件会一直监测UI的状态并实时调用CanExecute方法返回其状态,同样的,我们在之前MVVM的基础上来实现这一功能。
1、在Model里添加Progress类
class Progress { public double Minimum { get; set; } public double Maximum { get; set; } public double Value { get; set; } public string Text { get; set; } }
2、在Command里添加两个类,RelayCommandGo和RelayCommandStop
class RelayCommandGo : ICommand { private Action _execute; private Func<bool> _canExecute; public RelayCommandGo(Action execute, Func<bool> canExecute) { this._execute = execute; this._canExecute = canExecute; } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public bool CanExecute(object parameter) { return this._canExecute(); } public void Execute(object parameter) { this._execute(); } } class RelayCommandStop : ICommand { private Action _execute; private Func<bool> _canExecute; public RelayCommandStop(Action execute, Func<bool> canExecute) { this._execute = execute; this._canExecute = canExecute; } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public bool CanExecute(object parameter) { return this._canExecute(); } public void Execute(object parameter) { this._execute(); } }
3、在ViewModel里添加ProgressViewModel类
namespace MVVM3 { class ProgressViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public Progress Progress { get; set; } /// <summary> /// 最小值 /// </summary> public double Minimum { get { return this.Progress.Minimum; } set { this.Progress.Minimum = value; RaisePropertyChanged("Minimum"); } } /// <summary> /// 最大值 /// </summary> public double Maximum { get { return this.Progress.Maximum; } set { this.Progress.Maximum = value; RaisePropertyChanged("Maximum"); } } /// <summary> /// 当前数值 /// </summary> public double Value { get { return this.Progress.Value; } set { this.Progress.Value = value; RaisePropertyChanged("Value"); } } /// <summary> /// 当前数值 /// </summary> public string Text { get { return this.Progress.Text; } set { this.Progress.Text = value; RaisePropertyChanged("Text"); } } /// <summary> /// 是否可用的状态 /// </summary> public bool IsEnabledStart { get; set; } /// <summary> /// 是否可用的状态 /// </summary> public bool IsEnabledStop { get; set; } private void RaisePropertyChanged(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public RelayCommandGo CommandGo { get; set; } public RelayCommandStop CommandStop { get; set; } /// <summary> /// 线程同步事件 /// </summary> private ManualResetEvent _manualReset; /// <summary> /// 线程 /// </summary> private Thread _thread; public ProgressViewModel() { this.Progress = new Progress() { Maximum = 100 }; this._manualReset = new ManualResetEvent(false); this.IsEnabledStart = true; this.IsEnabledStop = false; this.CommandGo = new RelayCommandGo(StartProgress, EnabledStart); this.CommandStop = new RelayCommandStop(StopProgress, EnabledStop); } /// <summary> /// 开启线程 /// </summary> private void StartProgress() { this._manualReset.Set(); if (_thread == null) { _thread = new Thread(new ThreadStart(() => { for (int i = 1; i <= 100; i++) { this._manualReset.WaitOne(); this.Value = i; this.Text = string.Format("{0}%", i); Thread.Sleep(200); } })); _thread.Start(); } this.IsEnabledStop = this.IsEnabledStart; this.IsEnabledStart = !this.IsEnabledStart; } /// <summary> /// 停止线程的执行 /// </summary> private void StopProgress() { this._manualReset.Reset(); this.IsEnabledStart = this.IsEnabledStop; this.IsEnabledStop = !this.IsEnabledStop; } /// <summary> /// 确定此命令是否可以在当前状态下执行 /// </summary> /// <returns></returns> private bool EnabledStart() { return this.IsEnabledStart; } /// <summary> /// 确定此命令是否可以在当前状态下执行 /// </summary> /// <returns></returns> private bool EnabledStop() { return this.IsEnabledStop; } } }
4、在View里添加xaml
<Window x:Class="MVVM3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MVVM3" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <local:ProgressViewModel x:Key="progressViewModel"></local:ProgressViewModel> </Window.Resources> <Window.DataContext> <Binding Source="{StaticResource ResourceKey=progressViewModel}"></Binding> </Window.DataContext> <StackPanel> <TextBlock x:Name="tb" Height="23" Text="{Binding Text}"></TextBlock> <ProgressBar x:Name="pb" Height="20" Minimum="{Binding Minimum}" Maximum="{Binding Maximum}" Value="{Binding Value}"></ProgressBar> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <Button Content="Start" Width="72" Height="23" Margin="5" Command="{Binding CommandGo}"></Button> <Button Content="Stop" Width="72" Height="23" Margin="5" Command="{Binding CommandStop}"></Button> </StackPanel> </StackPanel> </Window>
程序运行如下图所示:
(1)、程序启动时
(2)、点击Start
(3)、点击Stop
相关文章推荐
- WPF ProgressBar显示进度(一)
- WPF ProgressBar显示进度(二)
- WPF ProgressBar显示进度(三)
- WPF 文件拷贝进度显示
- ProgressBar进度条10秒显示完成进度工作--binbinyang
- android中实现PopupWindow跟随ProgressBar进度显示
- 页面上现有ProgressBar控件progressBar,请用书写线程以10秒的的时间完成其进度显示工作
- wpf 窗体Load事件加载数据导致白屏 先展示加载进度BackgroundWorker(直至加载完成)显示
- 使用ASP.NET Atlas编写显示真实进度的ProgressBar(进度条)控件
- 页面上现有ProgressBar控件progressBar,请用书写线程以10秒的的时间完成其进度显示工作。
- 设置 ProgressBar 进度条(条形) 的颜色和用代码动态控制显示的长度
- WPFProgressBarAndSlider随位置显示Value
- ProgressBar---进度圈的显示
- 利用Ajax生成ProgressBar:实现同类大批量数据批量生成Html,并实时显示进度条,可查看处理日志
- WPF—ProgressBar进度条
- progressbar,书写线程以10秒的时间完成其进度显示工作
- wpf ProgressBar实时进度的测试
- C# WinForm ProgressBar垂直显示进度和从右向左显示进度
- c# progressBar进度条改变颜色及显示进度数字
- android progressbar 显示两个进度样式的解决方案