回答
通常你的UI只会绑定属性在你的虚拟机:
<ProgressBar Value="{Binding CurrentProgress, Mode=OneWay}"
Visibility="{Binding ProgressVisibility}"/>
你的虚拟机将使用一个BackgroundWorker
做在后台线程的工作,并定期更新值为CurrentProgress
。事情是这样的:
public class MyViewModel : ViewModel
{
private readonly BackgroundWorker worker;
private readonly ICommand instigateWorkCommand;
private int currentProgress;
public MyViewModel()
{
this.instigateWorkCommand =
new DelegateCommand(o => this.worker.RunWorkerAsync(),
o => !this.worker.IsBusy);
this.worker = new BackgroundWorker();
this.worker.DoWork += this.DoWork;
this.worker.ProgressChanged += this.ProgressChanged;
}
// your UI binds to this command in order to kick off the work
public ICommand InstigateWorkCommand
{
get { return this.instigateWorkCommand; }
}
public int CurrentProgress
{
get { return this.currentProgress; }
private set
{
if (this.currentProgress != value)
{
this.currentProgress = value;
this.OnPropertyChanged(() => this.CurrentProgress);
}
}
}
private void DoWork(object sender, DoWorkEventArgs e)
{
// do time-consuming work here, calling ReportProgress as and when you can
}
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.CurrentProgress = e.ProgressPercentage;
}
}
使用ProgressBar
控制及其Value
属性绑定到视图模型的属性:
查看
<ProgressBar Minimum="0" Maximum="0" Value="{Binding CurrentProgress}" />
视图模型
private double _currentProgress;
public double CurrentProgress
{
get { return _currentProgress; }
private set
{
_currentProgress = value;
OnPropertyChanged("CurrentProgress");
}
}
如何将此命令与我的命令连接?也就是说,我有一个按钮,点击后,我想启动进度条,直到完成操作。 – 2014-07-28 15:02:55
@KalaJ,你只需要在你的动作中更新CurrentProgress属性(例如,如果它是一个循环,每次迭代更新值) – 2014-07-28 15:34:29
事情是我只是等待系统读取/检测CD,当我插入光盘放入我的电脑。有时,系统需要一段时间才能读取CD,而其他时间则不需要。我如何更新这样的值?我想也许使用一个不确定的进度条,但我仍然困惑如何将它连接到我的命令? – 2014-07-28 17:25:09
添加两个属性到您的VM:
bool IsProgressBarVisible
double ProgressValue
如果您在VM开始很长一段时间的操作,设置IsProgressBarVisible属性为true,并设置ProgressValue定期到当前的进度值。尝试计算一个介于0和100之间的值。这具有您不必提供最小值和最大值的优点。 异步操作完成后,将IsProgressBarVisible设置为false。
在XAML中,绑定到这两个属性。使用值转换器将布尔可见性转换为可见性。
<ProgressBar Value="{Binding ProgressValue}" Visibility="{Binding IsProgressBarVisible,Converter={StaticResource BooleanToVisibility_ValueConverter}}"/>
- 1. 如何使用TIdTCPClient实现进度条?
- 2. 进度条实现
- 3. 实现YUI进度条模式的任何UI控件?
- 4. 如何在使用MVVM模式时实现工具栏?
- 5. 如何使用MVVM/ICommand模式在WPF中正确实现BackgroundWorker
- 6. 如何实现jQuery的PHP进度条
- 7. 如何在Ruby中实现进度条?
- 8. 如何实现自定义进度条
- 9. 如何实现循环进度条?
- 10. 实现分段进度条
- 11. 如何使用IEnumerable实现进度?
- 12. 如何在列表片段中使用AsyncTask实现进度条?
- 13. 如何使用wxPython正确实现进度条
- 14. 如何使用米式进度条?
- 15. 使用实体框架实现MVVM模式 - 添加删除
- 16. 在xaml MVVM模式中绑定进度条
- 17. 如何使用MVVM方法实现DataGrid
- 18. 如何使用WCF实现MVVM?
- 19. 如何在MVVM中实现模型
- 20. 如何使用进度条显示使用异步加载MVVM模式的RSS源
- 21. 进度条设计模式?
- 22. 如何使用MVVM模式在Wpf应用程序上实现本地化?
- 23. 如何实现QProgressBar从“繁忙”模式到标准进度模式的转换?
- 24. 我为什么要使用工具包来实现MVVM模式
- 25. 实现引导进度条来通知用户课程进度
- 26. 如何用ng样式进度条
- 27. 在libGDX中实现cricle进度条
- 28. 实现一个进度条类
- 29. 实现MySQL命令进度条
- 30. nodeBB上的进度条实现
嗨肯特,我需要一个单独的模型和视图模型的进度条?或者我会添加一个进度条到每个现有的视图,我打算使用它? – jpgooner 2010-08-19 09:43:29
我会从每个视图使用一个视图开始,然后决定是否可以将视图抽象为可重用组件。 – 2010-08-19 09:44:36
是的好呼喊,谢谢你的回答everone – jpgooner 2010-08-19 09:46:38