【问题标题】:UWP MapControl Cannot add a MapIcon when building an app package that targets an older OSUWP MapControl 在构建面向旧操作系统的应用程序包时无法添加 MapIcon
【发布时间】:2018-08-14 12:20:24
【问题描述】:

我正在尝试将 MapIcon 添加到我的 Bing 地图控件。当我在 Visual Studio 的调试环境中运行应用程序时,我没有任何问题。然而,一旦我构建了我的应用程序包并运行它,添加一个地图图标会使应用程序崩溃并抛出一个异常,内容如下:

“无法将 Windows.UI.Xaml.Controls.Maps.MapIcon 类型的对象转换为 Windows.UI.Xaml.Controls.Maps.IMapElement4 类型”

这里是实例化 MapIcon 的简单代码。重要的是要知道在调试模式下添加这些地图图标没有任何问题。该问题仅在构建并运行应用程序包后才存在。在发布的应用程序运行时,我能够使用 try/catch 来查明异常以显示异常。如果有人可以帮助我摆脱这个异常,或者有任何解决方法的建议,将不胜感激

 MapIcon messageicon2_2 = new MapIcon
                    {
                        Location = message_position,
                        NormalizedAnchorPoint = new Point(0.5, 1.0),
                        ZIndex = 0,
                        Title = "msg " + count + "(2.2)",
                        IsEnabled = true,                            
                        CollisionBehaviorDesired = MapElementCollisionBehavior.RemainVisible
                    };

                    MyWaypoints.Add(messageicon2_2);
                    messageLayer.MapElements = MyWaypoints;

[编辑澄清]:我知道这个错误专门来自 Mapicon 的实例化。此代码在 Windows 10 17134 机器上发布并运行时运行良好......我需要它运行的机器是 Windows 10 16299。这台机器无法更新到较新版本的 windows。我需要一种在旧版 Windows 上显示这些地图图标的方法。

【问题讨论】:

  • 哪行代码抛出异常?
  • 地图图标的实例化:MapIcon messageicon2_2 = new MapIcon
  • 这可能看起来很傻,但试试“var messageicon2_2 = new MapIcon”
  • 那行不通
  • 您是否尝试过使用自定义图像添加 MapIcon,请在 Windows 10 16299 中测试 this 并告知结果。

标签: c# exception uwp bing-maps uwp-maps


【解决方案1】:

异常指向 IsEnabled 属性,该属性已添加到 Windows 操作系统版本 1803 (17134) 的 MapElement。见MapElement.IsEnabled Property

我知道这个例外不是最有帮助的,但也许还有其他一些关于它的警告被遗漏了?

对于此类错误,您可以删除缺少的 API 的所有用途和/或确保编写可以在最低操作系统版本上运行的version adaptive code,同时还可以利用仅在某些操作系统版本。

具体来说,您可以执行以下操作:

        var messageicon2_2= new MapIcon
        {
            Location = message_position,
            NormalizedAnchorPoint = new Point(0.5, 1.0),
            ZIndex = 0,
            Title = "msg " + count + "(2.2)",
            CollisionBehaviorDesired = MapElementCollisionBehavior.RemainVisible
        };

        if (ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Controls.Maps.MapElement", "IsEnabled"))
        {
            messageicon2_2.IsEnabled = true;
        }

        MyWaypoints.Add(messageicon2_2);
        messageLayer.MapElements = MyWaypoints;

【讨论】:

    猜你喜欢
    • 2021-09-08
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 2019-06-02
    • 2015-08-11
    相关资源
    最近更新 更多