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