ShellTile添加StandardTileData

时间:2014-01-29 10:36:55

标签: c# .net wpf windows-phone-8

我想添加StandardTileData来自此路径中设备的图片:

C:/Data/Users/DefApps/AppData/{4A3C6293-E51F-4B26-944A-88A2C3D028BE}/Local/Images/EHkozMIXZ8w.png

有了这个:

try
{
    string imageUrl = @"C:/Data/Users/DefApps/AppData/{4A3C6293-E51F-4B26-944A-88A2C3D028BE}/Local/Images/EHkozMIXZ8w.png";
    StandardTileData tileData = new StandardTileData();
    tileData.Title = videoItem.Name;
    tileData.BackBackgroundImage = new Uri(imageUrl);

    Uri mp = new Uri("/MainPage.xaml?" + "videoID=" + videoItem.idStr, UriKind.Relative);

    ShellTile.Create(mp, tileData, false);
}
catch { }

我得到了这个执行:

file:///C:/Data/Users/DefApps/AppData/{4A3C6293-E51F-4B26-944A-88A2C3D028BE}/Local/Images/EHkozMIXZ8w.png

我知道为什么会收到此错误?我做错了什么?

编辑:

我试过这段代码:

            string fileName = Path.GetFileName(videoItem.ImgUrl);

            var sri = Application.GetResourceStream(new Uri(videoItem.ImgUrl, UriKind.Relative));
            var data = sri.Stream;
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();

            using (IsolatedStorageFileStream stream = storage.CreateFile("Shared\\ShellContent\\" + fileName))
            {
                data.CopyTo(stream);
            }

            StandardTileData tileData = new StandardTileData();
            tileData.Title = videoItem.Name;

            if (storage.FileExists("Shared\\ShellContent\\" + fileName))
            {
                tileData.BackgroundImage = new Uri(fileName, UriKind.RelativeOrAbsolute);
            }

            Uri mp = new Uri("/MainPage.xaml?" + "videoID=" + videoItem.idStr, UriKind.Relative);

            ShellTile.Create(mp, tileData, true);

调试器输入FileExists方法,代码无例外地工作。但我看不到瓷砖上的图像。

知道为什么吗?

解决方案:

问题在于IsolatedStorageFileStreamdata需要关闭:

stream.Close();
data.Close();

2 个答案:

答案 0 :(得分:1)

如果您使用的是IsolatedStorage中的图像,则必须将其保存在Shared / ShellContent /

MSDN信息:
If the URI references an image that was stored in isolated storage, then the image must be in the Shared\ShellContent folder. For more information, see Data for Windows Phone.

将您的图片复制到Shared / ShellContent并尝试这样:

StandardTileData tileData = new StandardTileData();
tileData.Title = videoItem.Name;
tileData.BackgroundImage = new Uri("/Shared/ShellContent/EHkozMIXZ8w.png", UriKind.RelativeOrAbsolute));
Uri mp = new Uri("/MainPage.xaml?" + "videoID=" + videoItem.idStr, UriKind.Relative);
ShellTile.Create(mp, tileData);

答案 1 :(得分:0)

好像您正在尝试使用随应用程序分发的图像(AppData)您需要使用appdata:架构。如果是这种情况,您需要更改此路径:

appdata:EHkozMIXZ8w.png

如果您尝试在IsolatedStorage中使用图像,则需要使用isostore:架构并将图像存储在Shared \ ShellContent文件夹中。

您可以查看有关在appdata文件夹中使用图片的here

您可以查看here有关在隔离的存储文件夹中使用图像的信息。