如果你的数据源需要用在多线程中,那么就应该重写你的Model定义。
一般Model需要实现了INotifyPropertyChanged后,才能通知绑定视图更新,而对于INotifyPropertyChanged接口,我们一般是这么写的: public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
然后在属性的Set方法中调用那个OnPropertyChanged方法来触发事件。
但现在是用在多线程中,因此那个方法必须要异步回调来触发,否则就会产生异常。方法如下:public abstract class ModelBase : INotifyPropertyChanged
{
SynchronizationContext context;
public ModelBase(SynchronizationContext _context)
{
context = _context;
OnPropertyChanged = propertyName =>
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
context.Post(t => handler(this, new PropertyChangedEventArgs((string)t)), propertyName);
}
};
}
public ModelBase()
{
OnPropertyChanged = propertyName =>
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
};
} public event PropertyChangedEventHandler PropertyChanged; protected Action<string> OnPropertyChanged;
}
public class A : ModelBase
{
string _Name;
public string Name
{
get { return _Name; }
set
{
if(_Name != value)
{
_Name = value;
OnPropertyChanged("Name");
}
}
}
}只要传递SynchronizationContext就能支持多线程了。
一般Model需要实现了INotifyPropertyChanged后,才能通知绑定视图更新,而对于INotifyPropertyChanged接口,我们一般是这么写的: public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
然后在属性的Set方法中调用那个OnPropertyChanged方法来触发事件。
但现在是用在多线程中,因此那个方法必须要异步回调来触发,否则就会产生异常。方法如下:public abstract class ModelBase : INotifyPropertyChanged
{
SynchronizationContext context;
public ModelBase(SynchronizationContext _context)
{
context = _context;
OnPropertyChanged = propertyName =>
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
context.Post(t => handler(this, new PropertyChangedEventArgs((string)t)), propertyName);
}
};
}
public ModelBase()
{
OnPropertyChanged = propertyName =>
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
};
} public event PropertyChangedEventHandler PropertyChanged; protected Action<string> OnPropertyChanged;
}
public class A : ModelBase
{
string _Name;
public string Name
{
get { return _Name; }
set
{
if(_Name != value)
{
_Name = value;
OnPropertyChanged("Name");
}
}
}
}只要传递SynchronizationContext就能支持多线程了。
解决方案 »
- 关于数据库datatime
- C# 捕捉异常错误
- c#.net中的treeview中的selectnodechange事件问题
- Web Application 中有没有实现Application.DoEvents()作用的方法?
- 外键关系问题
- ajax 后台取前台select的值 大哥们帮下最好举个简单的例子
- 怎样声明一个长度可变的string数组,并向数组中添加和删除值
- 如何從用","分隔的多個由數字組成的字符串得到一int的不定長數組。
- 电子书控件实现方法?
- 如何用C#隐藏进程?
- ArrayList中存放不同类型的数据
- DataTable循环遍历次数太多(大于1万次),程序死了,也无法调试,请问怎么解决?
改成这样试试 Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate()
{
// todo。。
});或者 Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(()=>{
// todo。。
}));