2016-09-18 45 views
0

我在我的ViewModel中有多個StorageFile s作爲Task<StorageFile> s,我想在它的視圖中綁定一個Image。UWP - StorageFile到ImageSource服從MVVM

不應該在ViewModel中創建BitmapImage,因爲它位於XAML名稱空間中,並且需要UI線程(我沒有這麼做)。

我該如何解決這個問題?使用ValueConverter不能這樣做,因爲打開StorageFile是異步...

PS:我不能使用URI時,StorageFile位於LocalCache文件夾...

回答

1

儘量只使用從StorageFilePath屬性:

<ListView ItemsSource="{Binding ImageItems}" 
      Grid.Row="1"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition/> 
        <RowDefinition/> 
       </Grid.RowDefinitions> 
       <TextBlock Text="{Binding Path}"/> 
       <Image Grid.Row="1"> 
        <Image.Source> 
         <BitmapImage UriSource="{Binding Path}"/> 
        </Image.Source> 
       </Image> 
      </Grid> 
     </DataTemplate> 
    </ListView.ItemTemplate> 

哪裏ImageItemspublic List<StorageFile> ImageItems財產

+0

我震驚的是,這個作品,感覺周圍的UWP沙箱黑客。我見過的所有解決方案都是在代碼中創建ImageSource,這讓XAML可以做到這一點。此外,它看起來像你可以直接將'Image.Source'設置爲綁定,不需要BitmapImage。謝謝! – ManIkWeet

+0

我從'Image.Source'擴展到'BitmapImage',所以用戶可以使用'DecodePixelWidth'屬性(內存泄漏問題) –

+0

我找不到描述內存泄漏的問題,只是圖像通常大於顯示的問題分辨率,意思是記憶被浪費了。雖然我的圖片總是低於顯示分辨率。 – ManIkWeet