gpt4 book ai didi

c# - 如何在WindowsPhone 7 中清除图像缓存。将UriSource 设置为null 不起作用!在 WP7 中清除图像缓存还有哪些其他变体?

转载 作者:行者123 更新时间:2023-12-03 22:20:23 24 4
gpt4 key购买 nike

Stefan Wick 在他的博客中写道,我们可以使用如下代码清除 WP7 中的图像缓存

<code>
BitmapImage bitmapImage = image.Source as BitmapImage;
bitmapImage.UriSource = null;
image.Source = null;

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...

Test project 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>
  1. 运行您的应用程序。当我们第一次导航到 MainPage.xaml 时 --- 内存值接近 9676448 字节
  2. 单击导航到 Caching.xaml(图像缓存)--- 内存值接近 9011200 字节
  3. 点击 Show 按钮显示图像 -- 内存值接近 12996608 字节
  4. 点击 Avoid Image Cache(清除缓存图像),然后按下清除按钮 --- 10090144 字节 .... 那么,413696 字节在哪里消失了??? (10090144 - 9676448 = 413696 字节)
  5. 单击“返回”按钮导航回 MainPage.xaml --- 11828248 字节...但是在导航到 Caching.xaml 之前的先前值为 9676448 字节...那么,2151800 字节在哪里消失了????

    如果您将导航 20 次到页面 Caching.xaml(单击显示图像和使用缓存清除图像)并且当导航回 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;

仅此一项就可以防止图像缓存。

但是,如果您正在使用数据绑定(bind),您很快就会意识到防止图像缓存可能并不那么容易。

在我使用数据绑定(bind)的应用程序中,我设置了与此类似的内容:

假设您要在列表框中显示图像。

//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>

-

 //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));
}
}

当你使用数据绑定(bind)时(当没有直接访问 元素时)上面应该清除图像缓存

或者,您可以创建一个自定义的附加依赖属性,这样您就可以这样做:并在那里处理缓存的清除,但我不需要它。

关于c# - 如何在WindowsPhone 7 中清除图像缓存。将UriSource 设置为null 不起作用!在 WP7 中清除图像缓存还有哪些其他变体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10075099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com