Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to cast object of type 'GMap.NET.WindowsPresentation.GMapImage' to type 'GMap.NET.WindowsForms.GMapImage' #153

Open
WindingWinter opened this issue Nov 27, 2019 · 6 comments

Comments

@WindingWinter
Copy link

WindingWinter commented Nov 27, 2019

Here's the code to illustrate the problem:

   public  class GMapWPFControl: GMap.NET.WindowsPresentation.GMapControl
    {
        public GMapWPFControl()
        {
            MapProvider = GMapProviders.GoogleTerrainMap;
        }
    }


    public class GMapWinFormControl: WindowsFormsHost
    {

        private GMapControl _mapControl;

        public GMapWinFormControl()
        {
            _mapControl = new GMap.NET.WindowsForms.GMapControl();
            _mapControl.MapProvider = GMapProviders.GoogleTerrainMap;
            Child = _mapControl;
        }
    }


<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:tabcontrolgrey"
        x:Class="tabcontrolgrey.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <TabControl>
        <TabItem Header="Winform">
            <local:GMapWinFormControl />
        </TabItem>

        <TabItem Header="WPF">
         <local:GMapWPFControl />
        </TabItem>
    </TabControl>

</Window>

The problem occurs when the program is launched, and both Winform and WPF of GmapControl will be loaded, so for Tile.Overlays, it should return the correct GMapImage depending on whether it is WPF or WinForm control, however, at this point, it seems that the wrong GMapImage is loaded into the GMapControl.

An InvalidCastException is thrown because

Unable to cast object of type 'GMap.NET.WindowsPresentation.GMapImage' to type 'GMap.NET.WindowsForms.GMapImage'.

invalidcastexception

In the above code sample, the WPF version of the GMapImage is loaded into Winform GMapControl, and hence it causes exception.

Any ideas how to workaround, or any fixes for this?

@WindingWinter WindingWinter changed the title GMap.NET.WindowsForms.GMapControl and GMap.NET.WindowsPresentation.GMapControl cannot commingle together in WPF tab control Unable to cast object of type 'GMap.NET.WindowsPresentation.GMapImage' to type 'GMap.NET.WindowsForms.GMapImage' Nov 27, 2019
@radioman
Copy link
Owner

radioman commented Dec 1, 2019

Why do you use winform version in wpf project?

@WindingWinter
Copy link
Author

Why do you use winform version in wpf project?

To promote code reuse, I want to reuse some of my legacy winform components in my (new) WPF projects. So it just happens that those legacy winform contains winform greatmaps.net.

This is actually a pretty common scenario, and it's something that Microsoft supports formally via WindowsFormsHost

@radioman
Copy link
Owner

radioman commented Dec 3, 2019

Ok, but there is wpf version of gmaps https://github.com/radioman/greatmaps/tree/master/GMap.NET.WindowsPresentation

@radioman
Copy link
Owner

radioman commented Dec 3, 2019

...anyway, if you'd remove the reference of GMap.NET.WindowsPresentation and dll itself, it should work

@WindingWinter
Copy link
Author

WindingWinter commented Dec 4, 2019 via email

@iifuzz
Copy link

iifuzz commented May 4, 2020

I am also having this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants