如何从隔离存储中加载图像以用于辅助图块 [英] How to load image from isolated storage for secondary tile

查看:88
本文介绍了如何从隔离存储中加载图像以用于辅助图块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用从PhotoChooserTask中保存的图像填充辅助平铺背景,但是由于某些原因,我无法完成此操作.我已经引用了很多站点,但是没有找到正确的实现方式.我要做的只是调用PhotoChooserTask,然后在完成的事件中将生成的图像保存到隔离的存储中,以供以后加载.这已在我的应用程序中与HubTile一起使用,但是由于某些原因,我无法将图像附加到辅助图块.到目前为止,我所拥有的如下:

I am attempting to populate a secondary tile background with an image saved from the PhotoChooserTask, but for some reason I cannot accomplish this. I have referenced a lot of sites but I have not found the proper implementation. All I do is call PhotoChooserTask, and then on the completed event I save the resulting image to isolated storage to be loaded later. This has worked with a HubTile in my application, but for some reason I cannot append the image to a secondary tile. So far what I have is as follows:

MainPage.xaml.cs

MainPage.xaml.cs

string imageFolder = @"\Shared\ShellContent";
string shareJPEG = "shareImage.jpg";

public MainPage()
    {
        InitializeComponent();

        photoChooserTask = new PhotoChooserTask();
        photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
    }

public void changePictureMenuItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {     
        try
        {
            photoChooserTask.Show();
        }
        catch (System.InvalidOperationException)
        {
            MessageBox.Show("An error occurred");
        }
    }

    void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
              //persist the data in isolated storage
                    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if(!myIsolatedStorage.DirectoryExists(imageFolder))
                        {
                            myIsolatedStorage.CreateDirectory(imageFolder);
                        }

                        if (myIsolatedStorage.FileExists(shareJPEG))
                        {
                            myIsolatedStorage.DeleteFile(shareJPEG);
                        }

                        string filePath = System.IO.Path.Combine(imageFolder, shareJPEG);
                        IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(filePath);

                        BitmapImage bitmap = new BitmapImage();
                        bitmap.SetSource(e.ChosenPhoto);
                        WriteableBitmap wb = new WriteableBitmap(bitmap);

                        // Encode WriteableBitmap object to a JPEG stream. 
                        Extensions.SaveJpeg(wb, fileStream, 173, 173, 0, 100);

                        fileStream.Close();
                    }
        }
    }    

private void CreateLiveTile(TileItem item)
    {
        //IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
        var title = item.Title.ToString();
        string tileParameter = "Param=" + item.Title.ToString();

        ShellTile Tile = CheckIfTileExist(tileParameter);  // Check if Tile's title has been used             

        if (Tile == null)
        {   
            //this is not working?             
            background = new Uri(@"isostore:/Shared/ShellContent/shareJPEG.png", UriKind.Absolute);
            //background = new Uri("isostore:/Shared/ShellContent/shareJPEG.png", UriKind.Absolute);

            try
            {
                var LiveTile = new StandardTileData
                {
                    Title = item.TileName,                        
                    BackgroundImage = background,  //not working
                    BackTitle = item.TileName,    
                    BackBackgroundImage = new Uri("/background.png", UriKind.Relative),
                    BackContent = item.Message,
                };

                ShellTile.Create(new Uri("/MainPage.xaml?" + tileParameter, UriKind.Relative), LiveTile);                      
        }
    }

最终,将创建辅助磁贴,但是BackgroundImage没有图像.我如何正确地调用隔离的strorage路径来相应地设置辅助图块的BackgroundImage?还是我应该做些其他事情或更改?

Ultimately, the secondary tile is created but there is no image for the BackgroundImage. How would I properly call the isolated strorage path to set the BackgroundImage of the secondary tile accordingly? Or is there something else I should be doing or change?

推荐答案

MainPage.xaml.cs

MainPage.xaml.cs

string imageFolder = @"\Shared\ShellContent"; 
string shareJPEG = "shareImage.jpg"; 

...

private void CreateLiveTile(TileItem item)  
{   
    var title = item.Title.ToString();  
    string tileParameter = "Param=" + item.Title.ToString();  

    ShellTile Tile = CheckIfTileExist(tileParameter);  // Check if Tile's title has been used               

    if (Tile == null)  
    {        
        string filePath = System.IO.Path.Combine(imageFolder, shareJPEG);                
        background = new Uri(@"isostore" + filePath, UriKind.Absolute);    //this worked

        ...

     }
}

这篇关于如何从隔离存储中加载图像以用于辅助图块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆