在 WP8.1 中如何获取手机图片库并显示在ListBox上 MVVM结构

第一你得知道怎么获取到手机内部的图片库

第二你要自己设计一个UI显示方式

第三通过你的ViewModel实现两者之间的联系

-----------------------begin------------------------

定义UI 如果想实现系统内部的的相册一样的效果 就是用GridView 

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" FontSize="50">请选择图片:</TextBlock>
        <ListBox Height="580" Grid.Row="1" x:Name="listbox" ItemsSource="{Binding TripPictureCol}" ScrollViewer.ZoomMode="Enabled">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate Typography.StylisticSet15="True">
                    <VirtualizingStackPanel></VirtualizingStackPanel>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate VirtualizingStackPanel.VirtualizationMode="Recycling">
                    <StackPanel Orientation="Horizontal">
                        <Image  Width="100" Height="100" Source="{Binding TirpPictureSource}">
                            <!--<Image.Source>
                                <BitmapImage UriSource="{Binding TirpPictureSource}" />
                            </Image.Source>-->
                        </Image>
                        <TextBlock Text="{Binding textBlock}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

现在获取图片库,在显示到UI上

但是在我实际做的情况当中发现不能直接把显示手机内部中的图片,因为权限的问题吧

所以我这里就把手机内部的图片复制到了我的临时文件夹当中,在显示在UI上

 1 try
 2             {
 3                 //定义存储位置
 4                 StorageFolder folder = ApplicationData.Current.TemporaryFolder;
 5                 StorageFolder DaoruFolder = await folder.CreateFolderAsync("Images",CreationCollisionOption.ReplaceExisting);
 6                 //获取图片库
 7                 StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
 8                 IReadOnlyList<StorageFolder> folders = await picturesFolder.GetFoldersAsync();
 9                 foreach(var i in folders)
10                 {
11                     if (i.Name == "Camera Roll" || i.Name == "Saved Pictures" || i.Name == "Screenshots")
12                     {
13                         IReadOnlyList<StorageFile> file = await i.GetFilesAsync();
14                         if (file.Count <= 0)
15                             continue;
16                         foreach (var s in file)
17                         {
18                             //StorageFile newfile = await DaoruFolder.CreateFileAsync(s.Name, CreationCollisionOption.GenerateUniqueName);        
19                             await s.CopyAsync(DaoruFolder, s.Name, NameCollisionOption.GenerateUniqueName);
20                         }
21                     }
22                 }
23                 
24             }
25             catch(Exception e)
26             {
27                 throw new Exception();
28             }
29             finally
30             {
31      //执行完成后,把图片导到UI上
32                 LoadImageAddress();
33             }

在把图片导出到UI上

 1 //读取临时文件夹的里面的图片
 2             StorageFolder temporayFolder = ApplicationData.Current.TemporaryFolder;
 3             StorageFolder myFolder = await temporayFolder.GetFolderAsync("Images");
 4             IReadOnlyList<StorageFile> myList = await myFolder.GetFilesAsync();
 5             foreach(var i in myList)
 6             {
 7                 Imagelist.Add(new Model()
 8                     {
 9                         10                         TripPictureUrl = i.Path
11                     });
12             }

posted @ 2015-04-14 16:08  PGQ  阅读(62)  评论(0)    收藏  举报