在列表视图中从磁盘Windows Phone 8.1加载映像?

时间:2015-04-22 13:47:40

标签: c# windows-phone-8 windows-phone-8.1

我使用的listView在每个项目上都有一个图像,该图像是从磁盘中提取的,但不需要的图像不会退出。

问题发生在图像未退出时,抛出FileNotFoundException(在控制台上)需要太长时间,并冻结UI

我的列表视图是这样的

<ListView 
    Name="TestLv" 
    ItemsSource="{Binding}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Imageview Source="{Binding Converter={StaticResource ImageConverter}}"/>
        </DataTemplate>
</ListView>

我有一个转换器来在磁盘上构建url imageSource

class Image : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var itemService = value as ClassItem;

        if(someCondition){
            return .. //compose file system url
        }

        //return an static image
        return ..
    }

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

或者如果有更好的方法可以做到这一点,那就完美了我的全部耳朵。

1 个答案:

答案 0 :(得分:0)

还有其他更好的方法来实现这个,但是为了不冻结UI,你可以实现asycn这样的东西。

    private async System.Threading.Tasks.Task<Image> LoadImageAsync(string imagename)
    { 

        //Get Image from local storage...
        return yourimage
    }

和你的转换器

public object Convert(object value, Type targetType, object parameter, string language)
{
    var itemService = value as ClassItem;

    if(someCondition){
        return LoadImageAsync(imagename);
    }

    //return an static image
    return ..
}