我想添加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
方法,代码无例外地工作。但我看不到瓷砖上的图像。
知道为什么吗?
解决方案:
问题在于IsolatedStorageFileStream
和data
需要关闭:
stream.Close();
data.Close();
答案 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)