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

这篇博客探讨了在WPF应用中,由于在非UI线程中尝试创建SolidColorBrush对象导致的错误。作者展示了如何通过Dispatcher.Invoke确保在正确的线程上执行该操作,从而避免必须在与DependencyObject相同的线程上创建DependencySource的异常。解决方案是在后台任务中使用Dispatcher来切换到UI线程进行SolidColorBrush的实例化。
摘要由CSDN通过智能技术生成

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

MVVM实体类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace WpfApp3
{
    public class TestInfo : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public Brush BG { get; set; }
    }
}

下面是报错代码:

<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid Background="{Binding BG}">
        
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp3
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Loaded += MainWindow_Loaded;
        }

        private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            TestInfo info = new TestInfo();
            await Task.Run(() =>
            { 
                info.BG = new SolidColorBrush(Colors.Red);
            });
            this.DataContext = info;
        }
    }
}

原因:

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

调整后的后端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp3
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Loaded += MainWindow_Loaded;
        }

        private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            TestInfo info = new TestInfo();
            await Task.Run(() =>
            {
                Brush brush = null;
                Dispatcher.Invoke(() =>
                {
                    brush = new SolidColorBrush(Colors.Red);
                });
                info.BG = brush;
            });
            this.DataContext = info;
        }
    }
}

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值