【问题标题】:Set Secondary Tile BackgroundImage from Image in Isolated Storage从独立存储中的图像设置辅助平铺背景图像
【发布时间】:2013-11-24 04:43:03
【问题描述】:

这就是我从图像 url 获取流的方式:

        using (var httpClient = new HttpClient())
        {
            response = await httpClient.GetStreamAsync(new Uri(IMAGEURL_HERE, UriKind.Absolute));
        }

        SaveImage(response);

这就是我将它保存到 IsoloatedStorage 的方式:

    private void SaveImage(Stream result)
    {
        using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
        {
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(result);
            var wb = new WriteableBitmap(bitmap);

            using (IsolatedStorageFileStream fileStream = file.CreateFile("FILENAME.jpg"))
            {
                int width = wb.PixelWidth;
                int height = wb.PixelHeight;
                if (wb.PixelWidth > 336)
                {
                    width = 336;
                }
                if (wb.PixelHeight > 336)
                {
                    height = 336;
                }
                Extensions.SaveJpeg(wb, fileStream, width, height, 0, 100);
            }
        }
    }

假设文件是​​ FILENAME.jpg,我想我可以将它设置为 BackgroundImage 到这样的辅助磁贴:

var tileData = new FlipTileData()
{
...
BackgroundImage = new Uri("isostore:/Shared/ShellContent/FILENAME.jpg", UriKind.Absolute),
...

这行不通。它不会抛出异常,只有图像不会显示。我想念什么?当然,如果我将 Image Url 作为 Uri 放到 BackgroundImage 中,它可以工作,但这不是我想要的。

编辑:我在这里看到了类似的问题,但它对我的代码没有帮助。

【问题讨论】:

  • tileData.BackgroundImage = ...
  • 我已经编辑了代码。在那里,它是 FlipTileData ctor,因此创建 Tile 不是问题,它工作得很好,也适用于来自站点的图像 URL,但不是来自 IsoStorage。

标签: c# windows-phone-8


【解决方案1】:

试试这个。可能是它的帮助。

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

private void SaveImage(Stream result)
{
    using (IsolatedStorageFile file = 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(result);
        WriteableBitmap wb = new WriteableBitmap(bitmap);

        // Encode WriteableBitmap object to a JPEG stream. 
        int width = wb.PixelWidth;
        int height = wb.PixelHeight;
        if (wb.PixelWidth > 336)
        {
            width = 336;
        }
        if (wb.PixelHeight > 336)
        {
            height = 336;
        }
        Extensions.SaveJpeg(wb, fileStream, width, height, 0, 100);

        fileStream.Close();


    }
} 

private void CreateTile()
{
    var tileData = new FlipTileData()
    {
         ....
         string filePath = System.IO.Path.Combine(imageFolder, shareJPEG);                
         BackgroundImage = new Uri(@"isostore:" + filePath, UriKind.Absolute);
         ....
    } 
}

【讨论】:

  • FlipTileData() 抛出 System.UriFormatException。在创建 Tile 之前,我已经使用 FileExists 检查了路径,文件就在那里。有什么想法吗?
  • 检查我更新的答案。现在它对我来说非常完美。 uri 中缺少 :
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多