首页 > 解决方案 > 调用 SolidColorBrush

问题描述

我怎么能调用,所以我不会得到以下错误?

System.InvalidOperationException:

调用线程无法访问此对象,因为不同的线程拥有它。

 // Method 1
                    if (((SolidColorBrush)RRefresh.Fill).Color == CustomGreen.Color && Foldername == string.Empty)
                    {
                        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            Driver.Navigate().Refresh();
                        }));
                    }

// Method 2
        if (Driver != null && ((SolidColorBrush)RRefresh.Fill).Color == CustomGreen.Color)
        {
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                Driver.Navigate().Refresh();
            }));
        }

标签: c#

解决方案


您可以使用control.Dispatcher.CheckAccess()来检查当前线程是否拥有该控件。如果它确实拥有它。否则使用此方法:

this.Dispatcher.Invoke(() =>
{
    ...// your code here.
});

推荐阅读