簡體   English   中英

WPF ListBox異步綁定

[英]WPF ListBox Aynchronous Binding

我有一個執行自動填充功能的列表框,該功能會動態填充。 列表框顯示帶有員工照片的姓名列表。 我發現用圖像填充數據很慢。

我希望能夠先填充名稱,然后在接收到數據時異步上載數據。 我該怎么做?

目前,我的Image類代碼:

public class Img : INotifyPropertyChanged
{
    private string name;
    private Image image;
    public event PropertyChangedEventHandler PropertyChanged;

    public Img(string name, Image image)
    {
        this.name = name;
        this.image = image;
    }

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            OnPropertyChanged("PersonName");
        }
    }

    public Image Image
    {
        get { return image; }
        set
        {
            image = value;
            OnPropertyChanged("Image");
        }
    }

    // Create the OnPropertyChanged method to raise the event
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

填充數據的代碼:

        foreach (KeyValuePair<string, string> entry in items)
        {
            System.Windows.Controls.Image webImage = new System.Windows.Controls.Image();
            webImage.Dispatcher.Invoke(DispatcherPriority.Normal,
                (ThreadStart)delegate
                {

                    BitmapImage image = new BitmapImage();
                    image.BeginInit();
                    image.UriSource = new Uri(//Where the source is);
                    image.EndInit();

                    webImage.Source = image;
                });
            myListBox.Items.Add(new Img(entry.Value, webImage));
        }

我的XAML代碼:

<Popup Name="myPopUp">
    <ListBox Name="myListBox" FontSize="14">
        <ListBox.ItemTemplate>
            <DataTemplate DataType="{x:Type local:Img}">
                <StackPanel Orientation="Horizontal">
                    <ContentPresenter Margin="3" Content="{Binding Image, IsAsync=True}"/>
                    <TextBlock Margin="3" Text="{Binding Name, IsAsync=True}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Popup>

此刻,它同時填充所有名稱和圖像...這導致列表框運行緩慢。

提前致謝

好了,對於延遲加載圖像工作,您缺少兩件事。

  1. PriorityBinding在加載速度慢,然后快速加載imagesources的順序。
  2. 每個圖像的DownloadCompleted事件必須通知它准備顯示的綁定更改。

查看這兩篇文章,看看是否可以得到提示...

如何將圖像byte []延遲加載到WPF圖像控件中?

WPF Image Control中的初始圖像

讓我知道是否有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM