Persisting data for Add-In extension ( 10.4 )

1467
6
08-11-2016 09:20 AM
MarkLopez2
New Contributor

Hi Everyone,

I'm currently converting some COM extensions to add-ins for our ArcMap 10.4 deployment but am stuck with a persistence issue.

Following the sample from here:ArcObjects 10 .NET SDK Help

private MyPersistentData _data;

protected override void OnLoad(Stream inStrm)
{
// Initialize the struct.
    _data.Location = "";
    _data.Point = new ESRI.ArcGIS.Geometry.PointClass();

    PersistHelper.Load < MyPersistentData > (inStrm, ref _data);
}

protected override void OnSave(Stream outStrm)
{
    PersistHelper.Save < MyPersistentData > (outStrm, _data);
}

[Serializable()]
private struct MyPersistentData
{
public string Location;
public ESRI.ArcGIS.Geometry.PointClass Point;
}

Utilizing the exact code in my add-in extension , I'm getting an error on the OnLoad routine.

  • Cannot create uninitialized instance of types requiring managed activation.

Saving the custom object works fine and it also load fine when change the object to a just a point.I'm needing to saving multiple items hence the need for a custom object. I'm not sure how I can use the PersistenceHelper class to save everything that's needed.

Tags (1)
0 Kudos
6 Replies
MarkRubelmann
New Contributor III

I'm a little confused by the error message you're getting, but my guess is that it's because the second parameter to Load is a ref parameter, not an out parameter.  Try new'ing up the _data member:

protected override void OnLoad(Stream inStrm)
{
    _data = new MyPersistentData();
    _data.Location = "";
    _data.Point = new ESRI.ArcGIS.Geometry.PointClass();

    PersistHelper.Load < MyPersistentData > (inStrm, ref _data);
}

If it were an out parameter, then Load would initialize the object instead of you doing it in OnLoad.

BTW, the problem could be something completely different given the bit about "managed activation".  What specific line is the error from?  The call to Load or setting Location or Point?

0 Kudos
MarkLopez2
New Contributor

Thank you Mark..(same hat )

The error occurred right on

PersistHelper.Load < MyPersistentData > (inStrm, ref _data);

According the ESRI resources for the PersistenceHelper class , the second parameter is an out

http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/004v/004v00000378000000.htm

I've also tried not new'ing variable without any success.

0 Kudos
MarkRubelmann
New Contributor III

Hmm...  I followed your link but it looks to me like it wants a ref.  It would give you a compiler error if your code isn't using the correct one.  Speaking of which, is that a compiler error you're getting or a runtime exception?

You said you've "also tried not new'ing".  Did you try including the new like I had in my first comment?

0 Kudos
MarkLopez2
New Contributor

Sorry.. I meant to say that I did try newing the variable. This is a runtime error. Also like to point out that if I change the object to have two string properties.. instead of the pointclass then everything works.

0 Kudos
MarkRubelmann
New Contributor III

The only other suggestion I can think of is changing MyPersistentData's Point variable to an ESRI.ArcGIS.Geometry.IPoint instead of PointClass.  If that doesn't work, then I'm afraid we've ventured deeper into ArcObjects than I've ever gone.  Do you need to have one of their COM objects inside the class you're trying to persist or is this simply to get their sample code up and running?

0 Kudos
MarkLopez2
New Contributor

Actually I'm trying to persist custom fillsymbols (SimpleFillSymbol). BinaryFormatter didn't like serializing them so eventually found the PersistenceHelper class. While the helper class works with a single symbol it didn't work with a custom object holding multiple symbols. Thank you for looking into this.

0 Kudos