在Windows 8应用程序中显示动态图片库?

时间:2013-01-07 17:43:05

标签: c# windows xaml windows-8 windows-applications

我正在开发一个简单的Windows 8应用程序,作为更复杂的概念的准确证据。

目前,我有一个文本框和两个按钮。一个按钮允许您选择一个文件夹(在我的文本框中填充文件夹路径)。

另一个按钮应返回当前所选文件夹中前5个图像的绝对路径,但我在使用Win8应用程序中的FileOpenPicker时遇到一些问题。

我希望做的是,当点击此按钮时,我不想返回图像的前5个路径,而是希望以这样的网格格式显示它们,除了延伸到右边,以扩展向下更像传统网站。

Screenshot

到目前为止我所拥有的:

XAML:

<StackPanel Grid.Row="2" Margin="120,0,0,0">
    <StackPanel Orientation="Horizontal" Margin="0,20,0,20">
        <TextBox  x:Name="pictureInput" HorizontalAlignment="Left" Grid.Row="2" TextWrapping="Wrap" Text="Select Image..." VerticalAlignment="Top" Width="300" Height="41" FontSize="24"/>
        <Button Content="Browse" HorizontalAlignment="Left" Grid.Row="1" VerticalAlignment="Top" Height="41" Width="147" Click="Browse_Folder_Click"/>
        <TextBlock x:Name="ImgThumbHere" Grid.Column="2" Width="540"/>
        <Button Content="Find Images" HorizontalAlignment="Left" VerticalAlignment="Top" Height="90"  Width="250" Click="Find_Images"/>
    </StackPanel>
</StackPanel>

Xaml.CS:

//method to select folder : 
     private async void Browse_Folder_Click(object sender, RoutedEventArgs e)
        {
            string folderPath = "";
            FolderPicker folderPicker = new Windows.Storage.Pickers.FolderPicker();
            // Create the picker object and set options
            folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
            // Users expect to have a filtered view of their folders depending on the scenario.
            // For example, when choosing a documents folder, restrict the filetypes to documents for your application.
            folderPicker.FileTypeFilter.Add("*");

            StorageFolder folder = await folderPicker.PickSingleFolderAsync();
            if (folder != null)
            {
                if (folder.Path != "" && folder.Path != null)
                {
                    folderPath = folder.Path;
                }
                else
                {
                    folderPath = folder.Name;
                }
            }
            else
            {
                throw new Exception("Folder path null.");
            }
            folderInput.Text = folderPath;
        }

//attempt at method to select first 5 images of selected folder : 

    private async void Find_Images(object sender, RoutedEventArgs e)
        {
            string[] picturePath;
            FileOpenPicker picPicker = new Windows.Storage.Pickers.FileOpenPicker();
            picPicker.ViewMode = PickerViewMode.Thumbnail;
            // Create the picker object and set options
            picPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
            // Users expect to have a filtered view of their folders depending on the scenario.
            // For example, when choosing a documents folder, restrict the filetypes to documents for your application.
            picPicker.FileTypeFilter.Add(".png");
            picPicker.FileTypeFilter.Add(".jpg");
            picPicker.FileTypeFilter.Add(".jpeg");
            picPicker.FileTypeFilter.Add(".gif");
            picPicker.FileTypeFilter.Add(".bmp");

            for (int i = 0; i < 5; i++)
            {
                StorageFile file = picPicker.//What can be called here to return paths ?
                if (file != null)
                {
                    picturePath[i] = file.path;
                }
                else
                {
                    throw new Exception("File path null.");
                }
            }
        }

任何人都可以帮我解决这个问题吗?

非常感谢。

1 个答案:

答案 0 :(得分:1)

查看http://msdn.microsoft.com/en-us/library/windows/apps/xaml/Hh758319(v=win.10).aspx

根据设计,你只能限于:

  • 访问本地存储
  • 访问少数众所周知的存储位置
  • 访问特定授权的位置