代码之家  ›  专栏  ›  技术社区  ›  Sarah Vessels

在多线程WPF应用程序中创建LinearGradientBrush时发生InvalidOperationException

  •  0
  • Sarah Vessels  · 技术社区  · 14 年前

    在一个 static var brush = new LinearGradientBrush(_snazzyGradient); ,这一行抛出异常。 _snazzyGradient 定义如下:

    private static readonly GradientStopCollection _snazzyGradient =
        new GradientStopCollection
        {
            new GradientStop((Color)ColorConverter.ConvertFromString("#DBF3FF"), 0.0),
            new GradientStop((Color)ColorConverter.ConvertFromString("#A3CCE0"), 1.0)
        };
    

    包含方法和 _时髦的 INotifyPropertyChanged ,如果它很重要,则用作视图模型。使用的静态方法 _时髦的 UserControl 类,我使用引用 _时髦的

    当我调试我的应用程序时,在 var brush=新的LinearGradientBrush(_snazzyGradient); 行,我得到以下异常:

    捕获到System.InvalidOperationException 消息=调用线程无法访问此对象,因为其他线程拥有它。 堆栈跟踪: 在System.Windows.Threading.Dispatcher.VerifyAccess() 在System.Windows.Freezable.ReadPreamble()上 位于System.Windows.Media.GradientStopCollection.OnInheritanceContextChangedCore(EventArgs args args) 位于System.Windows.Freezable.AddInheritanceContext(DependencyObject上下文,DependencyProperty属性) 在System.Windows.DependencyObject.providedeselfasinheritancecontext(DependencyObject doValue,DependencyProperty dp) 在System.Windows.DependencyObject.providedeselfasinheritancecontext(对象值,dependencProperty dp) 在System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex EntryIndex,DependencyProperty dp,PropertyMetadata,EffectiveValueEntry oldEntry,EffectiveValueEntry&newEntry,Boolean-converteWithDeferedReference,OperationType OperationType) 在System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp、对象值、PropertyMetadata元数据、Boolean强制WithDeferedReference、OperationType OperationType、Boolean isInternal) 在System.Windows.DependencyObject.SetValueInternal(dependencProperty dp,对象值) 位于System.Windows.Media.LinearGradientBrush..ctor(GradientStopCollection GradientStopCollection) 在加载的模板..ctor(ParentViewModel viewModel,模板模板) 内部异常:

    我已经更改了 用户控件

    public ParentViewModel Data
    {
        get
        {
            return (ParentViewModel)Dispatcher.Invoke(
                DispatcherPriority.Background,
                (DispatcherOperationCallback)delegate
                {
                    return GetValue(DataProperty);
                },
                DataProperty
            );
        }
        set
        {
            Dispatcher.BeginInvoke(
                DispatcherPriority.Background,
                (SendOrPostCallback)delegate
                {
                    SetValue(DataProperty, value);
                },
                value
            );
        }
    }
    

    我的问题是,我怎样才能摆脱这个 InvalidOperationException ? 把一堆 Dispatcher _时髦的 作为静态字段,但可能是从静态方法返回的?我不知道那是否有用。我当然想要多线程,因为我不想让GUI在读/写必要文件时陷入困境,诸如此类的事情。也许我的问题源于 GradientStop (继承自 DependencyObject ),等等,在视图模型中;也许应该从 用户控件

    1 回复  |  直到 14 年前
        1
  •  0
  •   Sarah Vessels    14 年前

    好吧,看来要搬家了 _snazzyGradient 从一个 static 静止的

    private static GradientStopCollection getSnazzyGradient()
    {
        return new GradientStopCollection
        {
            new GradientStop((Color)ColorConverter.ConvertFromString("#DBF3FF"), 0.0),
            new GradientStop((Color)ColorConverter.ConvertFromString("#A3CCE0"), 1.0)
        };
    }
    

    我猜这是因为 GradientStop 不知怎么的,因为它是 DependencyObject UserControl 类和依赖关系对象具有线程关联。现在正在做 var brush = new LinearGradientBrush(getSnazzyGradient());