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);
} }
}

最新文章

  1. iOS之分别使用代码和storyboard、xib为控件设置圆角(以按钮为例)
  2. css 3 动画
  3. js模版引擎handlebars.js实用教程——each嵌套
  4. The Genymotion Virtual device could not obtain an IP address解决办法
  5. jQuery判断元素是否存在方法
  6. C#中Guid类型值如何判断不是初始值!
  7. POJ 3126 Prime Path 解题报告(BFS &amp; 双向BFS)
  8. 负重前行的婚纱线上路 - i天下网商-最具深度的电商知识媒体
  9. 基于Grunt的版本号构建系统新手教程
  10. delphi字符串函数大全
  11. 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)
  12. 导出Excel1 - 项目分解篇
  13. duilib消息类型
  14. 【Java进阶】并发编程
  15. React使用Mobx管理数据
  16. 利用crontab定时备份nginx访问日志(也可以说是定时切分日志)
  17. phpstorm官方教程
  18. view的focusable属性改变设置是否可获取光标
  19. HTML-CSS font-family:中文字体的英文名称
  20. python groupby 函数 as_index

热门文章

  1. P4752 Divided Prime
  2. nova-conductor与AMQP(二)
  3. 改变你一生的编辑器:VSCode使用总结
  4. Parencodings(模拟)
  5. easyui 控件获取焦点方式
  6. JAVA 企业培训
  7. 记一次诡异的bug调试——————关于JDK1.7和JDK1.8中HashSet的hash(key)算法的区别
  8. typeof运算符
  9. linux学习记录.2.hello world.c
  10. redis中插入用户集合的语句,有四个属性