1

我正在尝试使用从 PhotoChooserTask 保存的图像填充辅助平铺背景,但由于某种原因,我无法完成此操作。我参考了很多网站,但我还没有找到正确的实现。我所做的只是调用 PhotoChooserTask,然后在完成的事件中,我将生成的图像保存到隔离的存储中以便稍后加载。这在我的应用程序中与 HubTile 一起使用,但由于某种原因,我无法将图像附加到辅助磁贴。到目前为止,我所拥有的如下:

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 没有图像。我将如何正确调用隔离存储路径来相应地设置辅助磁贴的 BackgroundImage?或者还有什么我应该做或改变的事情吗?

4

3 回答 3

2

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

        ...

     }
}
于 2012-08-20T16:30:41.100 回答
1

您确定图像已成功保存并且存在吗?您将其保存为 jpeg,但您引用了 png 文件。试试@"\Shared\ShellContent\shareJPEG.png"

于 2012-08-20T06:29:37.663 回答
0

首先,您应该将图像放在“\Shared\ShellContent”位置。您可以使用 .png 或 .jpg 文件

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);

    if (Tile == null)  
    {        
        string filePath = System.IO.Path.Combine(imageFolder, shareJPEG);
        using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (iso.FileExists(filePath)) // check file exist or not
                        background = new Uri(@"isostore:" + filePath, UriKind.Absolute); 
                } 
        ...

     }
}
于 2015-08-11T09:40:16.140 回答