I have migrated an classic COM ArcMap Extension of mine to an add-in.I have tried the new persistence approach of add-in's but I cannot serialize my custom objects through the XMLSerializer. I get all sorts of COM errors. The custom objects all implement IPersistVariant. And I suspect this interface needs COM registration to work.What would be the most efficient way to get my objects to persist?
Or is there a way that I can still use the IPersistVariant interface on my custom objects to persist them in the mxd-document?
This is the class of the custom object I am trying to persist:
Imports ESRI.ArcGIS.esriSystemImports ESRI.ArcGIS.Geometry _Public Class RigPolylineImplements IPersistVariant#Region "COM GUIDs" Public Const ClassId As String = "1572F48A-63B5-4720-AF6A-0B898B549764" Public Const InterfaceId As String = "13AA4CE9-95A0-43A1-8A92-9BF66B45E226" Public Const EventsId As String = "2EE2B76B-DC06-4563-8441-9FA0BC584C20"#End RegionPublic m_Name As String = ""Public m_Polyline As IPolylinePublic m_IsVisible As BooleanPublic m_PolylineID As IntegerPublic Sub New()End SubPublic Property Name() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value End SetEnd PropertyPublic Property Polyline() As IPolyline Get Polyline = m_Polyline End Get Set(ByVal Value As IPolyline) m_Polyline = Value End SetEnd PropertyPublic Property Visible() As Boolean Get Return m_IsVisible End Get Set(ByVal Value As Boolean) m_IsVisible = Value End SetEnd PropertyPublic Property PolylineID() As Integer Get Return m_PolylineID End Get Set(ByVal Value As Integer) m_PolylineID = Value End SetEnd PropertyPublic Function CreateCopy() As RigPolyline Dim pPolylineCopy As RigPolyline = New RigPolyline Dim pPolyline As IPolyline = New Polyline Dim pClone As IClone pPolylineCopy.Name = m_Name pPolylineCopy.Visible = m_IsVisible pPolylineCopy.PolylineID = m_PolylineID pClone = m_Polyline pPolyline = pClone.Clone pPolylineCopy.Polyline = pPolyline Return pPolylineCopyEnd FunctionPublic ReadOnly Property ID As ESRI.ArcGIS.esriSystem.UID Implements ESRI.ArcGIS.esriSystem.IPersistVariant.ID Get Try Dim extUID As New UIDClass() extUID.Value = Me.GetType().GUID.ToString("B") Return extUID Catch ex As Exception Dim pErr As New ShowError(ex) Return Nothing End Try End GetEnd PropertyPublic Sub Load(ByVal Stream As ESRI.ArcGIS.esriSystem.IVariantStream) Implements ESRI.ArcGIS.esriSystem.IPersistVariant.Load Try m_Name = Stream.Read m_Polyline = Stream.Read m_IsVisible = Stream.Read m_PolylineID = Stream.Read Catch ex As Exception Dim pErr As New ShowError(ex) End TryEnd SubPublic Sub Save(ByVal Stream As ESRI.ArcGIS.esriSystem.IVariantStream) Implements ESRI.ArcGIS.esriSystem.IPersistVariant.Save Try Stream.Write(m_Name) Stream.Write(m_Polyline) Stream.Write(m_IsVisible) Stream.Write(m_PolylineID) Catch ex As Exception Dim pErr As New ShowError(ex) End TryEnd SubEnd ClassThe class that inherits from Extension holds the Load en Save methods:
Imports ESRI.ArcGIS.esriSystemImports ESRI.ArcGIS.ADF.SerializationImports ESRI.ArcGIS.GeometryPublic Class APPExtensionInherits ESRI.ArcGIS.Desktop.AddIns.ExtensionPrivate Const PersistdataVersion As String = "4.0"Private mData As MyPersistentData _Private Structure MyPersistentData Public persistanceVersion As String Public RigPolyline As Byte()End StructurePublic Sub New()End SubProtected Overrides Sub OnLoad(ByVal inStrm As System.IO.Stream) mData.persistanceVersion = "" mData.RigPolyline = Nothing PersistenceHelper.Load(Of MyPersistentData)(inStrm, mData) If mData.persistanceVersion = PersistdataVersion Then Dim mbs As IMemoryBlobStream = New MemoryBlobStream Dim mbsv As IMemoryBlobStreamVariant = mbs mbsv.ImportFromVariant(mData.RigPolyline) Dim pStream As IPersistStream = New PropertySet pStream.Load(mbs) Dim pPropSet As IPropertySet = pStream Dim line As RigPolyline = pPropSet.GetProperty("RigPolyline") End IfEnd SubProtected Overrides Sub OnSave(ByVal outStrm As System.IO.Stream) mData = New MyPersistentData mData.persistanceVersion = "4.0" Dim line As RigPolyline = New RigPolyline line.Name = "My line" line.Polyline = New Polyline Dim propSet As IPropertySet = New PropertySet propSet.SetProperty("RigPolyline", line) Dim ps As IPersistStream = propSet Dim mbs As IMemoryBlobStream = New MemoryBlobStream ps.Save(mbs, 0) Dim bytes As Byte() Dim mbsv As IMemoryBlobStreamVariant = mbs mbsv.ExportToVariant(bytes) mData.RigPolyline = bytes PersistenceHelper.Save(Of MyPersistentData)(outStrm, mData)End SubEnd Class
أكثر...
Or is there a way that I can still use the IPersistVariant interface on my custom objects to persist them in the mxd-document?
This is the class of the custom object I am trying to persist:
Imports ESRI.ArcGIS.esriSystemImports ESRI.ArcGIS.Geometry _Public Class RigPolylineImplements IPersistVariant#Region "COM GUIDs" Public Const ClassId As String = "1572F48A-63B5-4720-AF6A-0B898B549764" Public Const InterfaceId As String = "13AA4CE9-95A0-43A1-8A92-9BF66B45E226" Public Const EventsId As String = "2EE2B76B-DC06-4563-8441-9FA0BC584C20"#End RegionPublic m_Name As String = ""Public m_Polyline As IPolylinePublic m_IsVisible As BooleanPublic m_PolylineID As IntegerPublic Sub New()End SubPublic Property Name() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value End SetEnd PropertyPublic Property Polyline() As IPolyline Get Polyline = m_Polyline End Get Set(ByVal Value As IPolyline) m_Polyline = Value End SetEnd PropertyPublic Property Visible() As Boolean Get Return m_IsVisible End Get Set(ByVal Value As Boolean) m_IsVisible = Value End SetEnd PropertyPublic Property PolylineID() As Integer Get Return m_PolylineID End Get Set(ByVal Value As Integer) m_PolylineID = Value End SetEnd PropertyPublic Function CreateCopy() As RigPolyline Dim pPolylineCopy As RigPolyline = New RigPolyline Dim pPolyline As IPolyline = New Polyline Dim pClone As IClone pPolylineCopy.Name = m_Name pPolylineCopy.Visible = m_IsVisible pPolylineCopy.PolylineID = m_PolylineID pClone = m_Polyline pPolyline = pClone.Clone pPolylineCopy.Polyline = pPolyline Return pPolylineCopyEnd FunctionPublic ReadOnly Property ID As ESRI.ArcGIS.esriSystem.UID Implements ESRI.ArcGIS.esriSystem.IPersistVariant.ID Get Try Dim extUID As New UIDClass() extUID.Value = Me.GetType().GUID.ToString("B") Return extUID Catch ex As Exception Dim pErr As New ShowError(ex) Return Nothing End Try End GetEnd PropertyPublic Sub Load(ByVal Stream As ESRI.ArcGIS.esriSystem.IVariantStream) Implements ESRI.ArcGIS.esriSystem.IPersistVariant.Load Try m_Name = Stream.Read m_Polyline = Stream.Read m_IsVisible = Stream.Read m_PolylineID = Stream.Read Catch ex As Exception Dim pErr As New ShowError(ex) End TryEnd SubPublic Sub Save(ByVal Stream As ESRI.ArcGIS.esriSystem.IVariantStream) Implements ESRI.ArcGIS.esriSystem.IPersistVariant.Save Try Stream.Write(m_Name) Stream.Write(m_Polyline) Stream.Write(m_IsVisible) Stream.Write(m_PolylineID) Catch ex As Exception Dim pErr As New ShowError(ex) End TryEnd SubEnd ClassThe class that inherits from Extension holds the Load en Save methods:
Imports ESRI.ArcGIS.esriSystemImports ESRI.ArcGIS.ADF.SerializationImports ESRI.ArcGIS.GeometryPublic Class APPExtensionInherits ESRI.ArcGIS.Desktop.AddIns.ExtensionPrivate Const PersistdataVersion As String = "4.0"Private mData As MyPersistentData _Private Structure MyPersistentData Public persistanceVersion As String Public RigPolyline As Byte()End StructurePublic Sub New()End SubProtected Overrides Sub OnLoad(ByVal inStrm As System.IO.Stream) mData.persistanceVersion = "" mData.RigPolyline = Nothing PersistenceHelper.Load(Of MyPersistentData)(inStrm, mData) If mData.persistanceVersion = PersistdataVersion Then Dim mbs As IMemoryBlobStream = New MemoryBlobStream Dim mbsv As IMemoryBlobStreamVariant = mbs mbsv.ImportFromVariant(mData.RigPolyline) Dim pStream As IPersistStream = New PropertySet pStream.Load(mbs) Dim pPropSet As IPropertySet = pStream Dim line As RigPolyline = pPropSet.GetProperty("RigPolyline") End IfEnd SubProtected Overrides Sub OnSave(ByVal outStrm As System.IO.Stream) mData = New MyPersistentData mData.persistanceVersion = "4.0" Dim line As RigPolyline = New RigPolyline line.Name = "My line" line.Polyline = New Polyline Dim propSet As IPropertySet = New PropertySet propSet.SetProperty("RigPolyline", line) Dim ps As IPersistStream = propSet Dim mbs As IMemoryBlobStream = New MemoryBlobStream ps.Save(mbs, 0) Dim bytes As Byte() Dim mbsv As IMemoryBlobStreamVariant = mbs mbsv.ExportToVariant(bytes) mData.RigPolyline = bytes PersistenceHelper.Save(Of MyPersistentData)(outStrm, mData)End SubEnd Class
أكثر...