这是我用来在XAML中绑定图像的代码
<Border toolkit:TiltEffect.IsTiltEnabled="true" Height="350" Width="400" Grid.ColumnSpan="3">
<Grid Height="350" Width="400" Margin="70,0,70,0" x:Name="Container1">
<Grid.Background>
<ImageBrush ImageSource="{Binding ImageCollection[0]}" Stretch="Uniform" AlignmentX="Left" AlignmentY="Center"/>
</Grid.Background>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<i:InvokeCommandAction Command="{Binding ImageTapCommand}" CommandParameter="CONTAINER0"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Grid>
</Border>同样,我使用4边框来显示我最近的图片。
在我的ViewModel中,我使用下面的方法从独立存储中读取图像。
public Stream GetFileStream(string filename, ImageLocation location)
{
try
{
lock (SyncLock)
{
if (location == ImageLocation.RecentImage)
{
filename = Constants.IsoRecentImage + @"\" + filename;
}
using (var iSf = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!iSf.FileExists(filename)) return null;
var fs = iSf.OpenFile(filename, FileMode.Open, FileAccess.Read);
return fs;
}
}
}
catch (Exception ex)
{
return null;
}
}在获得流之后,我将使用下面写的方法四来构建UI绑定的WritableBitmap
private WriteableBitmap BuildImage(Stream imageStream)
{
using (imageStream)
{
var image = new BitmapImage();
image.SetSource(imageStream);
return new WriteableBitmap(image);
}
}在这种情况下,我的问题是在导航到我的页面两三次之后。当我使用“image.SetSource(imageStream);”方法时,应用程序在BuildImage()方法上崩溃。我尝试了许多替代方案,但都失败了。我得到的异常是"System.OutOfMemoryException“
我尝试了图像控件,而不是图像画笔。
我尝试了位图,而不是WritableBitmap等,但结果是一样的。
如果我使用小图片,应用程序的崩溃率将会降低。但通过摄像头拍摄的图像崩溃率很高。
在过去的一周里,我一直在尝试解决这个问题,但没有找到任何替代方案来解决这个问题。
我找到了一个讨论类似问题的link,但没有得到很多人来解决这个问题
发布于 2013-05-06 20:01:09
在使用IsolatedStorageFileStream和IsolatedStorageFile之后,您是否正在对它们进行重置/处置?
https://stackoverflow.com/questions/16185557
复制相似问题