zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
    

      

  • 相关阅读:
    IE6-9中tbody的innerHTML不能赋值bug
    matchesSelector及低版本IE中对该方法的实现
    JavaScript日期组件的实现
    IE6/7/8中parseInt第一个参数为非法八进制字符串且第二个参数不传时返回值为0
    子程序设计原则
    仅IE6中链接A的href为javascript协议时不能在当前页面跳转
    JavaScript获取图片的原始尺寸
    JavaScript判断图片是否加载完成的三种方式
    Mac OS X 快捷键
    IE6-8中Date不支持toISOString方法
  • 原文地址:https://www.cnblogs.com/Jeely/p/11003693.html
Copyright © 2011-2022 走看看