我有两个类观察集合变化将不更新UI从背景,但一个正常的属性获取通知
public class ConccClass<T> : ObservableCollection<T>
{
}
和
public class TestTherad: INotifyPropertyChanged
{
private string name;
public string Name
{ get {
return name;
}
set
{
if (value != name)
{
name = value;
RaisePropertyChanged("Name");
}
}
}
//// the notification implemented fully here
}
现在我已经在创建“ConccClass”的集合我查看模型并将其绑定到xaml中的datagrid中。
问题 当我在后台线程上添加一个项目而没有任何dispacther时,它不反映在datagrid中。意味着没有添加项目。 Todo,我必须在分派器中添加项目。 BeginInvoke的。这对我有意义。
但更新任何项目的名称我不需要调度员。
Task.Factory.StartNew(() =>
{
while (true)
{
Thread.Sleep(100);
{
this.Dispatcher.Invoke(() => this.Coll.Add(new TestTherad())); // **Works well**
//this.Coll.Add(new TestTherad()); // **does not work at all.**
this.Coll[0].Name = r.Next().ToString(); // ** without dispatcher works well.**
}
}
});
为什么这样的行为?
哪个版本的.Net? – OmegaMan 2014-11-09 23:15:02
使用dotPeek,在'INotifyPropertyChanged'里面有'event PropertyChangedEventHandler PropertyChanged'。看看'PropertyChangedEventHandler'显示它是一个具有以下属性的代理'[HostProtection(SecurityAction.LinkDemand,SharedState = true)]'。我想知道[SharedState](http://msdn.microsoft.com/zh-cn/library/system.security.permissions.hostprotectionattribute.sharedstate(v = vs.110).aspx)是否允许委托成功运行主线程无需调用。 – 2014-11-10 03:21:27
@OmegaMan - VS 2012. – 2014-11-12 06:51:30