【发布时间】:2014-09-15 02:45:13
【问题描述】:
所以,我在我的音乐播放器(通用应用程序)中创建了一个“小”文件浏览器页面,我需要放置一个图像来告知是目录还是文件。但是代码不起作用。 这是转换器本身:namespace myApp 在它自己的命名空间之前。
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)。如有任何帮助,我将不胜感激。
【问题讨论】:
-
你需要返回图片源而不是图片
标签: c# windows-runtime windows-phone-8.1 win-universal-app