- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个 Windows Phone 8.1 应用程序,它要求我在 GridView 中显示图片库中的所有图像。我已经构建了一个名为 VirtualList 的类,它是一个支持增量加载的列表,并且我已将图片库中的所有图像添加到该列表中。当图像数量减少(少于 80 张照片)时,一切正常,但当超过 80 张照片时,应用程序会因 OutOfMemoryException 而关闭。我想目前没有显示的项目没有保存在内存中,或者是吗?就我的目的而言,我应该继续使用增量加载,还是应该切换到随机访问数据虚拟化?如果我应该切换到随机访问数据虚拟化,您能否提供一个有关如何实现它的示例?
我的代码如下:
虚拟列表.cs
class VirtualList : List<Windows.UI.Xaml.Media.ImageSource>, ISupportIncrementalLoading
{
private IReadOnlyList<StorageFile> photos;
public VirtualList(IReadOnlyList<StorageFile> files) : base()
{
photos = files;
}
public bool HasMoreItems
{
get
{
return this.Count < photos.Count;
}
}
public Windows.Foundation.IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
return LoadMoreItemsAwaitable(count).AsAsyncOperation<LoadMoreItemsResult>();
}
private async Task<LoadMoreItemsResult> LoadMoreItemsAwaitable(uint count)
{
for (int i = Count; i < Count + count; i++)
{
using (var fileStream = await photos[i].OpenAsync(FileAccessMode.Read))
{
BitmapImage bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(fileStream);
this.Add(bitmapImage);
}
}
return new LoadMoreItemsResult { Count = count };
}
}
XAML 代码 (MainPage.xaml):
<GridView x:Name="photosGrid" Height="392" Width="400" ItemsSource="{Binding}" Margin="0,0,-0.333,0" SelectionMode="Multiple" Background="Black">
<GridView.ItemTemplate>
<DataTemplate>
<Image Width="90" Height="90" Margin="5" Source="{Binding}" Stretch="UniformToFill"/>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
MainPage.xaml.cs代码
//This code is inside OnNavigatedTo method
var files = await KnownFolders.CameraRoll.GetFilesAsync();
VirtualList imageList = new VirtualList(files);
photosGrid.DataContext = imageList;
最佳答案
这里有很多问题。
第一个是你的收藏需要实现INotifyPropertyChanged
正确支持增量加载(不要问我为什么)。幸运的是它很容易修复:只需继承自 ObservableCollection<T>
而不是 List<T>
.
第二个问题来自您对 LoadMoreItemsAwaitable
的实现.更具体地说,for
循环:
for (int i = Count; i < Count + count; i++)
{
using (var fileStream = await photos[i].OpenAsync(FileAccessMode.Read))
{
BitmapImage bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(fileStream);
this.Add(bitmapImage);
}
}
每次您将一个项目添加到集合 ( this.Add(bitmapImage)
) 时,Count
的值增加。结果是 i
和 Count
两者同时增加,使您的循环无限。为防止出现这种情况,请保存 Count
的值循环外:
int offset = this.Count;
for (int i = offset; i < offset + count && i < photos.Count; i++)
{
using (var fileStream = await photos[i].OpenAsync(FileAccessMode.Read))
{
BitmapImage bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(fileStream);
this.Add(bitmapImage);
}
}
请注意,我还检查了 i
低于photos.Count
,否则您可能会遇到 ArgumentOufOfRangeException。
从现在开始,您可以尝试一下,您会发现它会奏效。不过,如果您向下滚动列表,内存会不断增加。为什么?因为你正在存储你的 BitmapImage
,从而抵消了虚拟化的好处。
让我解释一下:
不幸的是,我认为没有完美的方法可以用 ISupportIncrementalLoading
解决这个问题(没有 API 告诉网格需要重新显示以前的元素,因此您需要始终保留它们)。但是您仍然可以通过仅存储文件路径而不是 BitmapImage
来避免占用内存。 .
问题:有一种方法可以填充 Image
通过仅提供路径(通过使用 ms-appx
URI 方案)进行控制,但据我所知,它不适用于图片库中存储的图片。所以你确实需要返回BitmapImage
在某些时候控制。起初,我考虑编写一个转换器(将路径转换为 BitmapImage
,但它需要异步 API,并且转换器是同步的......我能想到的最简单的解决方案是制作自己的 Image
控件,即可以加载这种路径。Image
控件是密封的,因此您不能直接从它继承(有时,我认为 WinRT 是专门为惹恼开发人员而设计的),但您可以将其包装在 UserControl 中。
让我们创建一个名为 LocalImage
的用户控件. XAML 只是包装了 Image
控制:
<UserControl
x:Class="StackOverflowUniversal10.LocalImage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Image x:Name="Image" Width="90" Height="90" Margin="5" Source="{Binding}" Stretch="UniformToFill"/>
</UserControl>
在代码隐藏中,我们创建一个依赖属性,并使用它来加载图片:
public sealed partial class LocalImage : UserControl
{
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof (string),
typeof (LocalImage), new PropertyMetadata(null, SourceChanged));
public LocalImage()
{
this.InitializeComponent();
}
public string Source
{
get { return this.GetValue(SourceProperty) as string; }
set { this.SetValue(SourceProperty, value); }
}
private async static void SourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var control = (LocalImage)obj;
var path = e.NewValue as string;
if (string.IsNullOrEmpty(path))
{
control.Image.Source = null;
}
else
{
var file = await StorageFile.GetFileFromPathAsync(path);
using (var fileStream = await file.OpenAsync(FileAccessMode.Read))
{
BitmapImage bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(fileStream);
control.Image.Source = bitmapImage;
}
}
}
}
然后修改您的页面以使用 UserControl 而不是 Image
控制:
<GridView x:Name="photosGrid" Height="382" Width="400" ItemsSource="{Binding}" Margin="0,0,-0.333,0" SelectionMode="Multiple" Background="Black">
<GridView.ItemTemplate>
<DataTemplate>
<local:LocalImage Width="90" Height="90" Margin="5" Source="{Binding}"/>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
最后但同样重要的是,更改您的收藏以存储路径而不是图片:
class VirtualList : ObservableCollection<string>, ISupportIncrementalLoading
{
private IReadOnlyList<StorageFile> photos;
public VirtualList(IReadOnlyList<StorageFile> files) : base()
{
photos = files;
}
public bool HasMoreItems
{
get
{
return this.Count < photos.Count;
}
}
public Windows.Foundation.IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
return LoadMoreItemsAwaitable(count).AsAsyncOperation<LoadMoreItemsResult>();
}
private async Task<LoadMoreItemsResult> LoadMoreItemsAwaitable(uint count)
{
int offset = this.Count;
for (int i = offset; i < offset + count && i < photos.Count; i++)
{
this.Add(photos[i].Path);
}
return new LoadMoreItemsResult { Count = count };
}
}
它应该可以工作,内存消耗稳定。请注意,您可以(并且应该)通过设置 DecodePixelHeight
进一步减少内存消耗。和 DecodePixelWidth
你的属性 BitmapImage
(这样运行时将在内存中加载缩略图而不是全分辨率图片)。
关于c# - Windows Phone 8.1 - 在 gridview 上使用图片库图像进行数据虚拟化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28087721/
我开发了一个使用 mspn 服务的 windows phone 应用程序。它在 Windows 手机模拟器上运行良好。现在我想在 Windows Phone 设备中测试它。我有一个 Windows P
我正在考虑针对 Windows Phone 7 进行开发。为了测试我的应用程序,我需要能够模拟电话调用。在模拟器中可以吗? 最佳答案 不,不可能模拟您的应用程序被接收到的电话中断。还宣布为 the R
我看到 Windows Phone 8 支持 Windows Phone 7 应用程序。我在 Windows Phone 7 上开发了一些应用程序,我想在 Windows Phone 8 模拟器上运行
我正在Windows 8上使用Visual Studio2013。我有三个Windows Phone设备,lumia 620、920、1320,这些都是开发人员解锁的设备。 620设备已安装Windo
非常直接的问题。我的公司开发了一个 windows phone 7 应用程序,并一直在 windows phone 7 设备上对其进行测试。我们能否安全地假设同一个应用程序将向后兼容并在 Window
标题几乎概括了它。我已经编写了一个 Windows Phone 7 应用程序,现在我想将它部署到 Windows Phone 8 设备 (HTC Windows Phone 8X)。我已经注册并解锁了
我一直在开发 Windows Phone 8 应用程序,现在我想将其更改为 Windows Phone 8.1。我该怎么做? 是否应该创建一个新的 Windows Phone 8.1 应用并将所有现有
我有一个针对 Windows Phone 8 的应用程序。现在我想使用 this compression library其中指出: Supported Platforms: .NET Framewor
有什么区别Windows Phone 7.1 (NoDo) 和 Windows Phone 7.5 (芒果) ? 我已经为 Mango 安装了 SDK,但是当我尝试创建一个项目时,它显示为 7.1 版
我最近更新了我的 Visual Studio 2013,现在它允许开发 Windows Phone 8.1 应用程序。 但是现在,当我创建一个 Windows Phone 项目时,它是针对 WP 8.
我最近购买了 Lumia 630 双 SIM 卡,并一直在尝试注册我的手机以进行开发,但一直收到此错误“无法连接到手机。请确保通过 USB 传输服务的 Windows Phone IP 正在运行”。
我正在尝试使用 documentation from MSDN 在我现有的 Windows Phone OS 7.1 应用程序中支持新的 Windows Phone 磁贴功能。 .但是,我似乎无法通过
我在搜索如何启动Windows Phone 10模拟器时迷路了。 我已经做了: 我下载并安装了仿真器镜像(我知道flash.vhd文件的位置) 我正在运行Windows 8.1 x64专业版。 Hyp
我想制作像图片一样的动态磁贴。我该怎么做? 最佳答案 上面创建的图 block 很可能是在服务器端创建的。据说 Windows Phone 7.1 和 Windows Phone 8 之间发生了很多变
我刚刚将它安装在我的 Windows 8 上。它不是在 VM 上运行,而是在带有 bootcamp 的 macbook 上运行。 当我尝试模拟我的应用程序时,出现第一个错误: 但是后来我单击“重试”并
Microsoft 刚刚发布了结合了 Windows 8.1 和 Windows Phone 8.1 的 Windows App 8.1,因此您可以创建一个通用的应用程序。但是,将 Microsoft
是否有可用于WP7的日历控件(不是日期选择器和时间选择器)? 最佳答案 我还在CodePlex上启动了新项目。我刚刚研究了日历控件的Alpha版本。 http://wpcontrols.codeple
我在XAML中有一个文本框: .cs文件声明了以下事件: private void blah_OnKeyDown(object sender, KeyEventArgs e) {
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我正在为Windows Phone 7创建一个多人游戏。如何调试模拟器的多个实例以便对其进行调试? 最佳答案 实际上,您可以同时运行Windows Phone 7模拟器的多个实例,甚至可以同时调试它们
我是一名优秀的程序员,十分优秀!