Open an existing map

177
2
Jump to solution
11-01-2019 09:26 AM
EmmanuelJohn
New Contributor II

I'm trying to open a map located in a project folder, however when i try using this code i get Exception Unhandled  error 

System.NullReferenceException: 'Object reference not set to an instance of an object.' mapProjectItem was null.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Extensions;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;

namespace AddData
{
internal class Button4 : Button
{
protected override void OnClick()
{
{
var mapProjectItems = Project.Current.GetItems<MapProjectItem>();
var mapProjectItem = mapProjectItems.FirstOrDefault(mpi => mpi.Name.Equals("Map1"));
Map map = mapProjectItem.GetMap();
ProApp.Panes.CreateMapPaneAsync(map);
}
}
}
}
0 Kudos
1 Solution

Accepted Solutions
UmaHarano
Esri Regular Contributor

Hi

It would be a good idea to check the map project item exists to be safe. Also, you will have to wrap this code within the context of a QueuedTask.Run.  Like this:

protected override void OnClick()
{
QueuedTask.Run( () => {
var mapProjectItems = Project.Current.GetItems<MapProjectItem>();
var mapProjectItem = mapProjectItems.FirstOrDefault(mpi => mpi.Name.Equals("Map1"));
Map map = mapProjectItem?.GetMap(); //Check if mapProjectItem exists.
ProApp.Panes.CreateMapPaneAsync(map);
});

}

View solution in original post

2 Replies
UmaHarano
Esri Regular Contributor

Hi

It would be a good idea to check the map project item exists to be safe. Also, you will have to wrap this code within the context of a QueuedTask.Run.  Like this:

protected override void OnClick()
{
QueuedTask.Run( () => {
var mapProjectItems = Project.Current.GetItems<MapProjectItem>();
var mapProjectItem = mapProjectItems.FirstOrDefault(mpi => mpi.Name.Equals("Map1"));
Map map = mapProjectItem?.GetMap(); //Check if mapProjectItem exists.
ProApp.Panes.CreateMapPaneAsync(map);
});

}

View solution in original post

EmmanuelJohn
New Contributor II

Thank you Uma,

   I got this code to work with a Project Item.

0 Kudos