I am migrating my Windows Phone 8 ImageConverter to Windows 10 but I am facing issue while using Storage APIs as it uses async and await. Can someone suggest what can be used to achieve my requirement?
My Windows Phone 8 image converter class is below
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
string imageUrl = value.ToString();
if (imageUrl.Contains("NoImageIcon"))
return value;
if (imageUrl.Contains(Constants.IMAGES_FOLDER_PATH))
{
BitmapImage image = new BitmapImage();
image.CreateOptions = BitmapCreateOptions.BackgroundCreation;
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (!myIsolatedStorage.FileExists(imageUrl)) return null;
using (myIsolatedStorage)
{
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(imageUrl, FileMode.Open, FileAccess.Read))
{
image.SetSource(fileStream);
}
}
return image;
}
if (imageUrl.Contains("mp4"))
{
BitmapImage image = new BitmapImage(new Uri("/Images/video.png", UriKind.Relative));
image.CreateOptions = BitmapCreateOptions.BackgroundCreation;
return image;
}
if (MCSManager.Instance.isInternetConnectionAvailable)
return value;
else
{
BitmapImage image = new BitmapImage(new Uri("/Images/defaultImage.png", UriKind.Relative));
image.CreateOptions = BitmapCreateOptions.BackgroundCreation;
return image;
}
}
return new Uri("/Images/defaultImage.png", UriKind.Relative);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
Windows 10 Code
public class ImageConverter:IValueConverter
{
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
public object Convert(object value, Type targetType, object parameter, string culture)
{
if (value != null)
{
string imageUrl = value.ToString();
if (imageUrl.Contains("NoImageIcon"))
return value;
if (imageUrl.Contains(Constants.IMAGES_FOLDER_PATH))
{
BitmapImage image = new BitmapImage();
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
System.Threading.Tasks.Task.Factory.StartNew(async()=>
{
StorageFile imagefile = await localFolder.GetFileAsync(imageUrl);
using (IRandomAccessStream fileStream=await imagefile.OpenAsync(FileAccessMode.ReadWrite))
{
image.SetSource(fileStream);
return image;
}
});
}
if (imageUrl.Contains("mp4"))
{
BitmapImage image = new BitmapImage(new Uri("ms-appx:///Images/video.png", UriKind.RelativeOrAbsolute));
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
return image;
}
if (MCSManager.Instance.isInternetConnectionAvailable)
return value;
else
{
BitmapImage image = new BitmapImage(new Uri("ms-appx:///Images/defaultImage.png", UriKind.RelativeOrAbsolute));
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
return image;
}
}
return new Uri("ms-appx:///Images/defaultImage.png", UriKind.RelativeOrAbsolute);
}
public object ConvertBack(object value, Type targetType, object parameter, string culture)
{
return null;
}
private async Task<bool> FileExists(string fileName)
{
try
{
StorageFile file =await localFolder.GetFileAsync(fileName);
return true;
}
catch(FileNotFoundException ex)
{
return false;
}
}
}
I want to use this converter in my ListView DataTemplate. My DataTemplate is
<DataTemplate x:Key="NEWReportEvidenceListItemTemplate">
<UserControl>
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0"/>
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="DFImage.Width" Value="70"/>
<Setter Target="DFImage.Height" Value="70"/>
</VisualState.Setters>
</VisualState>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="720"/>
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="DFImage.Width" Value="108"/>
<Setter Target="DFImage.Height" Value="108"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Image DataContext="{Binding EvidencePath,Converter={StaticResource ImageConverter}}"
Source="{Binding Result}"
x:Name="DFImage"
Margin="4,0,0,0"
Stretch="Uniform"/>
</Grid>
</UserControl>
</DataTemplate>