搞了好长一阵子wp,做点好事。

C/S手机app中应用最多的是  获取网络图片,缓存到本地,展示图片

本次主要对其中的delay:LowProfileImageLoader进行修改,在获取图片的时候,加入本地缓存,和弱引用。

demo截图:

  

缓存相关:

1,App.xml.cs文件中通过IsolatedStorageFile创建缓存图片用的文件夹

// 应用程序启动(例如,从“开始”菜单启动)时执行的代码
// 此代码在重新激活应用程序时不执行
private void Application_Launching(object sender, LaunchingEventArgs e)
{
string imageCacheDriectory = "ImagesCache";
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.DirectoryExists(imageCacheDriectory) != true)
{
store.CreateDirectory(imageCacheDriectory);
}
}
}

2,LowProfileImageLoader.cs中 image在uri修改的事件中加入判断,如果本地已经缓存图片文件,直接读取本地;如果本地没有缓存,则通过网络获取,并缓存到本地

 private static void OnUriSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var image = (Image)o;
var uri = (Uri)e.NewValue; if (!IsEnabled || DesignerProperties.IsInDesignTool)
{
// Avoid handing off to the worker thread (can cause problems for design tools)
image.Source = new BitmapImage(uri);
}
else
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
string imageCacheFileName = App.IMAGECACHEDIRECTORY + ToInternalKey(uri.ToString()); if (store.FileExists(imageCacheFileName))
{
// check local image file
BitmapImage bitmapImage = new BitmapImage();
using (var imageStream = store.OpenFile(imageCacheFileName, FileMode.Open, FileAccess.Read))
{
bitmapImage.SetSource(imageStream);
}
WeakReference<BitmapImage> mybitimage = new WeakReference<BitmapImage>(bitmapImage);
image.Source = mybitimage.Target;
}
else
{
// Clear-out the current image because it's now stale (helps when used with virtualization)
image.Source = null;
lock (_syncBlock)
{
// Enqueue the request
_pendingRequests.Enqueue(new PendingRequest(image, uri));
Monitor.Pulse(_syncBlock);
}
}
}
}
} static String ToInternalKey(String value)
{
if (String.IsNullOrEmpty(value))
{
return String.Empty;
}
String exName = value.Substring(value.LastIndexOf('.')); byte[] bytes = UTF8Encoding.GetBytes(value);
return Convert.ToBase64String(bytes) + exName;
}
private static readonly Encoding UTF8Encoding = Encoding.UTF8;

3,Mainpage.xmal.cs中 相应清除缓存按钮事件中。查找缓存图片的文件夹,找到所有文件并逐个删除。(注:清除缓存图片过程中不能直接删除文件夹,且在移除的过程中要注意相关路劲是否正确,否则缓存并没有移除成功)

 /// <summary>
/// 清除缓存
/// </summary>
private void btn_clear_Click(object sender, RoutedEventArgs e)
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.DirectoryExists("ImagesCache") == true)
{
string[] filelist = store.GetFileNames("ImagesCache/");
foreach (string st in filelist)
{
store.DeleteFile("ImagesCache/"+st);
}
}
}
MessageBox.Show("图片缓存清理完毕");
if (images != null)
images.Clear();
}

核心的获取网络代码是 微软一个叫David Anson所写的demo中所摘取,

其作用是:

在应用后台开多一个线程用于获取网络图片,优化ui线程。简化相关开发代码。

具体见:

http://blogs.msdn.com/b/delay/archive/2010/09/02/keep-a-low-profile-lowprofileimageloader-helps-the-windows-phone-7-ui-thread-stay-responsive-by-loading-images-in-the-background.aspx?Redirected=true

http://blogs.msdn.com/b/delay/archive/2010/09/08/never-do-today-what-you-can-put-off-till-tomorrow-deferredloadlistbox-and-stackpanel-help-windows-phone-7-lists-scroll-smoothly-and-consistently.aspx

第一次写博客,欢迎大神拍砖

下载demo请戳:http://files.cnblogs.com/fatlin/ImageLoader.rar

ImageLoader(多线程网络图片加载)+本地缓存 for windowsphone 7的更多相关文章

  1. Glide 4.0.0 下之加载本地缓存的图片

    在网上搜了下,无意中发现RequestOptions还有个方法: onlyRetrieveFromCache 用了下是OK的 try { File imageFile = Glide.with(con ...

  2. 55、Android网络图片 加载缓存处理库的使用

         先来一个普通的加载图片的方法. import android.annotation.SuppressLint; import android.app.Activity; import and ...

  3. 【代码笔记】iOS-实现网络图片的异步加载和缓存

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. se ...

  4. android ImageLoader加载本地图片的工具类

    import android.widget.ImageView; import com.nostra13.universalimageloader.core.ImageLoader; /** * 异步 ...

  5. Flutter -------- 加载本地图片资源和网络图片

    在Flutter加载本地图片资源 在Flutter项目目录下创建文件夹 images ,在文件夹中添加几张图片 指定资源 pubspec.yaml文件中 version: 1.0.0+1 enviro ...

  6. imageview加载本地和网络图片

    ImageView是Android程序中经常用到的组件,它将一个图片显示到屏幕上. 在UI xml定义一个ImageView如下: public void onCreate(Bundle savedI ...

  7. Android 多线程 异步加载

    Android 应用中需要显示网络图片时,图片的加载过程较为耗时,因此加载过程使用线程池进行管理, 同时使用本地缓存保存图片(当来回滚动ListView时,调用缓存的图片),这样加载和显示图片较为友好 ...

  8. Android艺术——Bitmap高效加载和缓存(1)

    通过Bitmap我们可以设计一个ImageLoader,实现应该具有的功能是: 图片的同步加载:图片的异步加载:图片的压缩:内存缓存:磁盘缓存:网络获取: 1.加载 首先提到加载:BitmapFact ...

  9. Android之网络图片加载的5种基本方式

    学了这么久,最近有空把自己用到过的网络加载图片的方式总结了出来,与大家共享,希望对你们有帮助. 此博客包含Android 5种基本的加载网络图片方式,包括普通加载HttpURLConnection.H ...

随机推荐

  1. [Angular 2] More on *ngFor, @ContentChildren & QueryList<>

    In previous artical, we introduce the how to use *ngFor. The limitation for previous solution to dis ...

  2. ie提示jquer缺少标识符,字符串或数字

    属性之间是要用","分隔的,但最后一个属性的后面在IE中是不能有的,firefox可有可无. 至于最后的";"是另外一回事了.这是Javascript的语法问题 ...

  3. [PHP] Eclipse开发PHP环境配置

    首先准备好软件: 1. Apache,到这里找个最新版本 2. PHP,到这里下载 3. Eclipse IDE for Java EE Developers,到这里下载 4. DLTK Core F ...

  4. css笔记06:层叠样式选择器

    1. (1)HTML文件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...

  5. MYSQL基础笔记(一)

    关系型数据库概念: 1.什么是关系型数据库? 关系型数据库:是一种建立在关系模型(数学模型)上的数据库 关系模型:一种所谓建立在关系上的模型. 关系模型包含三个方面: 1.数据结构:数据存储的问题,二 ...

  6. TextFiled 中输入金额

    要求: 输入的金额不能超过六位, 小数点后面只能输入两位小数 如果 textFIled  中第一位输入的是0 ,后面必须输入小数点,否则禁止输入 用到 textfiled代理方法 #pragma ma ...

  7. 视频播放-VideoVIew,Vitamio

    播放视频文件其实并不比播放音频文件复杂,主要是使用 VideoView类来实现的.这个类将视频的显示和控制集于一身,使得我们仅仅借助它就可以完成一个简易的视频播放器.VideoView的用法和 Med ...

  8. [ImportNew]Java中的Timer类和TimerTask类

    http://www.importnew.com/9978.html java.util.Timer是一个实用工具类,该类用来调度一个线程,使它可以在将来某一时刻执行. Java的Timer类可以调度 ...

  9. Windows 8.1 归档 —— Step 1 选择与安装

    下面是 Windows 8.1 各版本区别: Windows 8.1 标准版(一般就称之为Windows 8.1): 包括全新的 Windows 商店.Windows 资源管理器.任务管理器等等,还将 ...

  10. Eclipse去除jquery引入错误

    之前在写Java项目时,总是出现引入jquery报错,虽然对其方法的应用没有什么影响,但是感觉难受,经过百度得到解决的方法: 第一步:去除eclipse的JS验证:将windows->prefe ...