搞了好长一阵子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. DataTable转换为List<Model>的通用类

    在开发中,把查询结果以DataTable返回很方便,但是在检索数据时又很麻烦,没有模型类型检索方便. 所以很多人都是按照以下方式做的: // 获得查询结果DataTable dt = DbHelper ...

  2. hive 经常使用命令

    1.查看表结构信息   desc formatted table_name;   desc table_name; 查看关联文件: desc extended f_tblog_online_mds; ...

  3. mysql 面试

    数据库的重要性是所有技术里最核心最需要掌握的(理解原理,并且被面试时能清晰的表达出来),直接决定运维人员薪水的高低! 所有题都要给出专业的解答方案,不是很水的那种泛泛的解答. 面试题001:什么是My ...

  4. 设置用户ID和设置组ID

    与一个进程关联的ID有6个或更多,如下图所示: 与每个进程相关联的用户ID和组ID 实际用户ID 实际组ID 我们实际是谁 有效用户ID 有效组ID 附加组ID 用于文件访问权限检索 保存的设置用户I ...

  5. linux read 用法

    1.基本读取 read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(后面在说).得到输入后,read命令将数据放入一个标准变量中.下面是 read命令 的最简单形式:: #!/bin/bas ...

  6. xcode笔记

    1.Alt键的使用   2.设置捕捉所有意外断点:停在代码出错处     2015年07月27日09:52:12 3.搜索 command + F:在当前的文件中搜索 command + Shift ...

  7. PHP中使用kindeditor

    KindEditor是一套开源的HTML可视化编辑器,主要用于让用户在网站上获得所见即所得编辑效果,兼容IE.Firefox.Chrome. Safari.Opera等主流浏览器.KindEditor ...

  8. ruby 疑难点之—— attr_accessor attr_reader attr_writer

    普通的实例变量 普通的实例变量,我们没法在 class 外面直接访问 #普通的实例变量,只能在 class 内部访问 class C1 def initialize(name) @name = nam ...

  9. 网络编程(发送get和post请求到服务器端,并获取响应)

    一:B/S结构,浏览器端到服务器端通信依赖http协议 交互过程: 1:在浏览器地址栏输入http://ip:port/应用/资源路径 2:浏览器根据ip和服务器建立连接,port确定和那个应用进行交 ...

  10. poj 2585 拓扑排序

    这题主要在于建图.对9个2*2的小块,第i块如果出现了不等于i的数字,那么一定是在i之后被brought的.可以从i到该数字建一条边. 图建好后,进行一次拓扑排序,判段是否存在环.若存在环,那么就是B ...