【问题标题】:Adding Image into Bing map将图像添加到 Bing 地图中
【发布时间】:2011-12-30 16:59:19
【问题描述】:

我已尝试通过以下方式添加图像,但仍然无法正常工作。图像类型是内容。

Image image = new Image();
image.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("myimage.png", UriKind.Relative));

//Define the image display properties
image.Opacity = 1.0;
image.Stretch = Stretch.Fill;
image.Width = 40;
image.Height = 40;

// Center the image around the location specified


//Add the image to the defined map layer
phoneDetailsLayer.AddChild(image, e.Position.Location);

mapViewAll.Children.Remove(phoneDetailsLayer);
mapViewAll.Children.Add(phoneDetailsLayer);

【问题讨论】:

  • 您是收到错误消息,还是没有显示?
  • 没有错误信息,只是没有出现
  • 检查你的图片位置和路径,我在图片属性中使用了构建操作作为资源。
  • 您能否也分享一些 XAML 代码,以便我们了解您的解决方案是如何构建的?
  • 奇怪,为什么所有的反对票?不满的回答者和朋友?投票赞成平衡(因为这是一个有效的问题)。

标签: windows-phone-7 map bing-maps


【解决方案1】:

确保您的图片是正确的资源类型并以最佳方式加载(即,如果多次使用,则加载一次)。有多种方法可以为 WPF(与 WP7 相同)加载图像,如下所述:WPF image resources

这篇文章在这里:Visual Studio: How to store an image resource as an Embedded Resource? 讨论了您应该/不应该使用的不同图像资源类型。

我认为您应该同时了解这两个方面,因为它可以帮助您避免将来可能出现的问题。

【讨论】:

    【解决方案2】:

    我无法为您的问题添加评论,但是当您说内容时,我会在这里问您,您是直接将图像添加到包含您的代码的项目中还是添加到单独的内容项目中?

    假设你已经直接添加了:

    如果您已将“构建操作”设置为“资源”,那么您应该使用 GetResourceStream 方法:

    Image image = new Image();
    StreamResourceInfo resource = Application.GetResourceStream(new Uri("/myimage.png", UriKind.Relative));
    var bmp = new System.Windows.Media.Imaging.BitmapImage();
    bmp.SetSource(resource.Stream);
    image.Source = bmp;
    

    但是,如果您已将“构建操作”设置为“内容”,则应使用 GetContentStream 方法

    Image image = new Image();
    StreamResourceInfo resource = Application.GetContentStream(new Uri("/myimage.png", UriKind.Relative));
    var bmp = new System.Windows.Media.Imaging.BitmapImage();
    bmp.SetSource(resource.Stream);
    image.Source = bmp;
    

    【讨论】:

      【解决方案3】:

      只是为了澄清这个问题的答案。问题不在于资源类型,问题与相对 Uri 的工作方式有关。就像任何结构良好的项目 ericlee 在他的项目中使用不同的文件夹一样(相对于项目根目录): /pages - 包含实际页面,因此也包含包含上述代码的页面 /images - 包含必须引用的实际 PNG 图像

      在原始代码中,对“myimage.png”的引用是一个相对 uri。该应用程序现在将查看“/pages/myimage.png”,因此找不到图像。这里的技巧是使用正确的相对 URI。它可以构造如下: 1.首先使用两个点上到项目根目录 -> ..(一个用于当前目录,一个用于上一级) 2. 现在参考 /images -> ../images 3. 现在添加实际的文件引用 -> ../images/myimage.png

      如果使用正确的 URI,问题就解决了。

      【讨论】:

        【解决方案4】:

        主要问题似乎是如何获得真正的 uri。 对我来说,下表在这种情况下对我有帮助(我只有德语):

        http://msdn.microsoft.com/de-de/library/aa970069.aspx

        示例:

        // Absolute URI (default)
        Uri absoluteUri = new Uri("pack://application:,,,/File.xaml", UriKind.Absolute);
        // Relative URI
        Uri relativeUri = new Uri("/File.xaml", UriKind.Relative);
        

        示例 2:

        Uri uri = new Uri("pack://application:,,,/File.xaml");
        

        或代码隐藏:

        'Image compiling is set to "content"
        MyImage1.Source = New BitmapImage(New Uri("/Images/MyFile.png", Relative))'only example
        

        【讨论】:

          【解决方案5】:
          /projectname;component/images/menu/lost.png
          

          是正确的方法,你剩下的答案真的不行

          【讨论】:

          • 澄清一下,您的 xaml.cs 文件与图像文件有什么关系?我认为 xaml.cs 文件位于单独的文件夹中,因此需要一个相对 uri。例如,如果 xaml.cs 文件位于 /views 您的相对 uri 将是“../images/menu/lost.png”
          • 我的 xaml 在 /pages 上,而图像在 /images 上?这可能是问题吗?
          • 这不是问题,但您应该注意您的路径的后果。每一个额外的。意味着你又上升了一个层次。链接是相对的!在你的情况下是这样的。 “/images/file.png” -> “/pages/images/file.png” “./images/file.png” -> “/pages/images/file.png” “../images/file.png” " -> "/images/file.png"
          猜你喜欢
          • 1970-01-01
          • 2011-01-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-28
          • 2012-02-14
          相关资源
          最近更新 更多