2014-02-26 50 views
0

由于我使用我自己的LongListSelector让用户从图像中进行选择,因此我需要检索Medialibrary中所有图像的URI。我找不到任何可能做到这一点呢。WP8获取MediaLibrary中图像的URI

所有我看到什么是可能的是获得图像的名称:

MediaLibrary m = new MediaLibrary(); 

foreach (var r in m.Pictures) 
{ 
    Stream imageStream = r.GetImage(); 
} 

我如何获取路径的休息吗?

编辑

继第一个解决方案:

Gallery.xaml

<phone:LongListSelector 
       x:Name="GalleryLLS" 
       LayoutMode="Grid" 
       GridCellSize="108,108" 
       SelectionChanged="GalleryLLS_SelectionChanged" 
       Margin="0,0,144,12" 
       ItemsSource="{Binding ListOfImages}" > 
       <phone:LongListSelector.ItemTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <Image Width="150" Height="150" 
           Source="{Binding}"/> 
         </StackPanel> 
        </DataTemplate> 
       </phone:LongListSelector.ItemTemplate> 
      </phone:LongListSelector> 

Gallery.xaml.cs

private List<WriteableBitmap> _listOfImages = new List<WriteableBitmap>(); 

    public List<WriteableBitmap> ListOfImages 
    { 
     get { return _listOfImages; } 

     set { _listOfImages = value; } 
    } 

    public Gallery() 
    { 
     InitializeComponent(); 

     var ml = new MediaLibrary(); 
     var Pictures = ml.Pictures; 
     foreach (var item in Pictures) 
     { 
      ListOfImages.Add(PictureDecoder.DecodeJpeg(item.GetImage())); 
     } 

    } 

结果不舒g图像。调试器显示imageas已正确添加到我的列表中,但我什么都看不到。

回答

2

看起来API并未公开从Media Library返回的图片的URI属性。所以你需要用不同的方法来做到这一点。例如,你可以有URI中的WritableBitmap秒的列表,而不是名单:

private List<WriteableBitmap> _listOfImages = new List<WriteableBitmap>(); 
public List<WriteableBitmap> ListOfImages 
{ 
    get { return _listOfImages; } 

    set { _listOfImages = value; } 
} 
....... 
....... 
var ml = new MediaLibrary();    
var Pictures = ml.Pictures; 
foreach (var item in Pictures) 
{ 
    ListOfImages.Add(PictureDecoder.DecodeJpeg(item.GetImage())); 
} 
........ 
//in XAML 
<phone:LongListSelector ItemsSource="{Binding ListOfImages}"> 
    <phone:LongListSelector.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <Image Width="150" Height="150" 
           Source="{Binding}"/> 
      </StackPanel> 
     </DataTemplate> 
    </phone:LongListSelector.ItemTemplate> 
........ 
</phone:LongListSelector> 
........ 

[改编自http://www.neelesh-vishwakarma.com代码]

+0

看来解码工作正常,但没有在我的列表中显示。我会根据你的回答更新我的问题 – 4ndro1d

+0

看来UI没有更新为ListOfImages属性填充。为了解决这个问题,可以:1.将'List '改成'ObservableCollection '或者2.在'foreach'后面加上'InitializeComponent();'。 – har07

+0

试了两个(一起以及),没有结果 – 4ndro1d

2

你为什么需要这条路?显示图像?如果你只需要在清单中显示为缩略图图像,你可以使用流来创建一个BitmapImage

var bi = new BitmapImage(); 
bi.SetSource(r.GetThumbnail()); 

现在,您可以设置biImage.Source

+0

是的,我想显示在我的LongListSelector。看到我编辑的问题。因为我想让用户在另一个页面中编辑图片,我首先想要将路径作为字符串传递。但我想我将不得不将图像保存到隔离存储 – 4ndro1d

+0

在您的视图模型中,您可以同时保留“BitmapImage”和图像路径。然后将该路径传递给编辑页面。另外,不要使用'GetImage',而要使用'GetThumbail'而不是http://msdn.microsoft.com/en-us/library/ff434150.aspx。 –