我使用以下代码获取自定义用户控件,从中制作位图,然后将其保存到独立存储以用于 WP8 动态磁贴。
public static void UpdateTile()
{
var frontTile = new LiveTileRegular(); // Custom Control
frontTile.Measure(new Size(173, 173));
frontTile.Arrange(new Rect(0, 0, 173, 173));
var bmp = new WriteableBitmap(173, 173);
bmp.Render(frontTile, null);
bmp.Invalidate();
const string filename = "/LiveTiles/LiveTileRegular.jpg";
using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!isf.DirectoryExists("/LiveTiles"))
{
isf.CreateDirectory("/LiveTiles");
}
using (var stream = isf.OpenFile(filename, FileMode.OpenOrCreate))
{
bmp.SaveJpeg(stream, 173, 173, 0, 100);
}
Debug.WriteLine("Image Exists: " + (isf.FileExists(filename) ? "Yes" : "No")); // Displays "Yes"
}
ShellTile.ActiveTiles.First().Update(new FlipTileData
{
Title = "Title",
BackgroundImage = new Uri("isostore:" + filename, UriKind.Absolute),
}); // Throws a NotSupportedException
}
NotSupportedException
在 ShellTile.ActiveTiles.First().Update()
方法上被抛出,消息非常非描述性。
有什么我明显做错的地方吗?
最佳答案
“TargetInnvocationException”异常实际上隐藏了“NotSupportedException”异常的潜在问题,这是我在将 ShellTile.ActiveTiles.First().Update()
移出 UI 线程后发现的。
异常仍然没有描述问题是什么,但在翻阅不同的论坛和文档后,我发现动态创建的图像的路径在与 Live Tiles 一起使用时非常重要。
如果您将独立存储中的图像用于动态磁贴或外壳磁贴,则基础文件夹必须是
/Shared/ShellContent
改变之后
const string filename = "/LiveTiles/LiveTileRegular.jpg";
到
const string filename = "/Shared/ShellContent/LiveTileRegular.jpg";
一切正常。
我们的 Windows Phone 开发人员能否获得更好的异常消息传递?!? :)
关于c# - 使用来自控件的动态图像为 WP8 创建动态磁贴会引发 "NotSupportedException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15579351/