An item with the same key has already been added

965
2
08-06-2019 04:49 AM
KiroAndreev1
New Contributor II

Hello,

I am using 100.5 version of Esri RunTime Sdk for WPF/C#.

I would like to add Graphic objects into GraphicsOverlay, but i don't like to initialize Graphic class into loop.I initialize  Graphic class outside loop and appear next error: 

An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at Esri.ArcGISRuntime.Internal.AttributeDictionary.Add(String key, Object value)

How to solved this problem, because i have a lot of Graphics and i like to work my application fast.

Best regard.

0 Kudos
2 Replies
RaviDhulipudi
Esri Contributor

Hi Kiro, Refer the attached code for a sample example and see if it works. Also, post your code so that we could try to resolve it.

0 Kudos
KiroAndreev1
New Contributor II

Dear Ravi,

My code is :

public void ReadBuildingsGML()
{
try
{
String graphGraphBuild = null;
string tempPath = Path.Combine(Path.GetTempPath(), "Temp");
//Set Folder permission
FullPermissions fullPermissions = new FullPermissions(tempPath);
fullPermissions.SetFullAccessPermissionsForEveryone();
XmlDocument docGrap = new XmlDocument();
Dictionary<String, Object> graphAttrBuild = new Dictionary<string, Object>();
if (File.Exists(tempPath + @"\" + graphKO + ".gml"))
{
XmlDocument doc = new XmlDocument();
doc.Load(tempPath + @"\" + graphKO + ".gml");
var nsManager = new XmlNamespaceManager(new NameTable());
//register mapping of prefix to namespace uri
nsManager.AddNamespace("gml", "http://www.opengis.net/gml");
XmlNodeList xnList = doc.SelectNodes("//gml:featureMember", nsManager);
for (int i = 0; i < xnList.Count; i++)
{
XmlNode nodeChild = xnList.FirstChild;

for (int j = 0; j < nodeChild.ChildNodes.Count; j++)
{
string[] firstSubStr = nodeChild.ChildNodes.Name.Split(delimRead);
if (firstSubStr[0] == "akn" || firstSubStr[0] == "arec")
{
int index = nodeChild.ChildNodes.Name.IndexOf(":") + 1;
string name = nodeChild.ChildNodes.Name.Substring(index);
if (name != "GEOMETRY")
{
if (name == "BUILDNO")
{
GetSetBuildNumber.Number = nodeChild.ChildNodes.InnerText;
}
if (name == "PARCNO")
{
GetSetParcNumber.Number = nodeChild.ChildNodes.InnerText;
}
if (name == "MAIN_PARCEL")
{
GetSetMainParc.MainParc = nodeChild.ChildNodes.InnerText;
}
graphAttrBuild.Add(name, nodeChild.ChildNodes.InnerText);
}
else
{
graphGraphBuild = "<xml>" + nodeChild.ChildNodes.InnerXml + "</xml>";
}
}
}
docGrap.LoadXml(graphGraphBuild);
XmlNodeList polygonList = docGrap.SelectNodes("//gml:Polygon", nsManager);
XmlNodeList outerBoundaryIs = docGrap.SelectNodes("//gml:outerBoundaryIs", nsManager);
if (polygonList[0].ChildNodes.Count > 0)
{
Polygon polygonOB = jsonParser.CreatePolygon(outerBoundaryIs[0].ChildNodes.Item(0).InnerText);
for (int k = 1; k < polygonList[0].ChildNodes.Count; k++)
{
if (polygonList[0].ChildNodes.Item(k).InnerText.Trim() != string.Empty)
{
if (FlagOrig == "NBO")
{
String param = graphAttrBuild["BUILDNO"].ToString() + "," + graphAttrBuild["PARCNO"].ToString();
String coordParam = jsonParser.RoundDeimal(polygonList.Item(0).ChildNodes.InnerText) + "\t" + param;
GlobalVar.InnerBoundBuildList.Add(coordParam);
}
polygonOB = GeometryEngine.Difference(polygonOB, jsonParser.CreatePolygon(polygonList.Item(0).ChildNodes.InnerText)) as Polygon;
}
}
Graphic graphic_Label = new Graphic();
Graphic graphic = new Graphic();
graphic.Geometry = polygonOB;
foreach (var attr1 in graphAttrBuild)
{
if (attr1.Key == "BUILDNO")
GetSetBuildNumber.Number = attr1.Value.ToString();
if (attr1.Key == "MAIN_PARCEL")
GetSetMainParc.MainParc = attr1.Value.ToString();
graphic.Attributes.Add(attr1.Key, attr1.Value);
}
if (FlagOrig == "NBO")
graphic.Symbol = styleObject.SetBuildSymbol(GetSetMainParc.MainParc);
if (FlagOrig == "BO")
graphic.Symbol = styleObject.SetOriginal();
MapPoint label = GeometryEngine.LabelPoint(polygonOB);
graphic_Label.Geometry = label;
graphic_Label.Symbol = Symbol.FromJson(styleObject.SymbolKO_Build_ToJson(Color.Black, 10, Color.Yellow, GetSetBuildNumber.Number, Esri.ArcGISRuntime.Symbology.HorizontalAlignment.Left, Esri.ArcGISRuntime.Symbology.VerticalAlignment.Bottom, 3));
Parallel.Invoke(
() => _overlay_Graphic.Graphics.Add(graphic),
() => _overlay_Graphic_Label.Graphics.Add(graphic_Label));
graphAttrBuild.Clear();
GlobalVar.ListBuildNo.Add(GetSetParcNumber.Number + ";" + GetSetBuildNumber.Number);
GlobalVar.UidKO.Add(i + 1);
GlobalVar.StartCount = i + 1;
// graphic = null;
graphic_Label = null;
}
}
}
}
catch
(Exception ex)
{ logger.Error(ex, "Настана грешка."); MessageBox.Show("Настана грешка. Ве молиме проверете логови (во /logs фолдерот) и контактирајте го администраторот."); }
}

This function read gml file.

0 Kudos