如何在flipview中的目录中显示图像文件? [英] How to show image files from a directory in a flipview?

查看:136
本文介绍了如何在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中的目录中显示图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆