从Application.Current.TryFindResource返回的DP对象无权访问调用线程

时间:2019-05-31 23:12:57

标签: wpf dispatcher resourcedictionary

我正在使用TryFindResource API来获取画笔对象。但是返回的对象无权访问调用线程-为什么?

我的代码:

public static Brush GetErrorBrush()
        {
            object resValue = Application.Current.TryFindResource("Colors_Error_SCBrush");

            if(resValue!=null && resValue is DependencyObject)
            {
                if(!(resValue as DependencyObject).CheckAccess())
                {
                    //If we dont do this, while assiging the value to Calling API, it fails with exception "Must create DependencySource on same Thread as the DependencyObject."
                    resValue = Brushes.Red;
                }
            }
            return resValue != null ? (Brush)resValue : Brushes.DarkRed;
        }

此外,我如何找到谁是该依赖对象的父对象-哪个线程拥有它?

引用thread

谢谢, 拉什米

0 个答案:

没有答案