6

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

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

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

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

4

6 回答 6

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

也看这里。

于 2009-12-18T18:08:07.507 回答
1

您是否尝试过实施INotifyPropertyChanged

于 2008-09-06T06:37:10.277 回答
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);
}

这似乎工作得很好,如果有人有建议,请告诉我。

于 2009-08-11T16:36:26.100 回答
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 而不是 Application.Current.RootVisual.Dispatcher 上调用 BeginInvoke,则不应出现此异常。

于 2009-01-01T08:20:29.943 回答
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

}
于 2013-05-05T03:42:20.880 回答
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();
                }
            }
        }
    }
}
于 2013-08-15T05:40:05.043 回答