# Getting MapWinGIS to work in WPF

**URL:** <https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664>\
**Category:** MapWinGIS\
**Created:** [July 23, 2020, 6:30pm UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664 "2020-07-23T18:30:57Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZeroByter](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@ZeroByter](https://mapwindow.discourse.group/u/ZeroByter)\
**Post date:** [July 23, 2020, 6:30pm UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/1 "2020-07-23T18:30:57Z")

</div>

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](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](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/603515a08868d9eb82bbedea6c049e44fc3711b6.png)

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!

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [July 23, 2020, 8:41pm UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/2 "2020-07-23T20:41:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ZeroByter](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@ZeroByter](https://mapwindow.discourse.group/u/ZeroByter)\
**Post date:** [July 23, 2020, 9:42pm UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/3 "2020-07-23T21:42:56Z")

</div>

@jerryfaust

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

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

---

<div class="post-metadata">

**Author:** ![RaffyM](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RaffyM](https://mapwindow.discourse.group/u/RaffyM)\
**Post date:** [July 24, 2020, 12:28am UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/4 "2020-07-24T00:28:02Z")

</div>

Hi @ZeroByter I got this working on WPF. I followed the instructions mentioned in this [Microsoft documentation](https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/walkthrough-hosting-an-activex-control-in-wpf).

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

---

<div class="post-metadata">

**Author:** ![ZeroByter](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@ZeroByter](https://mapwindow.discourse.group/u/ZeroByter)\
**Post date:** [July 24, 2020, 6:37am UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/5 "2020-07-24T06:37:56Z")

</div>

Thanks for replying with your instructions! Much appreciated 🙂

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.

---

<div class="post-metadata">

**Author:** ![RaffyM](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RaffyM](https://mapwindow.discourse.group/u/RaffyM)\
**Post date:** [July 24, 2020, 11:08am UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/6 "2020-07-24T11:08:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ZeroByter](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@ZeroByter](https://mapwindow.discourse.group/u/ZeroByter)\
**Post date:** [July 24, 2020, 11:33am UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/7 "2020-07-24T11:33:23Z")

</div>

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.

 ![image](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/ad07d8b87277513489e4d2bb3648017156581bb4.png)

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.

---

<div class="post-metadata">

**Author:** ![ZeroByter](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@ZeroByter](https://mapwindow.discourse.group/u/ZeroByter)\
**Post date:** [July 24, 2020, 12:47pm UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/8 "2020-07-24T12:47:48Z")

</div>

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](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/d3b6596dc0cf25cd6ceed2d877296c05e3f58541.png)

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

---

<div class="post-metadata">

**Author:** ![RaffyM](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RaffyM](https://mapwindow.discourse.group/u/RaffyM)\
**Post date:** [July 24, 2020, 1:33pm UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/9 "2020-07-24T13:33:11Z")

</div>

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

![axinterop.mapwingis](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/8085ef22dcf9ff51a2d407df2ab58b574bd54773.jpeg)

 ![solution explorer](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/ddd767937f97f6d4de69fc3d89d6d184a579c2a9.jpeg)

---

<div class="post-metadata">

**Author:** ![RaffyM](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RaffyM](https://mapwindow.discourse.group/u/RaffyM)\
**Post date:** [July 24, 2020, 1:36pm UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/10 "2020-07-24T13:36:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RaffyM](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RaffyM](https://mapwindow.discourse.group/u/RaffyM)\
**Post date:** [July 24, 2020, 1:41pm UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/11 "2020-07-24T13:41:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ZeroByter](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@ZeroByter](https://mapwindow.discourse.group/u/ZeroByter)\
**Post date:** [July 24, 2020, 2:10pm UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/12 "2020-07-24T14:10:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ZeroByter](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@ZeroByter](https://mapwindow.discourse.group/u/ZeroByter)\
**Post date:** [July 25, 2020, 7:16am UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/13 "2020-07-25T07:16:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ZeroByter](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@ZeroByter](https://mapwindow.discourse.group/u/ZeroByter)\
**Post date:** [July 25, 2020, 10:37am UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/14 "2020-07-25T10:37:49Z")

</div>

Found the solution to the problem… [I asked for help on the issue on the MSDN forums and got a helpful clue!](https://social.msdn.microsoft.com/Forums/vstudio/en-US/117bb233-dc2e-48c8-b9fa-eb82f8404e1b/wpf-using-windowsformshost?forum=csharpgeneral)

> 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 😕 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?

---

<div class="post-metadata">

**Author:** ![RaffyM](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RaffyM](https://mapwindow.discourse.group/u/RaffyM)\
**Post date:** [July 25, 2020, 11:53am UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/15 "2020-07-25T11:53:09Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![ZeroByter](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@ZeroByter](https://mapwindow.discourse.group/u/ZeroByter)\
**Post date:** [July 25, 2020, 12:27pm UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/16 "2020-07-25T12:27:09Z")

</div>

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 😆

Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![pmeems](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/pmeems/32/9_2.png) [@pmeems](https://mapwindow.discourse.group/u/pmeems)\
**Post date:** [January 5, 2022, 3:09pm UTC](https://mapwindow.discourse.group/t/getting-mapwingis-to-work-in-wpf/664/17 "2022-01-05T15:09:05Z")

</div>


