我在选择图像后出现了一个简单的图片库问题,我将它发送到Windows 10中的另一个屏幕,它工作得很完美但是当我在手机中尝试时,它没有任何理由崩溃,我的代码是以下之一:
XAML代码:
<Page
x:Class="Stop_Diabetes.Pages.Camera"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Stop_Diabetes.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="#227EC2" Loaded="Page_Loaded">
<Page.Resources>
<local:ImageConverter x:Key="imageConverter"/>
<DataTemplate x:Key="imageTemplate">
<Grid Width="190" Height="130">
<Image Source="{Binding Path=Thumbnail,
Converter={StaticResource imageConverter}}" Tapped="Image_Tapped"
Width="200" Height="200"/>
</Grid>
</DataTemplate>
<CollectionViewSource
x:Name="picturesSource"/>
</Page.Resources>
<Grid>
<Grid x:Name="stkGallery" Canvas.ZIndex="15" VerticalAlignment="Bottom">
<GridView x:Name="gvPictures" VerticalAlignment="Top" Height="150" ItemsSource="{Binding Source={StaticResource picturesSource}}" ItemTemplate="{StaticResource imageTemplate}" ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.HorizontalScrollBarVisibility="Visible">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Vertical"></WrapGrid>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
</Grid>
</Grid>
</Page>
C#重要代码:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
List<string> fileTypeFilter = new List<string>();
fileTypeFilter.Add(".jpg");
fileTypeFilter.Add(".jpeg");
fileTypeFilter.Add(".png");
fileTypeFilter.Add(".gif");
fileTypeFilter.Add(".bmp");
//Define thr query to iterate thriugh all the subfolders
var pictureQueryOptions = new QueryOptions(CommonFileQuery.OrderByDate, fileTypeFilter);
//Read through all the subfolders.
pictureQueryOptions.FolderDepth = FolderDepth.Deep;
//Apply the query on the PicturesLibrary
var pictureQuery = KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(pictureQueryOptions);
//
var picturesInformation = new FileInformationFactory(pictureQuery, ThumbnailMode.PicturesView);
picturesSource.Source = picturesInformation.GetVirtualizedFilesVector();
}
private async void Image_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
Image img = (Image)sender;
FileInformation fi = img.DataContext as FileInformation;
if (fi != null)
{
string path = fi.Name;
//do something with the path...
var pStorage = KnownFolders.PicturesLibrary;
StorageFile file = await pStorage.GetFileAsync(path);
ImageProperties imgProp = await file.Properties.GetImagePropertiesAsync();
using (var imgStream = await file.OpenAsync(FileAccessMode.Read))
{
WriteableBitmap bitmap = new WriteableBitmap((int)imgProp.Width, (int)imgProp.Height);
bitmap.SetSource(imgStream);
Frame.Navigate(typeof(Crop), bitmap);
}
}
}
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string culture)
{
if (value != null)
{
var img = (IRandomAccessStream)value;
var picture = new BitmapImage();
picture.SetSource(img);
return picture;
}
return DependencyProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType, object parameter, string culture)
{
throw new NotImplementedException();
}
}
我的问题发生在这里:
StorageFile file = await pStorage.GetFileAsync(path);
在PC中完美打开图像,但是当我在手机中时它说:
消息=“系统找不到指定的文件。\ r \ n”
这很奇怪,因为图片已加载,但以后无法打开,我尝试了不同的选项,如ID,路径等,没有人工作。有谁知道我应该在Windows 10 Mobile中更改什么?
答案 0 :(得分:2)
fileinfo类有一个openAsync和OpenReadAsync方法,为什么不使用它们呢?
https://msdn.microsoft.com/en-us/library/windows/apps/br207562
以下是您应该更改的代码
using (var imgStream = await fi.OpenReadAsync())
答案 1 :(得分:0)
在appxmanifest中启用了“图片库”功能吗?