分享一个网络图片控件(WebImage for wp8)

  闲话不多说,直接上代码!!!转载请附上原作者地址哦!!!thx!!!

  WebImage.xaml页面代码如下:

 

<UserControl x:Class="WpDemo.Controls.WebImage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">

    <Grid x:Name="LayoutRoot">
        <Image x:Name="ImgShow" Stretch="UniformToFill" Visibility="Collapsed"/>
        <Grid x:Name="gdWait">
            <Rectangle x:Name="rectProgress" Fill="#FFFFB3E0" Stroke="Black" VerticalAlignment="Bottom"/>
            <TextBlock x:Name="tbProgress" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" Foreground="White" Text="0"/>
        </Grid>
    </Grid>
</UserControl>
WebImage.xaml.cs类如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.ComponentModel;
using System.IO;

namespace WpDemo.Controls
{
    public partial class WebImage : UserControl
    {
        private static String CacheFolder = "cloud_cache_image";
        private BitmapImage _imgSource = null;

        public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(WebImage), null);
        public static readonly DependencyProperty StretchProperty = DependencyProperty.Register("Stretch", typeof(Stretch), typeof(WebImage), new PropertyMetadata(Stretch.UniformToFill));
        public static readonly DependencyProperty ImgUriProperty = DependencyProperty.Register("ImgUri", typeof(string), typeof(WebImage), new PropertyMetadata("", OnImgUriChanged));


        private string _ImgUri;

        public ImageSource Source
        {
            get { return ImgShow.Source; }
            set { ImgShow.SetValue(Image.SourceProperty, value); }
        }

        public Stretch Stretch
        {
            get { return ImgShow.Stretch; }
            set { ImgShow.SetValue(Image.StretchProperty, value); }
        }

        public string ImgUri
        {
            get
            {
                return _ImgUri;
            }
            set
            {
                _ImgUri = value;
                SetImgSource(_ImgUri);
            }
        }

        void SetImgSource(string uri)
        {
            string cacheName = Path.Combine(CacheFolder, ParseUri(uri));
            _imgSource = new BitmapImage();
            _imgSource.ImageOpened += imgSource_ImageOpened;
            if (IsolatedStorageFileHelper.FileExists(cacheName))
            {
                using (Stream stream = IsolatedStorageFileHelper.OpenFile(cacheName))
                {
                    _imgSource.SetSource(stream); ;
                }
                ImgShow.Visibility = System.Windows.Visibility.Visible;
                gdWait.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                _imgSource.DownloadProgress += imgSource_DownloadProgress;
                _imgSource.ImageFailed += imgSource_ImageFailed;
                _imgSource.UriSource = new Uri(uri, UriKind.Absolute);
            }
            ImgShow.Source = _imgSource;
        }

        void imgSource_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            _imgSource.ImageOpened -= imgSource_ImageOpened;
            _imgSource.ImageFailed -= imgSource_ImageFailed;
            _imgSource.DownloadProgress -= imgSource_DownloadProgress;
        }

        void imgSource_ImageOpened(object sender, RoutedEventArgs e)
        {
            ImgShow.Visibility = System.Windows.Visibility.Visible;
            gdWait.Visibility = System.Windows.Visibility.Collapsed;
            BitmapImage img = (BitmapImage)sender;
            WriteableBitmap wb = new WriteableBitmap(img);
            using (Stream stream = IsolatedStorageFileHelper.CreateFile(Path.Combine(CacheFolder, ParseUri(img.UriSource.AbsoluteUri))))
            {
                wb.SaveJpeg(stream, img.PixelWidth, img.PixelHeight, 0, 100);
            }
            _imgSource.ImageOpened -= imgSource_ImageOpened;
            _imgSource.ImageFailed -= imgSource_ImageFailed;
            _imgSource.DownloadProgress -= imgSource_DownloadProgress;
        }

        void imgSource_DownloadProgress(object sender, DownloadProgressEventArgs e)
        {
            rectProgress.Height = gdWait.ActualHeight * (e.Progress / 100.0);
            tbProgress.Text = e.Progress.ToString();
            _imgSource.DownloadProgress -= imgSource_DownloadProgress;
        }

        public WebImage()
        {
            InitializeComponent();
        }

        private static void OnImgUriChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            WebImage img = obj as WebImage;
            img.ImgUri = e.NewValue as string;
        }

        private string ParseUri(string uri)
        {
            return uri.Replace("://", "").Replace("/", "_");
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace WpDemo.Common
{
    public class IsolatedStorageFileHelper
    {
        private static IsolatedStorageFile _Store = null;
        private static IsolatedStorageFile Store
        {
            get
            {
                if (_Store == null)
                {
                    _Store = IsolatedStorageFile.GetUserStoreForApplication();
                }
                return _Store;
            }
        }
        public static void CreateFolder(string dir)
        {
            if (Store.DirectoryExists(dir))
                return;
            Store.CreateDirectory(dir);
        }
        public static void DeleteFolder(string dir)
        {
            if (Store.DirectoryExists(dir))
                return;
            Store.DeleteDirectory(dir);
        }

        public static void DeleteFile(string path)
        {
            if (!Store.FileExists(path))
                return;
            Store.DeleteFile(path);
        }

        public static IsolatedStorageFileStream CreateFile(string path)
        {
            string[] parts = path.Split('\\');
            string dir = "";
            for (int i = 0; i < parts.Length - 1; i++)
            {
                string part = parts[i];
                dir = System.IO.Path.Combine(dir, part);
                if (!Store.DirectoryExists(dir))
                    Store.CreateDirectory(dir);
            }
            return Store.CreateFile(path);
        }

        public static bool FileExists(string path)
        {
            return Store.FileExists(path);
        }

        public static IsolatedStorageFileStream OpenFile(string path)
        {
            return Store.OpenFile(path, System.IO.FileMode.Open);
        }

        public static void SerializeObject(object obj, string path)
        {
            if (Store.FileExists(path))
                Store.DeleteFile(path);
            XmlSerializer xml = new XmlSerializer(obj.GetType());
            using (var stream = CreateFile(path))
            {
                xml.Serialize(stream, obj);
            }
        }

        public static T DeSerializeObject<T>(string path)
        {
            if (!Store.FileExists(path))
                return default(T);
            XmlSerializer xml = new XmlSerializer(typeof(T));
            object obj = null;
            using (var stream = OpenFile(path))
            {
                obj = xml.Deserialize(stream);
            }
            return (T)obj;
        }
    }
}

用法如下:

xmlns:MyControl="clr-namespace:CloudWpDemo.Controls"
<StackPanel Grid.Column="0" Width="200">
                                <MyControl:WebImage ImgUri="{Binding Img_url}" Margin="5,5,5,5"></MyControl:WebImage>
                                <!--<Image Source="{Binding Img_url}"></Image>
                                <TextBlock Text="{Binding Name}"></TextBlock>-->
                            </StackPanel>

 我测试过蛮多次。。。。此控件挺好用的!!!希望对你的开发有用!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值