如何更改C#中进度条的值?

发布于 2024-08-26 00:05:08 字数 262 浏览 5 评论 0原文

我想在我的基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待"谢繁草 2024-09-02 00:05:08

如果您有一个类是您的“工作类”(您通过该类调用单个函数),那么您可以在该类上创建一个 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 the INotifyPropertyChanged interface.

Then in your UI: either use databinding, or specifically listen for that event and update the progress bar accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文