Reputation: 4056
I am loading a BitmapImage
from IsolatedStorage
and would like to set the value to the background of MainPage. I am not sure how to properly do this?
TombstoningHelper.cs
public async Task StorePhoto(Stream photoStream, string fileName)
{
// persist data into isolated storage
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (Stream current = await file.OpenStreamForWriteAsync())
{
await photoStream.CopyToAsync(current);
}
}
public async Task<BitmapImage> RetrievePhoto(string fileName)
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
Stream imageStream = await file.OpenStreamForReadAsync();
//Check if file exists
// display the file as image
BitmapImage bi = new BitmapImage();
bi.SetSource(imageStream);
return bi;
}
MainPage.xaml.cs
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
//Set Page Theming
ImageBrush ib = new ImageBrush();
TombstoningHelper tsh = new TombstoningHelper();
if (Settings.TransparentBackground.Value == null)
ib.ImageSource = new BitmapImage(new Uri("/Assets/Graphics/" + Settings.Background.Value, UriKind.Relative)); //No Error
else
ib.ImageSource = tsh.RetrievePhoto(Constants.BackgroundImageName); //Error occurs here
LayoutRoot.Background = ib;
I am getting an error above stating Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Windows.Media.Imaging.BitmapImage>' to 'System.Windows.Media.ImageSource
.
Upvotes: 0
Views: 94
Reputation: 514
You should use await statement, like this: ib.ImageSource = await tsh.RetrievePhoto(Constants.BackgroundImageName);
Upvotes: 0
Reputation: 2060
You need to use the await keyword since your Helper's methods are asynchronous.
else
ib.ImageSource = await tsh.RetrievePhoto(Constants.BackgroundImageName);
Upvotes: 2