2014-03-13 76 views
0

我發現從一個Windows應用商店的應用程序的資源顯示圖像的許多樣品,並得到它的樣本中顯示的圖像,但我會要求flipview顯示目錄中的圖片,或者至少顯示圖像文件名我提供通過代碼。迄今爲止我嘗試過的所有內容都是空白的。我可能失去了一些東西很明顯,這是XAML的相關部分:如何在flipview中顯示目錄中的圖像文件?

<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> 

這個工作,但它需要我來添加圖像資源第一....

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); 

我想錯過什麼?

回答

0

在此期間,我發現自己的答案,我現在增加對未來的讀者。上述示例不起作用,因爲Windows 8應用程序不允許訪問大多數PC的目錄,而用戶沒有使用FolderPicker選擇一個。該程序可以在以後重新使用該目錄中:

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

我已經改變了上面的XAML瀏覽:下面

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

任務會顯示在Flipview在圖片庫中的所有.jpg文件,如果在Package.appxmanifest Capabilities中,「圖片庫」已被選中:

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; 
} 
相關問題