2014-09-13 98 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; 
}