Silverlight数据绑定跨线程问题

6
我有一个Image控件,它的源绑定到对象的属性(指向图像的字符串URL)。在调用服务后,我使用新的URL更新数据对象。但是,在触发PropertyChanged事件后,异常被抛出并离开我的代码。
数据结构和服务逻辑都在一个核心dll中完成,该dll不了解UI。当我无法访问Dispatcher时,如何与UI线程同步?
附:访问Application.Current.RootVisual以获取Dispatcher不是解决方案,因为根可视化对象在不同的线程上(导致需要避免的确切异常)。
补充:这仅对图像控件造成问题,绑定到任何其他UI元素,跨线程问题将为您处理。
6个回答

7
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => {...});

Also look here.


1

0

我遇到了类似的问题,不过是在Windows窗体应用中:

我有一个类,它拥有自己的线程,更新另一个进程的统计信息,我的UI中有一个控件与该对象进行数据绑定。我遇到了跨线程调用问题,以下是我的解决方法:

Form m_MainWindow; //Reference to the main window of my application
protected virtual void OnPropertyChanged(string propertyName)
{
  if(PropertyChanged != null)
    if(m_MainWindow.InvokeRequired)
      m_MainWindow.Invoke(
        PropertyChanged, this, new PropertyChangedEventArgs(propertyName);
    else
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName);
}

这看起来很不错,如果有任何建议,请告诉我。


0
Application类中RootVisual属性的getter方法有一个线程检查,这会导致异常。我通过将根可视化对象的调度程序存储在我的App.xaml.cs文件中自己的属性中来解决了这个问题。
public static Dispatcher RootVisualDispatcher { get; set; }

private void Application_Startup(object sender, StartupEventArgs e)
{
    this.RootVisual = new Page();
    RootVisualDispatcher = RootVisual.Dispatcher;
}

如果您在 App.RootVisualDispatcher 上调用 BeginInvoke 而不是 Application.Current.RootVisual.Dispatcher,那么您就不应该会收到这个异常。

0
无论何时我们想要更新与 UI 相关的项目,该操作都应在 UI 线程中进行,否则您将收到一个无效的跨线程访问异常。
Deployment.Current.Dispatcher.BeginInvoke( () =>

{

UpdateUI(); // DO the actions in the function Update UI

});

public void UpdateUI()

{

//to do :Update UI elements here

}

0

INotifyPropertyChanged 接口用于通知客户端(通常是绑定客户端),某个属性值已经发生了变化。

例如,考虑一个名为 FirstName 的属性的 Person 对象。为了提供通用的属性更改通知,Person 类型实现了 INotifyPropertyChanged 接口,并在 FirstName 更改时引发 PropertyChanged 事件。

为了在绑定客户端和数据源之间进行更改通知,您的绑定类型应该执行以下操作之一:

实现 INotifyPropertyChanged 接口(首选)。

为绑定类型的每个属性提供更改事件。

不要同时执行两者。

示例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

// Change the namespace to the project name. 
namespace TestNotifyPropertyChangedCS
{
    // This form demonstrates using a BindingSource to bind 
    // a list to a DataGridView control. The list does not 
    // raise change notifications. However the DemoCustomer type  
    // in the list does. 
    public partial class Form1 : Form
    {
        // This button causes the value of a list element to be changed. 
        private Button changeItemBtn = new Button();

        // This DataGridView control displays the contents of the list. 
        private DataGridView customersDataGridView = new DataGridView();

        // This BindingSource binds the list to the DataGridView control. 
        private BindingSource customersBindingSource = new BindingSource();

        public Form1()
        {
            InitializeComponent();

            // Set up the "Change Item" button.
            this.changeItemBtn.Text = "Change Item";
            this.changeItemBtn.Dock = DockStyle.Bottom;
            this.changeItemBtn.Click +=
                new EventHandler(changeItemBtn_Click);
            this.Controls.Add(this.changeItemBtn);

            // Set up the DataGridView.
            customersDataGridView.Dock = DockStyle.Top;
            this.Controls.Add(customersDataGridView);

            this.Size = new Size(400, 200);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Create and populate the list of DemoCustomer objects 
            // which will supply data to the DataGridView.
            BindingList<DemoCustomer> customerList = new BindingList<DemoCustomer>();
            customerList.Add(DemoCustomer.CreateNewCustomer());
            customerList.Add(DemoCustomer.CreateNewCustomer());
            customerList.Add(DemoCustomer.CreateNewCustomer());

            // Bind the list to the BindingSource. 
            this.customersBindingSource.DataSource = customerList;

            // Attach the BindingSource to the DataGridView. 
            this.customersDataGridView.DataSource =
                this.customersBindingSource;

        }

        // Change the value of the CompanyName property for the first  
        // item in the list when the "Change Item" button is clicked.
        void changeItemBtn_Click(object sender, EventArgs e)
        {
            // Get a reference to the list from the BindingSource.
            BindingList<DemoCustomer> customerList =
                this.customersBindingSource.DataSource as BindingList<DemoCustomer>;

            // Change the value of the CompanyName property for the  
            // first item in the list.
            customerList[0].CustomerName = "Tailspin Toys";
            customerList[0].PhoneNumber = "(708)555-0150";
        }

    }

    // This is a simple customer class that  
    // implements the IPropertyChange interface. 
    public class DemoCustomer : INotifyPropertyChanged
    {
        // These fields hold the values for the public properties. 
        private Guid idValue = Guid.NewGuid();
        private string customerNameValue = String.Empty;
        private string phoneNumberValue = String.Empty;

        public event PropertyChangedEventHandler PropertyChanged;

        // This method is called by the Set accessor of each property. 
        // The CallerMemberName attribute that is applied to the optional propertyName 
        // parameter causes the property name of the caller to be substituted as an argument. 
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        // The constructor is private to enforce the factory pattern. 
        private DemoCustomer()
        {
            customerNameValue = "Customer";
            phoneNumberValue = "(312)555-0100";
        }

        // This is the public factory method. 
        public static DemoCustomer CreateNewCustomer()
        {
            return new DemoCustomer();
        }

        // This property represents an ID, suitable 
        // for use as a primary key in a database. 
        public Guid ID
        {
            get
            {
                return this.idValue;
            }
        }

        public string CustomerName
        {
            get
            {
                return this.customerNameValue;
            }

            set
            {
                if (value != this.customerNameValue)
                {
                    this.customerNameValue = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public string PhoneNumber
        {
            get
            {
                return this.phoneNumberValue;
            }

            set
            {
                if (value != this.phoneNumberValue)
                {
                    this.phoneNumberValue = value;
                    NotifyPropertyChanged();
                }
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接