如何更改C#中进度条的值?
我想在我的基于 C# 的应用程序中添加一个进度条。我想知道在特定事件中更新其值的好方法是什么? 我的问题是,我在 UI 中有一个函数可以完成所有处理。该函数依次调用更多函数、创建对象、更新数据库等(基本上执行所有工作)并在输出文件中返回值。我只能访问该功能。那么在每个步骤结束时如何更新进度条的值呢?
对于这种情况,最佳做法是什么?将 UI 逻辑和业务逻辑分开的好方法是什么?我想避免在我的库代码中编写与进度条相关的代码。
请提出一些解决方案。 谢谢和问候,
I want to add an progress bar into my C# based application. I would like to know what is the good way of updating its value at particular events?
My problem is, I have single function in UI which does all the processing.This function in turn calls many more functions, creates objects, updates database, etc. (basically performs all the work) and returns values in an output file. I have access to that function only. So in this how to update the progressbar's value at the end of each step?
What is the best practice for this kind of situation? What is the good way to keep UI logic and business logic seperate? I want to avoid writting progressbar related code in my library code.
Please suggest some solution.
Thanks and Regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有一个类是您的“工作类”(您通过该类调用单个函数),那么您可以在该类上创建一个
Progress
属性。然后,随着操作的进行,更新该属性的值并引发一个事件以指示它已更改。您可以定义特定的
ProgressChanged
事件,或者,如果您使用数据绑定,则使用INotifyPropertyChanged
接口。然后在您的 UI 中:使用数据绑定,或者专门侦听该事件并相应地更新进度栏。
If you have a class that's your "worker class" - the class that you call your single function through - then you can create a
Progress
property on that class. Then, as the operation proceeds, update the value of that property and throw an event to indicate that it's changed.You could either define a specific
ProgressChanged
event or, if you're using data binding, use theINotifyPropertyChanged
interface.Then in your UI: either use databinding, or specifically listen for that event and update the progress bar accordingly.