Silverlight DataBinding交叉线程问题

时间:2008-09-05 22:04:39

标签: silverlight multithreading data-binding

我有一个Image控件,它的源绑定到对象上的属性(字符串url到图像)。在进行服务调用后,我使用新URL更新数据对象。在调用PropertyChanged事件后,在抛出我的代码后抛出异常。

数据结构和服务逻辑都是在不知道UI的核心dll中完成的。当我无法访问Dispatcher时,如何与UI线程同步?

PS:访问Application.Current.RootVisual以获取Dispatcher不是一个解决方案,因为根视图位于不同的线程上(导致我需要阻止的确切异常)。

PPS:这只是图像控件的问题,绑定到任何其他ui元素,交叉线程问题是为你处理的。

6 个答案:

答案 0 :(得分:7)

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

同时查看here.

答案 1 :(得分:1)

您是否尝试过实施INotifyPropertyChanged

答案 2 :(得分:0)

Application类上RootVisual的属性getter有一个导致该异常的线程检查。我通过在我的App.xaml.cs中将root visual的调度程序存储在我自己的属性中来解决这个问题:

public static Dispatcher RootVisualDispatcher { get; set; }

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

如果您随后在App.RootVisualDispatcher而不是Application.Current.RootVisual.Dispatcher上调用BeginInvoke,则不应该出现此异常。

答案 3 :(得分: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);
}

这似乎很有效,如果有人有建议,请告诉我。

答案 4 :(得分: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

}

答案 5 :(得分: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();
                }
            }
        }
    }
}