C#WPF没有及时更新图形 [英] C# WPF not promptly updating graphics
问题描述
我有一个库,可以将事件激发到主窗口。
库触发一些事件并告诉它何时完成了IsBusy属性。我希望通过这些活动更新图形,特别是:
I have a library which fires events to the main window.
The library fires some events and tells when it has finished through the IsBusy property. I want the graphics to be updated by those event and particularly:
private bool _isBusy;
public bool IsBusy
{
get { return _isBusy; }
set
{
_isBusy = value;
if (value)
imgBusy.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send,
new Action(() => imgBusy.Source = new BitmapImage( new Uri(@"../../Resources/redBall.png", UriKind.Relative))));
else
imgBusy.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send,
new Action(() => imgBusy.Source = new BitmapImage(new Uri(@"../../Resources/greenBall.png", UriKind.Relative))));
}
}
我所期待的是:
idle - >绿色
开始--->红色
终止--->绿色
,其行为是:
1.通过后台工作人员启动时:
idle --->绿色
开始--->绿色
midway --->红色
终止--->绿色。
2.在没有背景工作者的情况下启动:
空闲,开始,中途,终止--->绿色
但是如果我设置一个断点,我会看到代码被击中那里球的颜色发生了变化! !
我是Winform的WPF新手,有一个Mainform.Update。这里有类似的东西吗?
Thanx任何帮助Patrick
so what I expect is:
idle --> green
started ---> red
terminated ---> green
while its behaviour is:
1. when launched through backgroundworker:
idle ---> green
started --->green
midway ---> red
terminated --->green.
2. when launched without backgroundworker:
idle,started,midway,terminated --->green
But if I put a breakpoint I see that the code is hit there where the colour of the ball is changed!!
I am new with WPF in winforms there was a Mainform.Update. Is there something similar here?
Thanx for any help Patrick
推荐答案
以下链接回答了关于为什么繁忙指标在没有后台工作人员时使用时无法按预期工作的问题,并且还提供了有关如何使用繁忙指标的明确说明。
http:// elegantcode。 com / 2011/10/07 / extended-wpf-toolkitusing-the-busyindicator / [ ^ ]
您也可以通过Tasks而不是后台工作人员实现相同的目标。下面是一个示例:
Hi,
The following link answers your question on why busy indicator does not work as expected when used without background worker and also provides a clear explanation on how to work with a busy indicator.
http://elegantcode.com/2011/10/07/extended-wpf-toolkitusing-the-busyindicator/[^]
You can also achieve the same with Tasks instead of background worker. Here is a sample:
Task taskCompare = Task.Factory.StartNew(() =>
{
//your long running logic or method invocation
}).ContinueWith(compareResult =>
{
//do something with your result
_busyIndicator.IsBusy = false;
});
BusyIndicatorContent = "your message here";
_busyIndicator.IsBusy = true;
这篇关于C#WPF没有及时更新图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!