如何从IsolatedStorage设置背景图像

时间:2014-06-25 11:51:27

标签: c# windows-phone-8 application-data

我正在从BitmapImage加载IsolatedStorage,并希望将值设置为MainPage的背景。我不确定如何正确地做到这一点?

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;

我收到的错误高于Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Windows.Media.Imaging.BitmapImage>' to 'System.Windows.Media.ImageSource

2 个答案:

答案 0 :(得分:2)

您需要使用await关键字,因为Helper的方法是异步的。

else
    ib.ImageSource = await tsh.RetrievePhoto(Constants.BackgroundImageName);

答案 1 :(得分:0)

您应该使用await语句,如下所示: ib.ImageSource = await tsh.RetrievePhoto(Constants.BackgroundImageName);