• ObservableCollection


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Collections.ObjectModel;
    using System.Globalization;
    
    namespace imgLoadApp
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            ObservableCollection<Uri> images = null;
    
            // 用于线程间同步的对象
            object lockobj = new object();
    
            public MainWindow()
            {
                InitializeComponent();
                images = new ObservableCollection<Uri>();
                Binding b = new Binding
                {
                    Source = images,
                    IsAsync = true
                };
                lbImages.SetBinding(ItemsControl.ItemsSourceProperty, b);
    
                // 这一句很关键,开启集合的异步访问支持
                BindingOperations.EnableCollectionSynchronization(images, lockobj);
    
                // 异步加载数据
                Task.Run(() =>
                {
                    // 代码写在 lock 块中
                    lock (lockobj)
                    {
                        for (int i = 0; i < 20000; i++)
                        {
                            Uri u = new Uri("0.jpg", UriKind.Relative);
                            images.Add(u);
                        }
                    }
                });
            }
        }
    
        public sealed class UriToBitmapConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                Uri uri = (Uri)value;
                BitmapImage bmp = new BitmapImage();
                bmp.DecodePixelHeight = 250; // 确定解码高度,宽度不同时设置
                bmp.BeginInit();
                // 延迟,必要时创建
                bmp.CreateOptions = BitmapCreateOptions.DelayCreation;
                bmp.CacheOption = BitmapCacheOption.OnLoad;
                bmp.UriSource = uri;
                bmp.EndInit(); //结束初始化
                return bmp;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return null;
            }
        }
    }
    

      

  • 相关阅读:
    将ASCII字符串转换为UNICODE字符串
    GetLastError()返回值大全
    C++构造函数的调用
    DOM – 7.动态创建DOM + 8.innerText innerHTML value
    DOM
    DOM – 4.doucument属性
    用jquery操作xml文件
    請推薦有關網路的書
    Linux命令全称
    轻松架设时时监控工具Cacti
  • 原文地址:https://www.cnblogs.com/Jeely/p/11003693.html
Copyright © 2020-2023  润新知