Ill*_*zun 2 c# windows-phone-7
Stefan Wick在他的博客中写道,我们可以使用类似的代码清除WP7中的图像缓存
<code>
BitmapImage bitmapImage = image.Source as BitmapImage;
bitmapImage.UriSource = null;
image.Source = null;
Run Code Online (Sandbox Code Playgroud)
http://blogs.msdn.com/b/swick/archive/2012/04/05/10151249.aspx?CommentPosted=true#commentmessage
我已经测试了他的示例 - 项目(ImageTips.zip)---我可以说上面的代码不起作用---它不会释放缓存图像的内存,也不会删除应用程序的缓存图像.
当您将UriSource设置为null时 - 您只释放页面Caching.xaml上的内存,但如果您将导航回MainPage.xaml ---您将看到内存未释放---内存已增加!
要看到我们可以在他的博客中使用Stefan的项目--- ImageTips.zip ......
要重现我的观察结果---您可以:1.将代码添加到MainPage.xaml以查看当前内存值,就像在Caching.xaml页面上一样:
<tab>
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(500);
timer.Start();
timer.Tick += delegate
{
GC.Collect();
tbMemory.Text = string.Format("Memory: {0} bytes", DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage"));
};
</code>
Run Code Online (Sandbox Code Playgroud)
单击按钮返回导航回MainPage.xaml --- 11828248字节...但导航到Caching.xaml之前的前一个值是9676448字节...所以,在哪里消失了2151800字节????
如果您将导航20次到页面Caching.xaml(单击show image并清除带缓存的图像),当导航回MainPage.xaml时---使用的内存将增加到3.3 MB ...依此类推
我在我的应用程序中遇到此问题,并且不知道如何解决它.
在Windows Phone 7中清除图像缓存还有哪些其他变体?或者为什么通过将UriSource设置为null来清除图像缓存---导航回上一页时不起作用(不释放内存)?
谢谢.
我可以向您保证,博客中给出的示例有效.请注意,您在示例中处理的内存量非常小,如果您在此处或那里"丢失"了2MB,则无关紧要.这可能是因为GC没有开始,或者同时分配了一些其他对象.(如果您确实遇到严重泄漏,那么您必须检查代码中的其他位置)
在我自己的应用程序中,我正在处理大量图像.在任何给定时间,它们消耗多达60 MB的内存.因此,我非常偏执,没有任何内存泄漏通过认证.
博客中的示例假定您以编程方式添加图像,并且您可以访问它们,因此可以将源设置为null.
BitmapImage bitmapImage = image.Source as BitmapImage;
bitmapImage.UriSource = null;
image.Source = null;
Run Code Online (Sandbox Code Playgroud)
仅此一项就可以防止图像缓存.
但是,如果您正在使用数据绑定,您将很快意识到防止图像缓存可能不那么容易.
在我使用数据绑定的应用程序中,我设置了类似于此的东西:
假设您要在列表框中显示图像.
//MainPage.xaml
<StackPanel>
<ListBox ItemsSource="{Binding Pictures}">
<ListBox.ItemTemplate>
<DataTemplate>
<Image MaxHeight="1000" Source="{Source}" />
</DataTemplate>
</ListBox.ItemTeplate>
</ListBox>
<Button Tap="LoadImages_Tap" Content="Load Images" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
-
//Picture.cs
public class Picture
{
private BitmapImage _source = new BitmapImage()
{
CreateOptions = BitmapCreateOptions.BackgroundCreation | BitmapCreateOptions.IgnoreImageCache | BitmapCreateOptions.DelayCreation
};
public Picture(string url) {
_source.UriSource = new Uri(url, UriKind.Absolute);
}
public BitmapImage Source
{
get
{
return _source;
}
}
}
}
//MainPage.xaml.cs
private ObservableCollection<Picture> _pictures = new ObservableCollection<Picture>();
public ObservableCollection<Picture> Pictures {
get {
return _pictures;
}
}
public MainPage() {
//...
DataContext = this;
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
{
PreventCaching();
_pictures.Clear();
}
base.OnNavigatedFrom(e);
}
private void PreventCaching() {
foreach(var picture in _pictures) {
picture.Source.UriSource = null;
}
}
private void LoadPictures_Tap(object sender, EventArgs e) {
PreventCaching();
_pictures.Clear();
foreach(var url in SomeMethodThatReturnsUrlsForImages()) {
_pictures.Add(new Picture(url));
}
}
Run Code Online (Sandbox Code Playgroud)
使用数据绑定时,上面应该清除图像缓存(当没有直接访问<Image>元素时)
或者你可以创建一个自定义附加依赖项属性,所以你可以这样做:<Image ext:ImageExtension.Source ="{SomeSource}"/>并处理清除那里的缓存但我不需要它.
| 归档时间: |
|
| 查看次数: |
3513 次 |
| 最近记录: |