场景:线程里面构建MVVM实体类,实体类包含 Brush 属性时,构建 SolidColorBrush 需要UI线程,否则会报 “必须在与 DependencyObject 相同的线程上创建 DependencySource”

MVVM实体类

 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Linq;
5 using System.Text;
6 using System.Threading.Tasks;
7 using System.Windows.Media;
8
9 namespace WpfApp3
10 {
11 public class TestInfo : INotifyPropertyChanged
12 {
13 public event PropertyChangedEventHandler PropertyChanged;
14
15 public Brush BG { get; set; }
16 }
17 }

实体类

下面是报错代码:

 1 <Window x:Class="WpfApp3.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6 xmlns:local="clr-namespace:WpfApp3"
7 mc:Ignorable="d"
8 Title="MainWindow" Height="450" Width="800">
9 <Grid Background="{Binding BG}">
10
11 </Grid>
12 </Window>

前端

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Data;
9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15
16 namespace WpfApp3
17 {
18 /// <summary>
19 /// MainWindow.xaml 的交互逻辑
20 /// </summary>
21 public partial class MainWindow : Window
22 {
23 public MainWindow()
24 {
25 InitializeComponent();
26
27 Loaded += MainWindow_Loaded;
28 }
29
30 private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
31 {
32 TestInfo info = new TestInfo();
33 await Task.Run(() =>
34 {
35 info.BG = new SolidColorBrush(Colors.Red);
36 });
37 this.DataContext = info;
38 }
39 }
40 }

后端

原因:

因为  info.BG = new SolidColorBrush(Colors.Red);  这行报错,原因是 构建 SolidColorBrush 需要UI 线程!!

调整后的后端代码:

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Data;
9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15
16 namespace WpfApp3
17 {
18 /// <summary>
19 /// MainWindow.xaml 的交互逻辑
20 /// </summary>
21 public partial class MainWindow : Window
22 {
23 public MainWindow()
24 {
25 InitializeComponent();
26
27 Loaded += MainWindow_Loaded;
28 }
29
30 private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
31 {
32 TestInfo info = new TestInfo();
33 await Task.Run(() =>
34 {
35 Brush brush = null;
36 Dispatcher.Invoke(() =>
37 {
38 brush = new SolidColorBrush(Colors.Red);
39 });
40 info.BG = brush;
41 });
42 this.DataContext = info;
43 }
44 }
45 }

后端

MVVM绑定 填坑,必须在与 DependencyObject 相同的线程上创建 DependencySource的更多相关文章

  1. Android填坑系列:在小米系列等机型上放开定位权限后的定位请求弹框

    背景: 近期因实际项目需要,在特定操作下触发定位请求,取到用户位置及附近位置. 问题: 经初步选型,最终决定接入百度定位,按照百度定位SDK Android文档,接入过程相对顺利.但随后发现,在小米系 ...

  2. 小程序textarea完美填坑

    相信做微信小程序的码友们都被textarea这个原生组件坑过,什么placeholder位置错乱,穿透弹窗或遮罩层,ios上输入法弹起后换行输入内容遮挡,删除输入内容时内容被遮挡等等... 反正综上所 ...

  3. [Xamarin]我的Xamarin填坑之旅(二)

    上一篇交代了我Xamarin填坑的背景,大概聊了聊第一步环境配置,第二步创建项目和开发框架选择.如果有一个可用的梯子,这部分基本不会出错. 接下来就具体聊一聊写代码的过程中遇到的一些事儿. 第三步是码 ...

  4. 填坑系列:通过ESXi来配置IPMI

    近日西安的天气很不错,可是看到从其他地方迁移来的主机在新环境下无法远程调试怪郁闷的,这就需要填坑,要不就会给后来者挖更大的坑. 今天遇到的坑是在IPMI的网络设置里面启用了VLAN标签之后,在新环境下 ...

  5. bootstrap-table填坑之旅<一>认识bootstrap-table

    应公司需求,改版公司ERP的数据显示样式.由于前期开发的样式是bootstrap,所以选bootstrap-table理所当然(也是因为看了bootstrap-table官网的example功能强大, ...

  6. Android Tips – 填坑手册

    出于: androidChina   http://www.androidchina.net/3595.html 学习 Android 至今,大大小小的坑没少踩,庆幸的是,在强大的搜索引擎与无私奉献的 ...

  7. React Native填坑之旅--ListView篇

    列表显示数据,基本什么应用都是必须.今天就来从浅到深的看看React Native的ListView怎么使用.笔者写作的时候RN版本是0.34. 最简单的 //@flow import React f ...

  8. WebApi传参总动员(填坑)

    本以为系列文章已经Over,突然记起来前面留了个大坑还没填,真是自己给自己挖坑. 这个坑就是: (body 只能被读取一次)Only one thing can read the body MVC和W ...

  9. React Native填坑之旅 -- 使用iOS原生视图(高德地图)

    在开发React Native的App的时候,你会遇到很多情况是原生的视图组件已经开发好了的.有的是系统的SDK提供的,有的是第三方试图组件,总之你的APP可以直接使用的原生视图是很多的.React ...

  10. Webapi创建和使用 以及填坑(二)

    Webapi创建和使用 以及填坑(二) 上篇文章由于时间问题没能讲到POST提交,今天做一个补充 POST: 当我们直接通过POST发送方式发送会发现错误信息 参考解决:https://www.cnb ...

随机推荐

  1. dart 中在实例化 new 关键字可以省略不写

    dart 中在实例化 new 关键字可以省略不写 class Person { String name; int age; String sex; Person(this.name, this.age ...

  2. 内容分发网络 CDN 概述

    本文分享自天翼云开发者社区<内容分发网络 CDN 概述>,作者:Jerry CDN(Content Delivery Network)是一种分布式网络架构,旨在提供高效.可靠地将内容传送给 ...

  3. Project Euler 728 题解

    Problem 728 Circle of Coins 得到 Wallbreaker5th 的指导. \(F\) 就是求这些环上区间(记为 \(A\))的异或线性基大小.令 \(A'_i\gets A ...

  4. Luogu P7250 BalticOI 山峰 题解 [ 蓝 ] [ 模拟 ] [ 并查集 ] [ BFS ]

    Luogu P7250 BalticOI 山峰. 一道大模拟,很暴力,也很难写.建议紫或蓝,标签为模拟.广度优先搜索.并查集. 思路 首先观察到答案取决于路线上的最低点,所以我们可以把所有点的高度丢进 ...

  5. initiator 连接target

    客户端     检查是否发现 [root@kvm1 ~]# iscsiadm --mode discovery --type sendtargets --portal 192.168.114.14 1 ...

  6. [SDOI2008] Sandy的卡片 题解

    讲一种自认为最暴力的方法. 首先肯定还是用差分的思想,对于每一张卡片进行重新标号,在卡片串与卡片串中插入特殊字符,然后找重复了 \(n\) 次的子串. 这里我们对于每一个子串开一个大小为 \(n\) ...

  7. 详解nginx配置url重定向-反向代理

    https://www.jb51.net/article/99996.htm 本文系统:Centos6.5_x64 三台主机:nginx主机,hostname: master.lansgg.com  ...

  8. HTTP Runner 运行提示执行后提示找不到有效的测试用例怎么解决?

    确保yaml文件编写正确 2.yaml文件名称test_xxx.yaml test开头 3.更改httprunner 版本号 pip install httprunner==1.4.2

  9. 【Unit4】UML解析器(模型化设计)-作业总结 & 【BUAA-OO】课程总结

    第四单元作业总结 1.题目概述 UML类图建模与查询(8) + UML顺序图/状态图建模与查询(3+3) + 模型错误检查(9),三次迭代共23条命令 2.构架设计 一开始以为和第三单元差不多,稍微用 ...

  10. 使用watch指令实时监控nvidia显卡状态

    当你在训练模型等需要实时检查英伟达显卡状态的时候,使用watch是很好的解决方案 相较于传统的nvidia-smi -l 1指令实时查看的显示效果不好看,watch可以标记处更新的部分,并且是动态刷新 ...