【问题标题】:Windows Phone 8: How can I render an invisible Map to a bitmap?Windows Phone 8:如何将不可见的地图渲染为位图?
【发布时间】:2026-02-02 03:35:01
【问题描述】:

这是我目前所拥有的:

const int imgSize = 210;
Map tempMap = new Map { Width = imgSize, Height = imgSize };
// ... adding some layers, setting the view
Size size = new Size(imgSize, imgSize);
tempMap.Measure(size);
tempMap.Arrange(new Rect(new Point(0, 0), size));
tempMap.UpdateLayout();

WriteableBitmap bmp = new WriteableBitmap(imgSize, imgSize);
bmp.Render(tempMap, null);
bmp.Invalidate();
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
string filename = "/Shared/ShellContent/" + /* ... file name */ ".jpg";
using (IsolatedStorageFileStream stream = store.OpenFile(filename, FileMode.OpenOrCreate))
    bmp.SaveJpeg(stream, imgSize, imgSize, 0, 100);

tile.Update(new FlipTileData
{
    // ... set other properties
    BackgroundImage = new Uri("isostore:" + filename, UriKind.Absolute)
});

我想用地图图像更新动态磁贴(来自诺基亚的 WP8)。根据this answer,即使是从不属于可视化树的 UIElements 也可以使用 WriteableBitmap 进行渲染;但是,我的图像只是黑色的。这是地图控件的问题,还是我做错了什么?

编辑 1:Windows Phone WriteableBitmap 类似乎与the one documented on MSDN 完全不同。很奇怪。

编辑 2:Documentation for the Silverlight WriteableBitmap

编辑 3:部分进展! 添加以下三行:

tempMap.Width = imgSize // right before the measure call. DO NOT set the height.
(Content as Grid).Children.Add(tempMap) // right after creating tempMap. (your layout may vary)
(Content as Grid).Children.Remove(tempMap) // after you're done. (your layout may vary)

这会产生完全蓝色的图像,而不是完全黑色的图像。更改 tempMap.Center 和 tempMap.ZoomLevel 对图块没有影响;调用 tempMap.SetView 根本没有效果(为了调试,我将地图留在了我的应用页面中,这样我就可以看到调用的效果 - 磁贴不是什么地图在页面中的样子)

【问题讨论】:

    标签: windows-phone-8 writeablebitmap here-api


    【解决方案1】:

    我自己尝试过,也无法正常工作。不过,我只是使用 Bing 的静态地图 API 解决了这个问题。然后我可以下载一张地图,指定为我想要的确切大小,然后使用它。以下链接显示了如何使用它,并提供了获取 API 密钥的链接(只需一秒钟)。

    http://bingmapsdemos.sharepoint.com/Pages/BingMapsStaticMaps.aspx

    【讨论】: