一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

解决WPF无法使用 dependencyobject,它属于其父 freezable 之外的其他线程

时间:2022-06-25 08:39:33 编辑:袖梨 来源:一聚教程网

system.invalidoperationexception
无法使用 dependencyobject,它属于其父 freezable 之外的其他线程。

代码如下:

 代码如下 复制代码

private static void PrintBankamsg(SolidColorBrush burshcolor, List tipcontrollist, HaocaiPositonSatus posi,int holenumber)
        {
            Action, SolidColorBrush, HaocaiPositonSatus, int> updateAction =
                new Action, SolidColorBrush, HaocaiPositonSatus,int>(Updatebanka);
            System.Windows.Application.Current.Dispatcher.BeginInvoke(updateAction, tipcontrollist, burshcolor, posi, holenumber);
        }
        private static void Updatebanka(List BanKaModelllist, SolidColorBrush burshcolor, HaocaiPositonSatus posi, int holenumber)
        {
            foreach (BankaList bm in BanKaModelllist.OrderBy(a => a._TipsConfig.StructureID))
            {
                BankaCtrl oneposition = bm.StackPanellist.FirstOrDefault(a => a.ToolTip.ToString() == posi.X + "," + posi.Y);
                if (oneposition != null)
                {
                    oneposition.Background = burshcolor;
                    oneposition.OperStatus = posi.ID.ToString();
                    oneposition.IsEnabled = false;
                    if (holenumber > 0)
                    {
                        BankaBtn btn= oneposition.FindName("hole"+holenumber) as BankaBtn;
                        btn.FillColor = burshcolor;
                    }
                    break;
                }
            }
        }


这一个线程,异步更新wpf ui的代码。一直没有问题,用方法更新数据也没有问题,但是写成依赖属性后,出现错误了。

 代码如下 复制代码

public static void ResetBanka( List BanKaModelllist)
        {
            foreach (BanKaModel bm in BanKaModelList.OrderBy(a => a.StructureID))
            {
                foreach (HaocaiPositonSatus positon in bm.AllPointList)
                {
                    if (positon != null)
                    {
                        positon.Status = 0;
                        PrintBankamsg(GetStatusColor(0), BanKaModelllist, positon,0);
                    }
                }
            }
          
        }

查了半天,发现SolidColorBrush 不能在别的线程生成,然后发给依赖属性。有可能依赖属性是在另一个线程控制的。

改成int传值就行了。

 代码如下 复制代码

 private static void PrintBankamsg(int burshcolor, List tipcontrollist, HaocaiPositonSatus posi,int holenumber)
        {
            Action, int, HaocaiPositonSatus, int> updateAction =
                new Action, int, HaocaiPositonSatus,int>(Updatebanka);
            System.Windows.Application.Current.Dispatcher.BeginInvoke(updateAction, tipcontrollist, burshcolor, posi, holenumber);
        }
        private static void Updatebanka(List BanKaModelllist, int burshcolor, HaocaiPositonSatus posi, int holenumber)
        {
            foreach (BankaList bm in BanKaModelllist.OrderBy(a => a._TipsConfig.StructureID))
            {
                BankaCtrl oneposition = bm.StackPanellist.FirstOrDefault(a => a.ToolTip.ToString() == posi.X + "," + posi.Y);
                if (oneposition != null)
                {
                    oneposition.Background = GetStatusColor(burshcolor);
                    oneposition.OperStatus = posi.ID.ToString();
                    oneposition.IsEnabled = false;
                    if (holenumber > 0)
                    {
                        BankaBtn btn= oneposition.FindName("hole"+holenumber) as BankaBtn;
                        btn.FillColor = GetStatusColor(burshcolor);
                    }
                    break;
                }
            }
        }

热门栏目