本文介绍了WPF调度调用返回的值总是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须返回一个的UIElement ,我称之为使用调度,下面是一个方法的调用代码。



不过的调度引用始终是NULL的返回值,任何想法?

 无效backgroundWorker_DoWork(对象发件人,DoWorkEventArgs E)
{
VAR幻灯片=(IList的<的UIElement>)e.Argument;
变种bmpSlides =新的List<&的UIElement GT;();
变种imageService =新ImageService();
诠释计数= 0;

的foreach(在幻灯片的UIElement幻灯片)
{
对象retVal的= slide.Dispatcher.Invoke(
新的ThreadStart(()=> imageService.GenerateProxyImage(幻灯片)));
bmpSlides.Add(imageService.GenerateProxyImage(幻灯片));
_backgroundWorker.ReportProgress(数/ 100 * slides.Count);
计数++;
}

e.Result = bmpSlides;
}


解决方案

D'哦,这里是如何做你正在尝试做的:

 对象retVal的; 
slide.Dispatcher.Invoke(新动作(()=> RETVAL = imageService.GenerateProxyImage(幻灯片)));



编辑:总的ThreadStart把我关 - 这不是多线程的。什么是你想用这个代码示例完成??


I have a call to a method that returns a UIElement that I call using the Dispatcher, below is the code.

However the return value of the Dispatcher invoke is always NULL, any ideas?

void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    var slides = (IList<UIElement>)e.Argument;
    var bmpSlides = new List<UIElement>();
    var imageService = new ImageService();
    int count = 0;

    foreach (UIElement slide in slides)
    {
        object retVal = slide.Dispatcher.Invoke(
            new ThreadStart(() => imageService.GenerateProxyImage(slide)));
        bmpSlides.Add(imageService.GenerateProxyImage(slide));
        _backgroundWorker.ReportProgress(count / 100 * slides.Count);
        count++;
    }

    e.Result = bmpSlides;
}
解决方案

D'oh, here's how to do what you are trying to do:

object retVal;
slide.Dispatcher.Invoke(new Action(() => retval = imageService.GenerateProxyImage(slide)));

Edit: The ThreadStart threw me off - this isn't multithreaded. What are you trying to accomplish with this code sample??

这篇关于WPF调度调用返回的值总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 10:26