简体   繁体   English

在调度程序中创建用户控件并将其添加到UI中的控件

[英]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中完成,但是问题是:

  • When I create the user control, and then add it to the UI, (listView) control, WPF doesn't show the UC (user control), but the listView seems to be populated withe the same number of UC i created. 当我创建用户控件,然后将其添加到UI(listView)控件时,WPF不会显示UC(用户控件),但是listView似乎填充了我创建的相同数量的UC。

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);

        }
  • Converter.getDataTable() takes long time. Converter.getDataTable()需要很长时间。
  • dataGrid.Dispatcer.Invoke works properly, because it updates and exciting control. dataGrid.Dispatcer.Invoke可以正常工作,因为它可以更新并激发控件。
  • ListViewControl.Dispatcher doesn't seem to work properly, neither this.Dispatcher ListViewControl.Dispatcher似乎无法正常工作,也不是this.Dispatcher

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: 不可能确切地说出应归咎于什么,但以下几件事可能有帮助也可能无济于事:

  1. If you do not use the ListView.View you should just use a ListBox . 如果不使用ListView.View ,则应该使用ListBox
  2. If you add UI-Elements to the control you should not use a DisplayMemberPath . 如果将UI元素添加到控件,则不应使用DisplayMemberPath
  3. Everything inside a BeginInvoke should already be on the UI-thread, so those additional dispatcher-calls inside should be redundant. BeginInvoke内部的所有内容都应该已经在UI线程上,因此内部的那些其他调度程序调用应该是多余的。 (Read the threading todel reference ) (阅读线程todel参考
  4. If 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)则您在其中创建线程的部分可能会丢失代码或发生奇怪的事情,即在代码中调用事件处理程序。
  5. The control in your XAML is called testpanel but in the code you call ListViewControl.Items.Add , i hope that's just some inconsistency in you code-posting. XAML中的控件称为testpanel但是在代码中,您调用ListViewControl.Items.Add ,我希望这只是代码发布中的一些不一致之处。
  6. The ItemContainerStyle could contain all sorts of things that would mess things up. ItemContainerStyle 可能包含各种会使事情弄乱的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM