【问题标题】:FileAttributes to ImageSource: IValueConverter not workingImageSource 的 FileAttributes:IValueConverter 不起作用
【发布时间】: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


【解决方案1】:

“属性”是 ItemsSource“列表”中每个项目的实际属性,还是视图模型中的单独属性?

使用您列出的文件路径创建一个存储文件,然后利用以下示例:

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

【讨论】:

    猜你喜欢
    • 2014-04-08
    • 2021-07-25
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 2019-10-10
    • 1970-01-01
    相关资源
    最近更新 更多