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:
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!
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?
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.
After creating the project, I added a reference to MapWinGIS.
I opened the toolbox, right clicked on Choose Items. Then from the COM components tab, I made sure that Map Control is checked.
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.
I then dragged the map control to the user control surface.
For layout property, I selected Dock -> Fill
For the name property, I followed what the Microsoft doc suggested, so I named it MapCotntrolLib
I then build the project
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
Create a new WPF app (net framework) project
Add a reference to axinterop.mapwingis.dll. You have to browse to the debug folder of your mapcontol project.
I added a reference to System.Windows.Forms and WindowsFormsIntegration
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.
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.
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 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.
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.
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.
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?
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