Getting MapWinGIS to work in WPF

Hey guys! So I got MapWinGIS to work in WinForms, looks pretty great!

Now, I know that in WPF there is a ‘WindowsFormsHost’ and so I tried using that for hosting the MapWinGIS ActiveX control, but when I try to debug the program I get: https://imgur.com/a/39tXl0i

Notice how I run make sure to run the application in 32 bit mode, just like I successfully do in the WinForms project.

These are my references:
image

And the code I am running is:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        System.Windows.Forms.Integration.WindowsFormsHost host = new
        System.Windows.Forms.Integration.WindowsFormsHost();

        var map = new AxMapWinGIS.AxMap();

        host.Child = map;

        this.grid.Children.Add(host);
    }
}

Any help or advice on this issue would be much appreciated.
Thanks!

Hello @ZeroByter, and welcome.

There have been a few posts recently regarding WPF. I have no personal knowledge regarding success within WPF. Perhaps @RaffyM or @ada have had some success?

Regards,
Jerry.

@jerryfaust

Hey! Thanks for taking the time to go through my post :slight_smile:

Hopefully someone has the answer, would really like to get this to work.

Hi @ZeroByter I got this working on WPF. I followed the instructions mentioned in this Microsoft documentation.

What happened was I had to create 2 projects. By the way, I used the 4.9.6.1 version of the control.

The first project was a Windows Forms Control Library (Net Framework). These were the steps I did.

  1. After creating the project, I added a reference to MapWinGIS.
  2. I opened the toolbox, right clicked on Choose Items. Then from the COM components tab, I made sure that Map Control is checked.
  3. Somehow I could not locate the map control in the toolbox but it is there. I used search in the toolbox and typed in map control.
  4. I then dragged the map control to the user control surface.
  5. For layout property, I selected Dock -> Fill
  6. For the name property, I followed what the Microsoft doc suggested, so I named it MapCotntrolLib
  7. I then build the project
  8. If you look at the Debug folder of the project you should see some dlls. You will use these dlls in your second project.

The next steps will be done in the second project

  1. Create a new WPF app (net framework) project
  2. Add a reference to axinterop.mapwingis.dll. You have to browse to the debug folder of your mapcontol project.
  3. I added a reference to System.Windows.Forms and WindowsFormsIntegration
  4. Then proceeded as what is discussed in the Microsoft doc which is similar to your code example.

The first time I tried to make a WPF mapping project using MapWinGIS, I couldn’t make it work so I also asked for help in this community. I soon realized that you have first make a user control project then use that user control in WPF app.

Hope this helps.
Raffy

1 Like

Thanks for replying with your instructions! Much appreciated :slight_smile:

It appears to me as if I accidentally have already done your instructions the first time around, before creating this post.

I redid all your steps and got the same result.

This is very strange, like I said I got the WinForms project to work, but then the WPF version says ‘The specified module could not be found’ despite it being there.

Should I downgrade my MapWinGIS version to see if that fixes it? I followed all your instructions and it still doesn’t work. Very weird.

FWIW, I am using VS studio community edition 2019. In an earlier attempt, I was able to use the latest version of mapwingis (5.2.2) in WPF.

Also in the same computer, I have a winforms application that I developed 2 years ago.

Take a look at the bin folders for the files that are supposed to be there. There were times that I got an error about something missing, and so I just copied the missing dlls.

In my computer, these are the files present:
for the MapControl user control
AxInterop.MapWinGIS.dll
Interop.MapWinGIS.dll
MapControlLib.dll - this will depend on what you named your user control project
MapControlLib.pdb

For the WPF app
AxInterop.MapWinGIS.dll
Interop.MapWinGIS.dll
stdole.dll
WpfAppMapping.exe - this depends on what name you gave your project
WpfAppMapping.exe.config
WpfAppMapping.pdb

I have almost the same files as you describe.

I didn’t have stdole.dll before, so I added that to no avail.
Also I do not have the .config file in the WPF bin folder but I don’t think that is the issue here.
Aside from those two exceptions, I have all the same DLLs.

It’s also kinda frustrating when the error message doesn’t mention what module is missing. All it says is that System.Windows.Forms.dll threw the exception, but that’s not very helpful.

Can you perhaps share your properties for the two DLLs in your WPF project? Specifically, what is their Embed Interop Types and Copy Local properties or anything else you think could be useful… I’m completely puzzled.

Update, so I added more DLLs to the bin folder and it still doesn’t find the ‘missing module’, which ever module that is…

image

but I can feel we are getting closer to the solution!

I am showing you the property of axinterop.mapwingis and a screenshot of my solution explorer for the wpf app.

axinterop.mapwingis

I forgot, the reason why there are a lot of items in my references list is that because I did not remove unused using statements in the header section of my .cs files. I will try to cleanup my files to remove all unused using statements and see what happens

Ok. no effect. After removing all unused using statements, the number of items in my references are still the same.

In your first post, there is a screenshot showing your references. Try removing Interop.MapWinGIS because I remember getting an error when I referenced that dll. If you look at the screenshot of my references, it does not incude it.

I removed it, no difference.

It would be great if we could learn what module is missing.

I tried comparing the references between my working WinForms versrion and my WPF version and including what references are missing but that didn’t help either.

Edit: So I just ran Dependency Walker on the exe and it didn’t find anything useful.

A few comments ago, you mentioned when there is a DLL missing, you just add it to the bin folder.

I would love to do that as well, but I do not know which DLL is missing to begin with. How did you know which DLL to add when you had a missing DLL?

Found the solution to the problem… I asked for help on the issue on the MSDN forums and got a helpful clue!

Thanks for your comment! You helped solve my issue.

I knew that both projects had to be the same CPU configuration, obviously, however my mistake was when I selected at the top toolbar ‘x86’ I thought it would also apply to the current project (both my WinForms and WPF projects were in the same solution) but it didn’t, instead it only applied to the WinForms one while the WPF one was still in ‘Any CPU’ (despite the toolbar doing a bit of misleading) and thus the exception crash.

I made sure this time that both projects were using the same configuration via the configuration manager and now it works nicely, thanks!

Edit: @RaffyM I have encountered a new problem :confused: In my WPF project I need to reference MapWinGIS in order to set various map properties, such as map.Tiles.ProviderId and Map.Tiles.Providers and other stuff, and the interface Tiles is defined in MapWinGIS, not in AxMapWinGIS.

Like you said, when I try to reference Interop.MapWinGIS the program crashes. How to get around this?

Hi @ZeroByter Glad you solved that pesky problem. I got the reference for MapWInGIS from the COM tab of the reference window and not from selecting it via Browse. If you select it via Browse, in my computer, I get a System.IO.FileNotFoundException: 'Could not load file or assembly 'Interop.MapWinGIS, Version=4.9.0.0

Yay! After some hiccups such as the map not displaying any tiles and some other minor obstacles, it works perfectly!

I hope this thread will be of great help to anyone in the future who decides to go down the adventure of setting up MapWinGIS in WPF :laughing:

Thanks! :slight_smile:

1 Like