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);
}
}
}
}
Solved! Go to Solution.
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);
});
}
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);
});
}
Thank you Uma,
I got this code to work with a Project Item.