从wpf窗口中的WPF进度条调用并从库中的对象更新 [英] WPF progress bar in a wpf window called and updated from an object in a library

查看:44
本文介绍了从wpf窗口中的WPF进度条调用并从库中的对象更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了我能找到的各种解决方案,但没有任何效果。

I have gone through every variation of solutions that I can find and nothing works.

我有一个包含一个类的库,我想从中启动一个wpf窗口包含进度条。 

I have a library that contains a class from which I want to launch a wpf window that contains a progress bar. 

我创建了一个测试人员wpf窗口来实例化该类并调用创建窗口的方法,更长时间工作,并更新进度条,因为它通过工作进展。

I created a tester wpf window to instantiate the class and call the method that creates the window, does the longer work, and updates the progress bar as it progresses through the work.

我尝试过的线程,后台工作人员,调用电话等的每个变体仍然不会更新进度条。 

Every variation I have tried of threading, background worker, invoke calls etc still does not update the progress bar. 

这应该是一件非常简单的事情......

This should be a very simple thing to do......

推荐答案

你有几件事情这将使复杂化。

You have a couple of things are going to complicate this.

控件具有线程关联性 - 这意味着你需要dispatcher.invokeasync从其他线程获取到ui线程。

Controls have thread affinity - which means you need to dispatcher.invokeasync to get from some other thread to the ui thread.

然后你将代码放在一个地方,进度窗口将进度条作为私有e成员。

Then you have your code in one place and the progress window has that progress bar as a private member.

我希望将这两件事分开。

I would want to keep the two things decoupled.

我会使用mvvmlight messenger工作进程告诉进度窗口做什么。

I'd use mvvmlight messenger for the worker process to tell the progress window to do stuff.

https://social.technet.microsoft.com/wiki/contents/articles/26070.communicating-between -classes.aspx?Redirected = true

https://social.technet.microsoft.com/wiki/contents/articles/26070.communicating-between-classes.aspx?Redirected=true

消息类型可能最好在进度窗口库中定义,除非您有其他模型库可以放入。这种方式两个项目都知道这个类是什么。

The message type is probably best defined in your progress window library unless you have some other model library you could put it in. That way both projects know what the class is.

调度员的东西:

Application.Current.Dispatcher.InvokeAsync(new Action(() =>
{
     var msg = new ProgressMessage { Value = nnn };
     Messenger.Default.Send<ProgressMessage >(msg);
}));


您还需要在进度窗口中订阅该消息类型,但是您可以看到我链接的那篇文章中的示例。

You also need to subscribe to that message type in your progress window but you can see examples in that article I linked.

假设您的进度条的最小值为0,最大值为100,那么它的值将为0到100。

Assuming your progress bar has minimum 0 and maximum 100 it will be expecting a number 0 to 100 for it's Value.


这篇关于从wpf窗口中的WPF进度条调用并从库中的对象更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆