首页 > 代码库 > 【WPF】自定义浏览远程图片控件
【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 = http://www.mamicode.com/e.OldValue as string;> //下载远程图片到本地,下载完成后保存到本地并回调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); } }}
【WPF】自定义浏览远程图片控件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。