MapWindow / MapWindow5

MW5 is a desktop GIS which is extendable using the plug-in architecture. It is using the MapWinGIS mapping control and is created from scratch in 2015 using new technologies like MEF and Dependency Injection making it small in size, robust and fast.
288 stars 100 forks source link

Exception when calling IAppContext from CustomVectorTool in template plugin project #124

Open TongZhai opened 3 years ago

TongZhai commented 3 years ago

This 'Run' code in MW5.Plugins.TemplatePlugin.Tools.Vector.CustomVectorTool gives exception:

        public override bool Run(ITaskHandle task)
        {
            Log.Info("Custom tool is running");
            var numLyrs = _context.Map.Layers.Count;
            Log.Info($"There are {numLyrs} layers on the map.");
            return true;
        }

The log/exception message at runtime is as below:

Custom tool is running
Error during tool execution: Custom tool from Template plugin

Exception: Stack trace: 
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at MW5.Tools.Model.GisTask.<>c__DisplayClass43_0.<RunAsync>b__1(Task`1 task)

Inner exception: Object does not match target type.
Stack trace: 
   at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   at MapWinGIS._DMap.get_NumLayers()
   at MW5.Plugins.TemplatePlugin.Tools.Vector.CustomVectorTool.Run(ITaskHandle task)
   at MW5.Tools.Model.GisTask.Run(CancellationToken cancellationToken)
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()

This code worked on my development machine (meaning all libraries are installed and ocx are registered etc), it fails on barebone testing machine (Windows 10). More specifically, I compiled the plugin on dev machine, then place it (the .dll file) under my installed MapWindow5\Plugins\ folder (also on dev machine) and it works, on our barebone testing machine (Windows 10), we also installed the same version of MapWindow5 (MapWindow-v5.6.3.0-x64.exe), but when I copied this same .dll onto our barebone testing machine's MapWindow5\Plugins\ folder, it gave above exception. Both dev machine and testing machine are Windows 10 at .Net framework 4.8.03752

I see the _context is protected, but the CustomVectorTool inherits from GisTool, so it should be accessible.

mathijs-dumon commented 3 years ago

I'm not 100% sure about this but most of the GIS tools in MW5 do not interact with the map. Instead they operate on shapefiles 'outside' of the GUI thread. If you want to interact with the OCX control you MUST invoke methods on the main thread. There is a helper method SafeInvoke do this in MW5.Shared:

        (_context.Map as ISynchronizeInvoke).SafeInvoke(() => { 
             // Do whatever on _context.Map here
        });

I'm not sure what you're trying to do, but probably you don't want to do it this way.

Also we have a forum (https://mapwindow.discourse.group/c/mw-plugin-developers/) where you are more likely to get appropriate help, the github issues are only meant for bug reports.

TongZhai commented 3 years ago

@mathijs-dumon ok, I will start visiting the developer forum for advice then, thank! Ok, I am a bit surprised by the gis tool not interacting with map display, not directly that is. So, I am building this hydrologic themed application that would download data, then dynamically reproject, clip etc the gis data and display on the map, also some post-process statistics are to be displayed by manipulating the map display, these would be done through custom plug-ins, so do you think these are feasible in MW5 environment? thanks!