radioman / greatmaps

GMap.NET - Great Maps for Windows Forms & Presentation
908 stars 409 forks source link

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

Open WindingWinter opened 4 years ago

WindingWinter commented 4 years ago

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?

radioman commented 4 years ago

Why do you use winform version in wpf project?

WindingWinter commented 4 years ago

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 commented 4 years ago

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

radioman commented 4 years ago

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

WindingWinter commented 4 years ago

My use case is that I need to use both the WPF and winform version in my application. For old work I have no choice but to use the winform component, but for new work I want to use Wpf as the main app is WPF .

But it seems that WPF and Winform greatmaps.net has a problem coexist. Thinking of fixing it ?

On Tue, 3 Dec 2019 at 9:28 PM, radioman notifications@github.com wrote:

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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/radioman/greatmaps/issues/153?email_source=notifications&email_token=ADENIZRMC7WI4GWN4MNXKZLQWZNJJA5CNFSM4JSA2KB2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFZL2UQ#issuecomment-561167698, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADENIZSLDBLEMXKZPFMB2CDQWZNJJANCNFSM4JSA2KBQ .

iifuzz commented 4 years ago

I am also having this problem.