Windows phone 7 本地保存图像&;显示windows phone 7
我正在尝试使用WP7实现一个新闻应用程序(feedrss)。我尝试过这个解决方案,但它对我不起作用,下面是我的代码:Windows phone 7 本地保存图像&;显示windows phone 7,windows-phone-7,local-storage,Windows Phone 7,Local Storage,我正在尝试使用WP7实现一个新闻应用程序(feedrss)。我尝试过这个解决方案,但它对我不起作用,下面是我的代码: namespace PhoneApp1 { public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent();} private void Go_Click(object sender, RoutedEventArgs
namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();}
private void Go_Click(object sender, RoutedEventArgs e)
{
WebClient _client = new WebClient();
_client.DownloadStringCompleted += Feed;
Location.Text = "http://www.aufaitmaroc.com/feeds/ma-vie.xml";
_client.DownloadStringAsync(new Uri((Location.Text)));
InitializeComponent();
}
private void Feed(object Sender, DownloadStringCompletedEventArgs e)
{
XElement _xml;
try
{
if (!e.Cancelled)
{
_xml = XElement.Parse(e.Result);
List<FeedItem> l = new List<FeedItem>();
foreach (XElement value in _xml.Elements("channel").Elements("item"))
{
FeedItem _item = new FeedItem();
_item.Title = value.Element("title").Value;
_item.enclosure = value.Element("enclosure").Attribute("url").Value;
_item.Description = Regex.Replace(value.Element("description").Value,
@"<(.|\n)*?>", String.Empty);
_item.Link = value.Element("link").Value;
_item.Guid = value.Element("guid").Value;
_item.Published = DateTime.Parse(value.Element("pubDate").Value);
l.Add(_item);
HttpWebRequest reqest1 = (HttpWebRequest)WebRequest.Create(_item.enclosure);
reqest1.BeginGetResponse(DownloadImageCallback, reqest1);
Thread.Sleep(1000);
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri(_item.enclosure), client);
}
listBox.ItemsSource = l;
}
}
catch
{
}
}
IsolatedStorageFile MyStore = IsolatedStorageFile.GetUserStoreForApplication();
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
var resInfo = new StreamResourceInfo(e.Result, null);
var reader = new StreamReader(resInfo.Stream);
byte[] contents;
using (BinaryReader bReader = new BinaryReader(reader.BaseStream))
{
contents = bReader.ReadBytes((int)reader.BaseStream.Length);
}
IsolatedStorageFileStream stream = MyStore.CreateFile("10.jpg");
stream.Write(contents, 0, contents.Length);
stream.Close();
}
void DownloadImageCallback(IAsyncResult result)
{
HttpWebRequest req1 = (HttpWebRequest)result.AsyncState;
HttpWebResponse responce = (HttpWebResponse)req1.EndGetResponse(result);
Stream s = responce.GetResponseStream();
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
string directory = "Imagestest";
if (!MyStore.DirectoryExists(directory))
{
MyStore.CreateDirectory(directory);
IsolatedStorageFileStream stream = MyStore.CreateFile("10.jpg");
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var isoFileStream = myIsolatedStorage.CreateFile(directory + "//ANALYSE_10052013095620.jpg"))
{
var bitimage = new BitmapImage();
var wb = new WriteableBitmap(bitimage);
var width = wb.PixelWidth;
var height = wb.PixelHeight;
// bitimage.SetSource = isoFileStream;
image1.Source = bitimage;
System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
}
}
}
else
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(directory + "//ANALYSE_10052013095620.jpg"))
{
myIsolatedStorage.DeleteFile(directory + "//ANALYSE_10052013095620.jpg");
}
using (var isoFileStream = myIsolatedStorage.CreateFile(directory + "//ANALYSE_10052013095620.jpg"))
{
var bitimage = new BitmapImage();
var wb = new WriteableBitmap(bitimage);
var width = wb.PixelWidth;
var height = wb.PixelHeight;
System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
}
}
}
});
}
private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
FeedItem currentFeedItem = (FeedItem)listBox.SelectedItem;
PhoneApplicationService.Current.State["FeedItem"] = currentFeedItem;
}
名称空间PhoneApp1
{
公共部分类主页:PhoneApplicationPage
{
公共主页()
{
InitializeComponent();}
私有无效Go_单击(对象发送者,路由目标e)
{
WebClient _client=新的WebClient();
_client.DownloadStringCompleted+=Feed;
Location.Text=”http://www.aufaitmaroc.com/feeds/ma-vie.xml";
_DownloadStringAsync(新Uri((Location.Text));
初始化组件();
}
私有void提要(对象发送方,下载StringCompletedEventArgs e)
{
XElement xml;
尝试
{
如果(!e.取消)
{
_xml=XElement.Parse(e.Result);
列表l=新列表();
foreach(xml.Elements(“频道”).Elements(“项目”)中的元素值)
{
FeedItem _item=新的FeedItem();
_item.Title=value.Element(“Title”).value;
_item.enclosure=value.Element(“enclosure”).Attribute(“url”).value;
_item.Description=Regex.Replace(value.Element(“Description”).value,
@“”,String.Empty);
_item.Link=value.Element(“Link”).value;
_item.Guid=value.Element(“Guid”).value;
_item.Published=DateTime.Parse(value.Element(“pubDate”).value);
l、 添加(_项);
HttpWebRequestRequest1=(HttpWebRequest)WebRequest.Create(_item.enclosure);
reqest1.BeginGetResponse(下载ImageCallback,reqest1);
睡眠(1000);
WebClient客户端=新的WebClient();
client.OpenReadCompleted+=新的OpenReadCompletedEventHandler(client\u OpenReadCompleted);
OpenReadAsync(新Uri(_item.enclosure),客户端);
}
listBox.ItemsSource=l;
}
}
抓住
{
}
}
IsolatedStorageFile MyStore=IsolatedStorageFile.GetUserStoreForApplication();
无效客户端\u OpenReadCompleted(对象发送方,OpenReadCompletedEventArgs e)
{
var resInfo=新的StreamResourceInfo(例如,Result,null);
var reader=newstreamreader(resInfo.Stream);
字节[]内容;
使用(BinaryReader bReader=newBinaryReader(reader.BaseStream))
{
contents=bReader.ReadBytes((int)reader.BaseStream.Length);
}
IsolatedStorageFileStream=MyStore.CreateFile(“10.jpg”);
stream.Write(contents,0,contents.Length);
stream.Close();
}
void DownloadImageCallback(IAsyncResult结果)
{
HttpWebRequest req1=(HttpWebRequest)result.AsyncState;
HttpWebResponse Response=(HttpWebResponse)req1.EndGetResponse(结果);
流s=response.GetResponseStream();
Deployment.Current.Dispatcher.BeginInvoke(()=>
{
string directory=“Imagestest”;
如果(!MyStore.directory存在(目录))
{
创建目录(目录);
IsolatedStorageFileStream=MyStore.CreateFile(“10.jpg”);
使用(IsolatedStorage File myIsolatedStorage=IsolatedStorage File.GetUserStoreForApplication())
{
使用(var isoFileStream=myIsolatedStorage.CreateFile(目录+“//analysis_10052013095620.jpg”))
{
var BITMAGE=新的BitmapImage();
var wb=新的可写位图(BITMAGE);
var-width=wb.PixelWidth;
var height=wb.PixelHeight;
//bitimage.SetSource=isoFileStream;
图像1.源=比特图像;
System.Windows.Media.Imaging.Extensions.SaveJpeg(wb,isoFileStream,宽度,高度,0,100);
}
}
}
其他的
{
使用(IsolatedStorage File myIsolatedStorage=IsolatedStorage File.GetUserStoreForApplication())
{
if(myIsolatedStorage.FileExists(目录+“//analysis_10052013095620.jpg”))
{
myIsolatedStorage.DeleteFile(目录+“//analysis_10052013095620.jpg”);
}
使用(var isoFileStream=myIsolatedStorage.CreateFile(目录+“//analysis_10052013095620.jpg”))
{
var BITMAGE=新的BitmapImage();
var wb=新的可写位图(BITMAGE);
var-width=wb.PixelWidth;
var height=wb.PixelHeight;
System.Windows.Media.Imaging.Extensions.SaveJpeg(wb,isoFileStream,宽度,高度,0,100);
}
}
}
});
}
私有无效列表框\u SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
FeedItem currentFeedItem=(FeedItem)列表框。选择编辑项;
PhoneApplicationService.Current.State[“FeedItem”]=currentFeedItem;
}
当我运行此项目时,什么都没有发生,它不会创建任何文件夹,当我停止internet时,图像将不再显示。如果您想在本地保存图像并从本地保存显示图像,请尝试此示例
,在WP 8上工作正常。如果您的应用程序中需要图像缓存,我可以为您推荐库 特点:
- 在内存中缓存(因此它可以非常快速地处理列表、网格等)
- 缓存是隔离的存储文件(防止每次重新加载)
- 完全异步(无延迟)
- 通过XAML绑定转换器使用,您无需更改代码