必须在DependencyObject的同一线程上创建DependencySource [英] Must create DependencySource on same Thread as DependencyObject
问题描述
class ListOfItems
{
public List< SomeObject> ListToBind;
public void DownloadItems()
{
Task.Factory.StartNew(()=>
{
...
...
if(OnDownloadCompleted!= null)
OnDownloadCompleted(this,EventArgs.Empty);
}
}
}
class SomeObject
{
public string NameOfItem;
public MyClass Properties;
}
class MyClass
{
public int Percentage;
public SolidColorBrush Color;
}
这是我使用的对象模型,它被简化版本,我不希望你重新组织,有一个原因我这样写。在 ListOfItems
类是做所有的工作(还有一些其他用于使代码可读的方法) - 下载源码,解析和填充 ListToBind
与数据,fe
[0] => NameOfItem =FirstOne,Properties = {99,#FF00FF00}
[1] => NameOfItem =SecondOne,属性= {50,#FFFF0000}
等
As您可以看到,当这种方法 DownloadItems
完成其工作时,会引发 OnDownloadCompleted
事件。在主线程中是以下代码
void listOfItems_OnDownloadCompleted(object sender,EventArgs args)
{
dataGrid .Dispatcher.Invoke(new Action(()=> {
dataGrid.ItemsSource = ListOfItemsInstance.ListToBind;
}));
}
DataGrid在 MainWindow.xaml
由于以下xaml代码片段而被填充:$ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $假>
< DataGrid.Columns>
< DataGridTextColumn Header =TagBinding ={Binding Name}/>
< DataGridTextColumn Header =ColorBinding ={Binding MyClass.Percentage}>
<! - < DataGridTextColumn.CellStyle>
< Style TargetType =DataGridCell>
< Setter Property =BackgroundValue ={Binding MyClass.Color}/>
< / Style>
< /DataGridTextColumn.CellStyle> - >
< / DataGridTextColumn>
< /DataGrid.Columns>
< / DataGrid>
它的工作原理很好。但是有这个问题。尝试取消注释xaml snippet注释,您将获得必须在DependencyObject相同的线程上创建DependencySource。
错误。
最后,我的问题是,如何避免这个错误?
编辑:
SolidColorBrush 是一个 Freezable 是派生的DispatcherObject。 DispatcherObject具有线程关联性 - 即它只能在创建它的线程上使用/交互。然而,Freezables确实提供了冻结实例的能力。这将防止对象的任何进一步更改,但它也将释放线程亲和性。所以你可以改变它,使你的属性不存储一个DependencyObject像SolidColorBrush,而只是存储颜色。或者您可以使用冻结方法冻结您正在创建的SolidColorBrush。
I have an application written in wpf, which downloads some webpages, parses html code and saves some values.
class ListOfItems
{
public List<SomeObject> ListToBind;
public void DownloadItems()
{
Task.Factory.StartNew(() =>
{
...
...
if (OnDownloadCompleted != null)
OnDownloadCompleted(this, EventArgs.Empty);
}
}
}
class SomeObject
{
public string NameOfItem;
public MyClass Properties;
}
class MyClass
{
public int Percentage;
public SolidColorBrush Color;
}
This is the object model I'm using. It's simplified version and I don't want you to reorganize it, there is a reason I wrote it this way. In ListOfItems
class is method which does all the job (there are some other methods used inside to make code readable) - downloads source, parses and fills ListToBind
with data, f.e.
[0] => NameOfItem = "FirstOne", Properties = {99, #FF00FF00}
[1] => NameOfItem = "SecondOne", Properties = {50, #FFFF0000}
etc.
As you can see, when this method DownloadItems
completes its job, OnDownloadCompleted
event is raised. In the main thread is following code
void listOfItems_OnDownloadCompleted(object sender, EventArgs args)
{
dataGrid.Dispatcher.Invoke(new Action(() => {
dataGrid.ItemsSource = ListOfItemsInstance.ListToBind;
}));
}
DataGrid on the MainWindow.xaml
is filled with values, because of following xaml code snippet.
<DataGrid Name="dataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Tag" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Color" Binding="{Binding MyClass.Percentage}">
<!--<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding MyClass.Color}" />
</Style>
</DataGridTextColumn.CellStyle>-->
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
It works just fine. But there is this problem. Try to uncomment commented xaml snippet and you will get Must create DependencySource on same Thread as the DependencyObject.
error.
Finally, my question is, how to avoid this error?
EDIT:
It should look like this in the end. This picture is taken from MS Excel and coloured in Adobe Photoshop.
The SolidColorBrush is a Freezable which is a derived DispatcherObject. DispatcherObjects have thread affinity - i.e it can only be used/interacted with on the thread on which it was created. Freezables however do offer the ability to freeze an instance. This will prevent any further changes to the object but it will also release the thread affinity. So you can either change it so that your property is not storing a DependencyObject like SolidColorBrush and instead just store the Color. Or you can freeze the SolidColorBrush that you are creating using the Freeze method.
这篇关于必须在DependencyObject的同一线程上创建DependencySource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!