如何使用backgroundWorker填充SurfaceListBox [英] How to fill a SurfaceListBox with a backgroundWorker

查看:76
本文介绍了如何使用backgroundWorker填充SurfaceListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


嘿那里, 

Hey there, 

我想在我的SurfaceListBox中填充XML结构的项目。这可以通过将ItemSourceProperty绑定到XPath过滤的文档来实现。有时需要一段时间才能装入所有物品。出于这个原因,我想调整加载过程。 

i want to fill my SurfaceListBox with items of a XML-structure. This is possible by binding the ItemSourceProperty to the XPath filtered document. Sometimes it takes a while till all the items are loaded. For that reason i want to tweak the loading process. 

我不认为可以通过使用itemsource绑定一个接一个地添加项目。因此,我有想法在backgroundWorker线程中加载新项目。如果物品尚未加载,我可以显示加载消息。

I don't think that the it is possible to add the items one after another by using the itemsource binding. Therefore i had the idea to load the new items in a backgroundWorker thread. If the items are not loaded yet i could display a loading-message.

 


private void Item_PreviewTouchUp(object sender, TouchEventArgs e)
{
loadingLibraryItems.Visibility = Visibility.Visible;
libraryListBox.Visibility = Visibility.Collapsed;

_Worker = new BackgroundWorker();
_Worker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
_Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_WorkCompleted);
_Worker.RunWorkerAsync();
}

private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
librarySourceBinding = new Binding();
librarySourceBinding.Source = libData.Document;
librarySourceBinding.XPath = "//Entry[@Category = "+catID+"]";
libraryListBox.SetBinding(SurfaceListBox.ItemsSourceProperty, librarySourceBinding);
}

private void bgWorker_WorkCompleted(object sender, RunWorkerCompletedEventArgs e)
{
loadingLibraryItems.Visibility = Visibility.Collapsed;
libraryListBox.Visibility = Visibility.Visible;
}

推荐答案

  ;

我做同样的事情,但在Mvvm

I'm doing the same thing but in Mvvm

例如:

 

你有一个属性ObservableCollectionEx< Image> MyImageCollection = new  ObservableCollectionEx< Image>();

You have a property ObservableCollectionEx<Image> MyImageCollection = new  ObservableCollectionEx<Image>();

您将该集合设置为列表框的itemsource。

you set that collection to be the itemsource of your listbox.

 

在loadingevent或任何你想要启动它的事件中(我不使用背景工作者,但它也是一个线程)

In the loadingevent or any event you want to start it (i don't use a backgroundworker but its also a thread)

 

  &NBSP; ThreadPool.QueueUserWorkItem(o =>

    ThreadPool.QueueUserWorkItem(o =>

         {

                {

    ;                //加载图片或监视器的代码

                               //code to load image or wathever

MyImageCollection.add(新图片);  

MyImageCollection.add(the New Image);  

 

 

      ;    });

                });

如果要在加载所有项目后设置某些内容的可见性,请计算在线程集之前需要多少项目 

If you want to set the visibility of something after all items are loaded, you count how much items you need before the threadpool 

Int NeededItems = 100;

Int NeededItems = 100;

Int CurrentItems = 0;

Int CurrentItems = 0;

添加内容后线程池中的内容在你的收藏中添加:

The in the threadpool after you added something to your collection you add:

Currentitems ++;

Currentitems++;

if(CurrentItems == NeededItems)设置可见性。

if(CurrentItems == NeededItems) set the visibility.

 

以下是Observa的代码bleCollectionEx类:

Here is the code of the ObservableCollectionEx class:

 

  public class ObservableCollectionEx< T> :ObservableCollection< T>

 public class ObservableCollectionEx<T> : ObservableCollection<T>

  &NBSP; {

    {

  &NBSP; &NBSP; &NBSP; //覆盖事件,以便该课程可以访问它

        // Override the event so this class can access it

  &NBSP; &NBSP; &NBSP;公共覆盖事件System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged;

        public override event System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged;

 

  &NBSP; &NBSP; &NBSP; protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)

        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)

  &NBSP; &NBSP; &NBSP; {

        {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //很好 - 像MSDN一样使用BlockReentrancy

            // Be nice - use BlockReentrancy like MSDN said

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;使用(BlockReentrancy())

            using (BlockReentrancy())

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

            {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler = CollectionChanged;

                System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler = CollectionChanged;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(eventHandler == null)

                if (eventHandler == null)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;返回;

                    return;

 

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;委托[]委托= eventHandler.GetInvocationList();

                Delegate[] delegates = eventHandler.GetInvocationList();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //通过调用列表

                // Walk thru invocation list

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; foreach(委托中的System.Collections.Specialized.NotifyCollectionChangedEventHandler处理程序)

                foreach (System.Collections.Specialized.NotifyCollectionChangedEventHandler handler in delegates)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

                {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; DispatcherObject dispatcherObject = handler.Target as DispatcherObject;

                    DispatcherObject dispatcherObject = handler.Target as DispatcherObject;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //如果订阅者是DispatcherObject且不同的帖子

                    // If the subscriber is a DispatcherObject and different thread

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(dispatcherObject!= null&& dispatcherObject.CheckAccess()== false)

                    if (dispatcherObject != null && dispatcherObject.CheckAccess() == false)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

                    {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //在目标调度员的线程中调用处理程序

                        // Invoke handler in the target dispatcher's thread

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind,handler,this,e);

                        dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }

                    }

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; else //按原样执行处理程序

                    else // Execute handler as is

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;处理程序(this,e);

                        handler(this, e);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }

                }

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }

            }

  &NBSP; &NBSP; &NBSP; }

        }

  &NBSP; }

    }


这篇关于如何使用backgroundWorker填充SurfaceListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆