项目有这样的需求,
要求窗口加载一揽子图片,为了不让UI阻塞太久,采用异步读取后绑定显示的方案.
图片的下载应该采用并发的过程(等待网络响应会很耗时,一张一张的下载,等待时间太长)
图片的下载不能占用过多的线程数,应有个阀值(图片不是核心业务,不能占用那么多资源)
在图片加载的过程中,如果用户有操作,比如窗口跳转,则未加载完成的图片加载的过程应取消(为了替用户节省流量).
需求就是这么多了,如何实现呢?
思路是这样的,由于需要异步,且需要等待,首先想到使用队列,先让队列排列起来,再定量迭代读取.
因为要涉及异步的取消,想到了用WebClient对象的异步功能, 当然,所以发起异步请求之后的对象我都需要记录,
所以还需要一个list容器.
外部接口是两个参数,url,图片的网址,一个回调,定义了图片下载完成后的操作.
内部的核心流程,
1.将一个图片任务从队列中取出,
2.异步发生此请求,
3.将发起请求的对象放进容器,以备撤销时使用.
撤销的核心流程是.
1.让处理线程停止
2.取消队列中的任务,
3.让等待响应的任务取消.
需要用到以下命名空间:
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.IO;
using System.ComponentModel;
using System.Threading;
先声明两个回调
public delegate void GetDataStreamCallback(Stream stream);
public delegate void GetPicCallback(BitmapSource bimage);
第一个是从网络读取流的回调,第二个是生成了图片源之后的回调.
定义了一个接口:
/// <summary>
/// 可以撤销的操作
/// </summary>
interface IRevocable
{
void RevokeAsync();
event Action ProcessCompleted;
}
一个方法,是用来撤销操作的,
一个事件,是当异步完成时触发的.(不管是正常完成还是撤销,都视为操作完成)
做一个类继承该接口,用来获取网络数据
public class HttpResourceGet : IRevocable
{
public event GetDataStreamCallback OnDataStreamGenerated;
public event Action ProcessCompleted;
WebClient m_client;
public HttpResourceGet()
{
m_client = new WebClient();
m_client.OpenReadCompleted += ((send, ev) =>
{
do
{
if (ev.Error != null || ev.Cancelled)
{
break;
}
if (OnDataStreamGenerated != null)
{
OnDataStreamGenerated(ev.Result);
ev.Result.Close();
}
} while (false);
if (ProcessCompleted != null)
{
ProcessCompleted();
}
});
}
public void BeginGetData(string url)
{
m_client.OpenReadAsync(new Uri(url));
}
public void RevokeAsync()
{
m_client.CancelAsync();
}
}
再做一个类,把网络数据包装为图片源
public class HttpPicGet : IRevocable
{
public event GetPicCallback OnImageLoadCompleted;
public event Action ProcessCompleted;
HttpResourceGet m_httpGet;
public HttpPicGet()
{
m_httpGet = new HttpResourceGet();
m_httpGet.OnDataStreamGenerated += (stream =>
{
BitmapSource bi = new BitmapImage();
bi.SetSource(stream);
if (OnImageLoadCompleted != null)
{
OnImageLoadCompleted(bi);
}
});
m_httpGet.ProcessCompleted += (() =>
{
if (ProcessCompleted != null)
{
ProcessCompleted();
}
});
}
public void BeginLoadPic(string url)
{
m_httpGet.BeginGetData(url);
}
public void RevokeAsync()
{
m_httpGet.RevokeAsync();
}
}
做一个容器,用来处理多条任务
public class RevocableContainer
{
private class QueueItem
{
public GetPicCallback action;
public string url;
}
const int Threshold = 3;
AutoResetEvent m_event;
int m_count;
bool m_isThreadProcessing;
Queue<QueueItem> m_queue;
List<IRevocable> m_list;
object m_lock;
public RevocableContainer()
{
m_event = new AutoResetEvent(false);
m_queue = new Queue<QueueItem>();
m_list = new List<IRevocable>();
m_lock = new object();
m_count = Threshold;
m_isThreadProcessing = false;
}
void HttpRequestThread()
{
while (true)
{
if (m_count == 0)
{
m_event.WaitOne();
}
QueueItem item = null;
//out from queue
lock (m_queue)
{
if (!m_isThreadProcessing)
{
break;
}
if (m_queue.Count == 0)
{
break;
}
item = m_queue.Dequeue();
Interlocked.Decrement(ref m_count);
}
//do request
HttpPicGet pic = new HttpPicGet();
pic.OnImageLoadCompleted += (img =>
{
item.action(img);
});
pic.ProcessCompleted += (() =>
{
lock (m_list)
{
m_list.Remove(pic);
}
if (m_count == 0)
{
m_event.Set();
}
Interlocked.Increment(ref m_count);
});
pic.BeginLoadPic(item.url);
//into list
lock (m_list)
{
m_list.Add(pic);
}
Thread.Sleep(1);
}
}
public void EnQueue(string url, GetPicCallback action)
{
QueueItem item = new QueueItem() { action = action, url = url };
BackgroundWorker worker = null;
lock (m_queue)
{
m_queue.Enqueue(item);
if (!m_isThreadProcessing)
{
m_isThreadProcessing = true;
worker = new BackgroundWorker();
}
}
if (worker != null)
{
worker.DoWork += ((send, ev) => HttpRequestThread());
worker.RunWorkerCompleted += ((send, ev) =>
{
lock (m_queue)
{
m_isThreadProcessing = false;
}
});
worker.RunWorkerAsync();
}
}
public void CancelAll()
{
lock (m_queue)
{
m_isThreadProcessing = false;
m_queue.Clear();
}
lock (m_list)
{
foreach (IRevocable item in m_list)
{
item.RevokeAsync();
}
}
}
}
做异步绑定需要的类
public class MyImage : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
string m_url;
BitmapSource m_source;
public string URL
{
get { return m_url; }
set
{
if (m_url != value)
{
m_url = value;
OnPropertyChanged(new PropertyChangedEventArgs("URL"));
}
}
}
public BitmapSource Source
{
get { return m_source; }
set
{
if (m_source != value)
{
m_source = value;
OnPropertyChanged(new PropertyChangedEventArgs("Source"));
}
}
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
if (PropertyChanged != null)
PropertyChanged(this, args);
}
}
业务的部分都做完了
接下来就是做一个例子来验证成果了
public partial class MainPage : PhoneApplicationPage
{
RevocableContainer m_container = new RevocableContainer();
// Constructor
public MainPage()
{
InitializeComponent();
}
private void DoClick(object sender, RoutedEventArgs e)
{
string[] sources = new string[]
{
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526395.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526396.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526397.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526398.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526399.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526400.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526401.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526402.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526403.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526404.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526405.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526406.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526407.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526408.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526409.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526410.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526411.jpg",
"http://gb.cri.cn/mmsource/images/2008/05/26/ei080526412.jpg"
};
MyImage[] imgs = new MyImage[sources.Length];
for (int i = 0; i < imgs.Length; ++i)
{
imgs[i] = new MyImage();
MyImage imgItem = imgs[i];
imgItem.URL = sources[i]+ "?rand=" + Guid.NewGuid().ToString();//加Guid保证调试时无缓存
m_container.EnQueue(imgItem.URL, (bitsource => imgItem.Source = bitsource));
}
lbContent.DataContext = imgs;
}
private void RevokeClick(object sender, RoutedEventArgs e)
{
m_container.CancelAll();
}
}
XAML页面代码如下:
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ListBox Height="670" x:Name="lbContent" Grid.Row="0" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Height="100" Width="100" Source="{Binding Source, Mode=OneWay}" />
<TextBlock Text="{Binding URL}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" VerticalAlignment="Bottom" Margin="12,0,12,0">
<StackPanel Orientation="Horizontal">
<Button x:Name="btnDo" Width="100" Height="100" Content="DO" Click="DoClick" />
<Button x:Name="btnRevoke" Width="100" Height="100" Content="Revoke" Click="RevokeClick" />
</StackPanel>
</Grid>
</Grid>
运行起来,达到需要所要求的效果
分享到:
相关推荐
本书旨在分享强大的Microsoft编程工具(诸如Silverlight、XNA和Visual Studio 2010 Express Edition一类)的使用心得,介绍了如何使用现有的C#和.NET编程技能来开发一些Windows Phone 7应用程序。本书作者团队探讨了...
Silverlight for Windows Phone 7
Windows Phone 读取网络图片前显示默认图片
Silverlight for Windows Phone Toolkit.msi 其中包含了很多Windows Phone 开发需要的组件。 你正在开发Windows Phone程序的时候, 当你需要添加日历控件的时候,你发现在基本工具箱里面没有你想要的空间,并且也没有...
Windows Phone Blog Menu是一个Silverlight导航控件,看起来像Windows Phone 7。 控件中的tiles链接到web站点在你的博客中使用这个控件可以表达你对WP7的喜爱。 Windows Phone Blog Menu is a Silverlight ...
Silverlight for Windows Phone开发系列课程(9):在Windows Phone设备上进行调试 Silverlight for Windows Phone开发系列课程(10):应用程序生命周期
这个系列主要讲述一个网络收音机的开发过程,这一网络收音机程序涉及到Silverlight控件的使用,图片的操作与使用,图片的transform和animation,网络媒体的播放等等各个方面。与此同时,我还会文章中讲述Silverlight...
第四章 Silverlight for Windows Phone 7: Hello Windows Phone 7 第五章 当 Windows Azure 碰到了 Windows Phone 7 推送通知服务概述 Silverlight 交互开发: 第六章 了解 Windows Phone 7 网页浏览器控件 – Part...
Windows phone 7人人图片快速上传图片
本书适合于Windows Phone 7开发入门的读者,也适合于从其他智能手机平台转向Windows Phone 7平台的读者,同时对于有Windows Phone 7开发经验的读者也极具参考价值。 本书系统论述了Windows Phone 7操作系统的应用...
Windows_Phone_7入门经典-使用Silverlight和XNA开发Windows_Phone应用
While numerous books cover both Silverlight and XNA, there lacks a resource that covers the specifics of Windows Phone 7 development. This book fills that void and shares the specifics of building ...
windows phone 7 code
Within the pages that follow, I show you the basics of writing applications for Windows Phone 7 using the C# programming language with the Silverlight and XNA 2D frameworks. Yes, Programming Windows...
Windows Phone 7介绍,移动互联网大会资料
This book is a gift from the Windows Phone 7 team at Microsoft to the programming community, and I am proud to have been a part of it. Within the pages that follow, I show you the basics of writing...
基于Windows_Phone_7的Silverlight开发_PDF1
主要是解决了Windows Phone将本地图片上传到Web Service端的代码示例。