GMap.NET - placing GMapControl in UserControl and then UserControl on Form yields MissingMethodException

StackOverflow https://stackoverflow.com/questions/9793018

  •  25-05-2021
  •  | 
  •  

質問

I am trying to build a USerControl that contains a GMapControl. When I place the GMapControl directly on the Form, then it works as expected. If I however place the GMapControl on a UserControl, and then add that UserControl to the Form, I get errors.

For example:

My UserControl, Map.cs:

public Map()
        {
            InitializeComponent();

            gMapControl1.MapProvider = GMapProviders.OpenStreetMap;
            gMapControl1.Position = new PointLatLng(54.6961334816182, 25.2985095977783);
            gMapControl1.MinZoom = 1;
            gMapControl1.MaxZoom = 24;
            gMapControl1.Zoom = 9;

            top = new GMapOverlay("1");
            objects = new GMapOverlay("objects");
            routes = new GMapOverlay("routes");
            polygons = new GMapOverlay("polygons");

            gMapControl1.Overlays.Add(routes);
            gMapControl1.Overlays.Add(polygons);
            gMapControl1.Overlays.Add(objects);
            gMapControl1.Overlays.Add(top);

            gMapControl1.OnMarkerClick += new MarkerClick(gMapControl1_OnMarkerClick);
            gMapControl1.OnPolygonClick += new PolygonClick(gMapControl1_OnPolygonClick);
        }

Then I add this UserControl to my Form by dragging it on there. Then I get an Exception:

Failed to create component 'Map'. The error message follows: 'System.MissingMethodException: Method not found: 'Void GMap.NET.WindowsForms.GMapControl.set_MapProvider(GMap.NET, MapProviders.GMapProvider)'. at OpenStreetMapTest.Map..ctor()'

If I have the same code that I have in the UserControl Map inside a Form, then no errors. Also, the set_MapProvider exists and works if I don't put the GMapControl inside a UserControl.

Any ideas?

役に立ちましたか?

解決

Decompile the code and see what the Map construtor is doing. Maybe it's locating some method by reflection. Can't think why else you'd get a MissingMethodException dependong on where the control is sitting.

On DesignMode suggestion, that property just flat out doesn't work for nested user controls which is really frustrating. However, you can use the following work around (this property would be in a UserControlBase class from which you would inherit)

Simply check IsDesignerHosted instead of IsDesignMode.

        /// <summary>
        /// Indicates if the code is being run in the context of the designer
        /// </summary>
        /// <remarks>
        /// <see cref="Component.DesignMode"/> always returns false for nested controls. This is one
        /// of the suggested work arounds here: http://stackoverflow.com/questions/34664/designmode-with-controls
        /// </remarks>
        public bool IsDesignerHosted
        {
            get
            {
                Control ctrl = this;

                while (ctrl != null)
                {
                    if ((ctrl.Site != null) && ctrl.Site.DesignMode)
                        return true;
                    ctrl = ctrl.Parent;
                }
                return false;
            }
        }

他のヒント

you should wrap everything inside the if ( !DesignMode )

eg.

Map()
{
    InitializeComponent();

    if ( !DesignMode )
    {
        gMapControl1.MapProvider = GMapProviders.OpenStreetMap;
        gMapControl1.Position = new PointLatLng(54.6961334816182, 25.2985095977783);
        gMapControl1.MinZoom = 1;
        gMapControl1.MaxZoom = 24;
        gMapControl1.Zoom = 9;

        top = new GMapOverlay("1");
        objects = new GMapOverlay("objects");
        routes = new GMapOverlay("routes");
        polygons = new GMapOverlay("polygons");

        gMapControl1.Overlays.Add(routes);
        gMapControl1.Overlays.Add(polygons);
        gMapControl1.Overlays.Add(objects);
        gMapControl1.Overlays.Add(top);

        gMapControl1.OnMarkerClick += new MarkerClick(gMapControl1_OnMarkerClick);
        gMapControl1.OnPolygonClick += new PolygonClick(gMapControl1_OnPolygonClick);
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top