搞了好长一阵子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. [AngularJS] ngModelController render function

    ModelValue and ViewValue: $viewValue: Actual string value in the view. $modelValue: The value in the ...

  2. 压力单位MPa、Psi和bar之间换算公式

    压力单位MPa.Psi和bar之间换算公式 1bar=10^5PaPsi为英制压力单位.“磅力每平方英寸(1bf/in2)为1psi=6894.76 pa: 1bar等于10的5次方=10^5 pa ...

  3. C++编译器默默编写并调用哪些函数

    什么时候empty class(空类)不再是个empty class呢?当C++处理过它之后,是的,如果你自己没有声明,编译器就会为它声明(编译器版本)一个copy构造函数.一个copy assign ...

  4. qwt总结1

    废话不想太多,说下自己的使用总结. 一般来说,对于一个图表. 可能的话,应该有一个坐标轴,QWT中,是用QwtPlot这个类 来描述一个图的坐标系图(只表示坐标系的背景,没有描点).坐标(刻度)的设置 ...

  5. unity 4.x 从入门到精通(持续更新)

    为了做毕业设计开始学习unity 3d,但发现书中有很多错误,所以在这里将我遇到的一些错误及我的解决办法贴出来 1.414页 按照书中的方法设置后起点和终点之间没有连接关系路径的,需要在bake前设置 ...

  6. 判断脚本,图片,CSS,iframe等是否加载完成

    1.图片 <img id="MyImg" src="src"/>jquery实现:$("#MyImg").load(functi ...

  7. mysql重连,连接丢失:The last packet successfully received from the server--转载

    原文地址:http://nkcoder.github.io/blog/20140712/mysql-reconnect-packet-lost/ 1.1 错误信息: Caused by: com.my ...

  8. USB相关知识

    USB基础知识概论 如何实现Linux下的U盘(USB Mass Storage)驱动: How to WriteLinux USB MSC (Mass Storage Class) Driver U ...

  9. Matrix multiplication hdu4920

    Problem Description Given two matrices A and B of size n×n, find the product of them. bobo hates big ...

  10. Python Thread

    lock 对象: acquire():负责取得一个锁.如果没有线程正持有锁,acquire方法会立刻得到锁.否则,它闲意态等锁被释放. 一旦acquire()返回,调用它的线程就持有锁. releas ...