InvalidOperationException:在ImageOffsetProperty上暂停调度程序处理 [英] InvalidOperationException: dispatcher processing is suspended on ImageOffsetProperty

查看:101
本文介绍了InvalidOperationException:在ImageOffsetProperty上暂停调度程序处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#和WPF,并试图在屏幕上滑动图像条。老实说,我从Internet上的一些代码示例中获取了代码,并从中获取了所需的部分。

I am using C# and WPF and am trying to slide an image strip along the screen. I am being honest, I got the code from some code sample on the Internet and took the parts I needed from it.

public double ImageOffset
{
    get
    {
        try
        {
            return (double)this.Dispatcher.Invoke(
               System.Windows.Threading.DispatcherPriority.Background,
               (DispatcherOperationCallback)delegate 
               { return GetValue(ImageOffsetProperty); }, ImageOffsetProperty);
        }
        catch
        {
            return (double)this.GetValue(ImageOffsetProperty);
        }
    }

    set
    {
        this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
            (SendOrPostCallback)delegate 
            { SetValue(ImageOffsetProperty, (object)value); },
            (object)value);
    }
}

有时程序尝试获取ImageOffsetProperty和 InvalidOperationException:调度程序处理暂停时无法执行此操作发生,而且我不知道如何解决。

Sometimes when the program tries to get the ImageOffsetProperty an InvalidOperationException: Cannot perform this operation while dispatcher processing is suspended happens and I have no idea how to fix it.

I还尝试了 Dispatcher.BeginInvoke ,将ImageOffsetProperty安全地保存在Double中,然后将其返回,但始终返回0。

I also tried Dispatcher.BeginInvoke, safe the ImageOffsetProperty in a Double and then return it, but it always returned 0.

是什么原因导致 InvalidOperationException

推荐答案

如何删除所有 Background 之类的东西,然后像这样编写一个标准的依赖项属性包装器:

How about dropping all the Background stuff and just write a standard dependency property wrapper like this:

public double ImageOffset
{
    get { return (double)GetValue(ImageOffsetProperty); }
    set { SetValue(ImageOffsetProperty, value); }
}

从后台线程设置属性后,将赋值放入 Dispatcher.Invoke Dispatcher.BeginInvoke 呼叫。

When the property is set from a background thread, put the assignment in a Dispatcher.Invoke or Dispatcher.BeginInvoke call.

替换

obj.ImageOffset = someOffset;

by

Dispatcher.Invoke(() => obj.ImageOffset = someOffset);

Application.Current.Dispatcher.Invoke(() => obj.ImageOffset = someOffset);

如果您无法直接访问 Dispatcher 属性。

if you don't have direct access to the Dispatcher property of some UI element.

这篇关于InvalidOperationException:在ImageOffsetProperty上暂停调度程序处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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