UWP MapControl Cannot add a MapIcon when building an app package that targets an older OS

172 Views Asked by At

I am trying to add a MapIcon to my Bing maps control. When I am running the application in my debugging environment on visual studio I don't have any issues. However once I build my app package and run it, adding a map Icon crashes the application and throws this an exception that reads:

"Unable to cast object of type Windows.UI.Xaml.Controls.Maps.MapIcon to type Windows.UI.Xaml.Controls.Maps.IMapElement4"

Here is the simple code that instantiates the MapIcon. It is important to know that I do not have any issues adding these map Icons in debug mode. The problem only exists once the app package has been built and run. I was able to pinpoint the exception using a try/catch to display the exception while the released app is running. If anyone can help me to get rid of this exception, or have any advice for a work around, it would be much appreciated

 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;

[EDIT To Clarify]: I know that this bug is coming specifically from the instantiation of the Mapicon. This code works perfectly fine when it is released and run on a Windows 10 17134 machine.... The machine that I need it to run is Windows 10 16299. This machine can not be updated to a newer version of windows. I am in need of a way to display these map icons on this older release of windows.

1

There are 1 best solutions below

0
On

The exception points to the IsEnabled property which was added to MapElement for Windows OS Version 1803 (17134). See MapElement.IsEnabled Property.

I know the exception is not the most helpful, but maybe there was some other warning about it that got missed?

For errors like this, you can either remove all uses of the missing API and/or make sure to write version adaptive code that can run on the lowest OS version, while also taking advantage of the selected features that are available only on certain OS versions.

Specifically you can do something like:

        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;