如何在flipview中的目录中显示图像文件?

编程入门 行业动态 更新时间:2024-10-23 18:24:01
本文介绍了如何在flipview中的目录中显示图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我发现许多样本在Windows应用商店应用中显示来自资源的图像,并让它在样本中显示图像,但我要求flipview在目录中显示图像,或者至少显示图像文件名I按代码提供。到目前为止,我尝试的所有内容都是空的。我可能遗漏了一些明显的东西,这是XAML的相关部分:

I found many samples of displaying images from a resource in a Windows Store app and got it to display images within a sample, but I would require the flipview to show images in a directory, or at least to show image file names I provide by code. With everything I tried so far the flipview remains empty. I maybe missing something obvious, this is the relevant part of the XAML:

<FlipView x:Name="flipView1" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="809,350,9,7" Width="548" Height="411" > <FlipView.ItemTemplate> <DataTemplate> <Image Source="{Binding Path=Image }" Stretch="Uniform"/> </DataTemplate> </FlipView.ItemTemplate> </FlipView>

这有效,但它要求我先将图像添加到资源中....

this works, but it requires me to add the images a resource first....

ImageBrush brush1 = new ImageBrush(); brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/P1000171.jpg")); FlipViewItem flipvw1 = new FlipViewItem(); flipvw1.Background = brush1; flipView1.Items.Add(flipvw1);

但是(例如)这不是:

string name = String.Format(@"c:\temp\P1000171.JPG"); Uri uri = new Uri(name); BitmapImage img = new BitmapImage(uri); flipView1.Items.Add(img);

我错过了什么?

推荐答案

与此同时,我自己找到了答案,我现在为未来的读者添加了答案。上面的示例不起作用,因为如果没有用户使用FolderPicker选择一个,则不允许Windows 8应用程序访问大多数PC的目录。该程序可以在以后重新使用该目录:

In the meantime I've found the answer myself which I now add for future readers. The above example won't work because a Windows 8 app isn't allowed to access most of the PC's directories without the user having selected one using a FolderPicker. The program can re-use that directory later with:

StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);

我在这里更改了上述XAML:

I've changed the above XAML here:

<Image Source="{Binding}" Stretch="UniformToFill"/>

下面的任务将显示Flipview中图片库中的所有.JPG文件,如果在Package.appxmanifest,Capabilities,图片库已经过检查:

The Task below will show all .JPG files in the Pictures library in a Flipview, if, in the Package.appxmanifest, Capabilities, "Pictures library" has been checked:

public async Task flipviewload() { IReadOnlyList<StorageFile> fileList = await picturesFolder.GetFilesAsync(); var images = new List<BitmapImage>(); if (fileList != null) { foreach (StorageFile file in fileList) { string cExt = file.FileType; if (cExt.ToUpper()==".JPG") { Windows.Storage.Streams.IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); using (Windows.Storage.Streams.IRandomAccessStream filestream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapImage bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync(fileStream); images.Add(bitmapImage); } } } } flpView.ItemsSource = images; }

更多推荐

如何在flipview中的目录中显示图像文件?

本文发布于:2023-11-02 08:22:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551880.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像文件   如何在   目录中   flipview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!