【WPF】自定义控件之远程图片浏览
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; namespace WpfCopy.Controls { class CacheImageControl : Control { public bool IsShare { get { return (bool)GetValue(IsSharesShareFileProperty); } set { SetValue(IsSharesShareFileProperty, value); } } public static readonly DependencyProperty IsSharesShareFileProperty = DependencyProperty.Register("IsShare", typeof(bool), typeof(CacheImageControl), new PropertyMetadata(false)); public int ClientId { get { return (int)GetValue(ClientIdProperty); } set { SetValue(ClientIdProperty, value); } } // Using a DependencyProperty as the backing store for ClientId. This enables animation, styling, binding, etc... public static readonly DependencyProperty ClientIdProperty = DependencyProperty.Register("ClientId", typeof(int), typeof(CacheImageControl), new PropertyMetadata(0)); public ImageSource ImageSource { get { return (ImageSource)GetValue(ImageSourceProperty); } set { SetValue(ImageSourceProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("MyProperty", typeof(ImageSource), typeof(CacheImageControl)); public string ImagePath { get { return (string)GetValue(ImagePathProperty); } set { SetValue(ImagePathProperty, value); } } // Using a DependencyProperty as the backing store for ImagePath. This enables animation, styling, binding, etc... public static readonly DependencyProperty ImagePathProperty = DependencyProperty.Register("ImagePath", typeof(string), typeof(CacheImageControl), new PropertyMetadata(null, OnImagePathChanged)); public bool IsFailed { get { return (bool)GetValue(IsFailedProperty); } set { SetValue(IsFailedProperty, value); } } // Using a DependencyProperty as the backing store for IsFailed. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsFailedProperty = DependencyProperty.Register("IsFailed", typeof(bool), typeof(CacheImageControl), new PropertyMetadata(false)); public bool IsLoading { get { return (bool)GetValue(IsLoadingProperty); } set { SetValue(IsLoadingProperty, value); } } // Using a DependencyProperty as the backing store for IsLoading. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsLoadingProperty = DependencyProperty.Register("IsLoading", typeof(bool), typeof(CacheImageControl), new PropertyMetadata(false)); public bool IsEmpty { get { return (bool)GetValue(IsEmptyProperty); } set { SetValue(IsEmptyProperty, value); } } // Using a DependencyProperty as the backing store for IsEmpty. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsEmptyProperty = DependencyProperty.Register("IsEmpty", typeof(bool), typeof(CacheImageControl), new PropertyMetadata(true)); public static readonly RoutedEvent ImagePathChangedEvent = EventManager.RegisterRoutedEvent("ImagePathChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CacheImageControl)); public event RoutedEventHandler ImagePathChanged { add { AddHandler(ImagePathChangedEvent, value); } remove { RemoveHandler(ImagePathChangedEvent, value); } } static CacheImageControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(CacheImageControl), new FrameworkPropertyMetadata(typeof(CacheImageControl))); } public static void OnImagePathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var sender = d as CacheImageControl; if (sender != null) { var oldValue = e.OldValue as string; var newValue = e.NewValue as string; sender.OnImagePathChanged(oldValue, newValue); } } protected virtual void OnImagePathChanged(string oldValue, string newValue) { if (string.Equals(oldValue, newValue)) return; IsLoading = IsEmpty = IsFailed = false; if (string.IsNullOrWhiteSpace(newValue)) { IsEmpty = true; } else { IsLoading = true;
//下载远程图片到本地,下载完成后保存到本地并回调Call_Back方法,呈现本地图片 FileCacheMgr.Instance.GetUserFile(newValue, ClientId, CacheFileEventHandler_CallBack); } } ///显示下载图片 void CacheFileEventHandler_CallBack(object sender, CacheFileEventArgs e) { if (Application.Current == null) return; Application.Current.Dispatcher.BeginInvoke(new Action<CacheFileEventArgs>(E => { IsLoading = false; if (E.IsFaulted) { IsFailed = true; } else { ImageSource = new BitmapImage(new Uri(Path.GetFullPath(E.CacheFile.LocalFile), UriKind.Absolute)); } }), e); } } }