在我的应用程序中,我实现了自定义状态栏控件。它有progressbar,statusTextBox等。其他模块可以使用MEF获取该类的实例,并使用方法和属性将其数据绑定在他的元素中。问题是只有在某些操作完成后,我的状态栏视图才会更新。 这是一个代码示例:
[ImportingConstructor]
public IconManagerModel(IStatusBar statusBar)
{
StatusBar = statusBar;
}
public void SomeMethod()
{
for(...)
{
//I tried to use Dispatcher but it didn't help. View updates after method has finished
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal,
delegate()
{
StatusBar.SetProgress(amountComleted,total)
}
);
// ...
}
}
感谢名单
答案 0 :(得分:1)
您想在另一个线程上运行SomeMethod()
,然后回调Dispatcher
以更新进度。实际上,如果自定义进度条连接到某个UI元素,那么实现应该处理回调到UI线程。
你可能想要一些类似的东西:
public IconManagerModel(IStatusBar statusBar)
{
StatusBar = statusBar;
var thread = new Thread(new ThreadStart(SomeMethod));
thread.Start();
}
SomeMethod()
现在将在不同的线程上运行,因此如果您更新UI线程上的进度,那么您应该看到所需的结果。
答案 1 :(得分:0)
要通过slade添加上述答案,如果您希望消息泵立即处理消息,我还建议您使用DispatcherPriority.Render。使用Invoke(同步)和BeginInvoke(异步)。前者将立即强制更新,但会阻止您的处理工作。后者将在消息泵空闲时更新,通常建议使用。
无论哪种方式,您的后台工作都需要在后台线程上,特别是如果它很长或者会在一段时间内阻止您的UI。