简体   繁体   English

如何从IsolatedStorage设置背景图像

[英]How to Set Background Image from IsolatedStorage

I am loading a BitmapImage from IsolatedStorage and would like to set the value to the background of MainPage. 我正在从IsolatedStorage加载BitmapImage ,并想将值设置为MainPage的背景。 I am not sure how to properly do this? 我不确定如何正确执行此操作?

TombstoningHelper.cs 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 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 . 我在上面指出Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Windows.Media.Imaging.BitmapImage>' to 'System.Windows.Media.ImageSource时出现错误。

You need to use the await keyword since your Helper's methods are asynchronous. 您需要使用await关键字,因为Helper的方法是异步的。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM