2014-09-13 295 views
0

因此,我在我的音樂播放器(通用應用程序)內製作了一個「微小」文件資源管理器頁面,我需要放置一張圖像來通知它是否是目錄或文件。但代碼不起作用。 這是轉換器本身:命名空間myApp在它自己的命名空間之前。FileAttributes ImageSource:IValueConverter not working

namespace Converters 
{ 
    public sealed class AttributesToImageConverter : Windows.UI.Xaml.Data.IValueConverter 
    { 
     public object Convert (object value, Type targetType, object parameter, string language) 
     { 
      FileAttributes f = (FileAttributes)value; 
      Windows.UI.Xaml.Media.Imaging.BitmapImage img = new Windows.UI.Xaml.Media.Imaging.BitmapImage (); 
      img.DecodePixelWidth = 50; 
      if (f == FileAttributes.Directory) 
      { 
       img.UriSource = new Uri ("ms-appx:/Asstes/folder.png", UriKind.Absolute); 
      } 
      else 
       img.UriSource = new Uri ("ms-appx:/Asstes/file.png", UriKind.Absolute); 
      return img; 
     } 

     public object ConvertBack (object value, Type targetType, object parameter, string language) 
     { 
      throw new NotImplementedException (); 
     } 
    } 
} 

這是XAML:

<Page 
    ... 
    xmlns:converter="using:myApp.Converters" > 

    <Page.Resources> 
     <converter:AttributesToImageConverter x:Key="AttributesToImageConverter" /> 
    </Page.Resources> 

    ... 
    <Grid x:Name="LayoutRoot" DataContext=""> 
    ... 
     <ListView x:Name="ContentRoot" ItemsSource="{Binding List}" Height="500" Margin="10,-10,10,15" Background="Transparent" BorderBrush="Transparent" > 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" Margin="2,2,2,2"> 
         <Image Width="50" Height="50" Margin="5,0,5,0" Source="{Binding Attributes, Converter={StaticResource AttributesToImageConverter}}" /> 
         <TextBlock Text="{Binding Name}" Foreground="White" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    ... 
    </Grid> 

其他綁定到這個環境中工作,完美的作品,這其中並沒有在同一IStorageItem結合Name屬性。此外,使用ListView會導致應用程序在顯示加載的數據後幾秒鐘內關閉,而沒有任何調試信息或異常,但代碼爲-2147483645(0x80000003)。我會很感激任何幫助。

+1

你需要返回圖像源不是一個圖像 – 2014-09-13 14:41:35

回答

0

「屬性」是ItemsSource「List」中每個項目的實際屬性還是視圖模型中的獨立屬性?

創建您列出的文件路徑的存儲文件,然後利用下面的例子:

var imageFile = args.Files[0] as StorageFile; 

// Ensure the stream is disposed once the image is loaded 
using (IRandomAccessStream fileStream = await imageFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) 
{ 
    // Set the image source to the selected bitmap 
    var bitmapImage = new BitmapImage(); 

    await bitmapImage.SetSourceAsync(fileStream); 
    return bitmapImage; 
}