[英]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>
此刻,它同时填充所有名称和图像...这导致列表框运行缓慢。
提前致谢
好了,对于延迟加载图像工作,您缺少两件事。
PriorityBinding
在加载速度慢,然后快速加载imagesources的顺序。 DownloadCompleted
事件必须通知它准备显示的绑定更改。 查看这两篇文章,看看是否可以得到提示...
让我知道是否有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.