Dispatcher在一个委托中调用多个控件?

时间:2014-01-17 09:38:44

标签: wpf

我正在研究WPF应用程序, 我必须从google API获取picasa相册并绑定到WPF listview。

我必须绑定到2个listview并使用Dispatcher.Invoke()。

以下是代码段:

   private void BindPicasa()
    {
        //My custom Google helper class.
        GoogleClass google = new GoogleClass();

        ThreadStart start = delegate()
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal,
                 new Action(delegate()
                 {
                     //Fetch the album list
                     List<AlbumClass.Album> album = google.RequestAlbum(GoogleID);

                     //bind to a 1st listview in text title.
                     ListLeftAlbum.DataContext = album;

                     //bind to a 2nd listview in thumbnail preview.
                     ListMainAlbum.Visibility = System.Windows.Visibility.Visible;
                     ListMainAlbum.DataContext = album;
                 }));
        };
        new Thread(start).Start();
    }

启动时UI会冻结, 但是,如果我拿出每个listview并使用自己的Dispatcher运行,那很好,UI负责,但这似乎不是优雅的方式。 有人推荐吗?谢谢你!

   private void BindPicasa()
    {
        //My custom Google helper class.
        GoogleClass google = new GoogleClass();

        ThreadStart start = delegate()
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal,
                 new Action(delegate()
                 {
                     //Fetch the album list
                     List<AlbumClass.Album> album = google.RequestAlbum(GoogleID);
                 }));
            ListLeftAlbum.Dispatcher.Invoke(DispatcherPriority.Normal,
                 new Action(delegate()
                 {
                     //bind to a 1st listview in text title.
                     ListLeftAlbum.DataContext = album;
                 }));
            ListMainAlbum.Dispatcher.Invoke(DispatcherPriority.Normal,
                 new Action(delegate()
                 {
                     //bind to a 2nd listview in thumbnail preview.
                     ListMainAlbum.Visibility = System.Windows.Visibility.Visible;
                     ListMainAlbum.DataContext = album;
                 }));
        };
        new Thread(start).Start();
    }

1 个答案:

答案 0 :(得分:1)

您的第一个版本几乎是正确的,您只需要在后台线程上获取相册,而不是将请求发送回ui线程

private void BindPicasa()
{
    //My custom Google helper class.
    GoogleClass google = new GoogleClass();

    ThreadStart start = delegate()
    {
        //Fetch the album list
        List<AlbumClass.Album> album = google.RequestAlbum(GoogleID);

        Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal,
             new Action(delegate()
             {
                 //bind to a 1st listview in text title.
                 ListLeftAlbum.DataContext = album;

                 //bind to a 2nd listview in thumbnail preview.
                 ListMainAlbum.Visibility = System.Windows.Visibility.Visible;
                 ListMainAlbum.DataContext = album;
             }));
    };
    new Thread(start).Start();
}