码迷,mamicode.com
首页 > Windows程序 > 详细

【WPF】自定义浏览远程图片控件

时间:2014-10-14 17:03:18      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   for   sp   div   on   cti   

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

 

【WPF】自定义浏览远程图片控件

标签:style   blog   io   ar   for   sp   div   on   cti   

原文地址:http://www.cnblogs.com/wywnet/p/4024333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!