[英]Creating a User control in dispatcher and add it to a control in the UI
I have a long time processing operation , and for that it has to be done in the backGround, But the problem is: 我的处理时间很长 ,为此必须在backGround中完成,但是问题是:
I used the backgroundWorker, then i used the Dispatcher of the listView, then the main Dispatcher, but all with the same problem 我使用了backgroundWorker,然后使用了ListView的Dispatcher,然后使用了主Dispatcher, 但是所有问题都相同
i wonder if i can use the UIThread for that, but i dont know how. 我想知道是否可以使用UIThread,但是我不知道如何。
My code is: 我的代码是:
private void btn_click(object sender, System.Windows.RoutedEventArgs e)
{
string path = fileSourcesCombo.SelectedItem.ToString();
converter = getConverter(path);
this.Dispatcher.BeginInvoke(new Action(delegate()
{
System.Data.DataTable dataTable = converter.getDataTable();
dataGrid.Dispatcher.Invoke(new Action(delegate()
{
dataGrid.ItemsSource = dataTable.DefaultView;
}
));
List<MyAttribute> attributes = converter.attributes;
foreach (MyAttribute attribute in attributes)
{
string name = attribute.name;
string type = attribute.type;
CustomAttribute customAtt = new CustomAttribute(name, type);
ListViewControl.Dispatcher. Invoke(new Action(delegate() { ListViewControl.Items.Add(customAtt); }));
}
}
),System.Windows.Threading.DispatcherPriority.Background);
}
as i said before there is no compiling error generated, it's just that the list view seems to be populated with empty items on all of the method i tried. 正如我之前所说的,没有生成编译错误,只是列表视图似乎在我尝试的所有方法中都填充有空项。
EDIT : 编辑:
when i changed the ListView into a ListItem it worked, but i dont know why?? 当我将ListView更改为ListItem时,它起作用了,但是我不知道为什么? any how i still would like to use the listView control instead..
任何我仍然想如何使用listView控件代替。
This is the Xaml code where it works: 这是Xaml代码的工作原理:
<Grid Margin="8,0">
<ListView x:Name="testpanel" Margin="8" BorderThickness="0" DisplayMemberPath="" Style="{DynamicResource SimpleListBox}" ItemContainerStyle="{DynamicResource SimpleListBoxItem}">
</ListView>
</Grid>
if i remover the DynemicResource from : ItemContainerStyle , it doesn't work 如果我从:ItemContainerStyle移除DynemicResource,它将无法正常工作
Please post the XAML for your ListView. 请为您的ListView发布XAML。 Are you possibly missing a DisplayMemberPath?
您是否可能缺少DisplayMemberPath? That is how it would behave if it has Items but does not know what to display.
如果它有项目但不知道要显示什么,这就是它的行为。
It's not exactly possible to tell what is to blame but here are a few things that may or may not help: 不可能确切地说出应归咎于什么,但以下几件事可能有帮助也可能无济于事:
ListView.View
you should just use a ListBox
. ListView.View
,则应该使用ListBox
。 DisplayMemberPath
. DisplayMemberPath
。 BeginInvoke
should already be on the UI-thread, so those additional dispatcher-calls inside should be redundant. BeginInvoke
内部的所有内容都应该已经在UI线程上,因此内部的那些其他调度程序调用应该是多余的。 (Read the threading todel reference ) converter = getConverter(path)
is being called on a Background thread that part where you create a thread is either missing from your code or weird things are happening, ie calling an event-handler in code. converter = getConverter(path)
则您在其中创建线程的部分可能会丢失代码或发生奇怪的事情,即在代码中调用事件处理程序。 testpanel
but in the code you call ListViewControl.Items.Add
, i hope that's just some inconsistency in you code-posting. testpanel
但是在代码中,您调用ListViewControl.Items.Add
,我希望这只是代码发布中的一些不一致之处。 ItemContainerStyle
could contain all sorts of things that would mess things up. ItemContainerStyle
可能包含各种会使事情弄乱的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.