Select to view content in your preferred language

How to save ESRI.ArcGIS.Client.Graphic object into isolated storage

841
5
08-17-2011 10:31 AM
BabakSekandari1
Deactivated User
I am attempting to save an ESRI.ArcGIS.Client.Graphic object into silverlight's isolated storage.

My code appears as such:

private void GraphicQueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs queryEventArgs)
{
//...
FeatureSet featureSet = queryEventArgs.FeatureSet;
string featureSetDisplayName = featureSet.DisplayFieldName;
//...
foreach (Graphic graphicFeature in featureSet.Features)
{

using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream(featureSetDisplayName, FileMode.Create, isoStore))
{
// What do I do here?

}
}
// ...
}
}

I don't know what do do to save the Graphic object graphicFeature into isolated storage once I have my IsolatedStorageFileStream.
0 Kudos
5 Replies
IgressT
Emerging Contributor
0 Kudos
BabakSekandari1
Deactivated User
Thank you for the reply.

When I added that extension method from the GraphicsLayerExtension.cs file, I was able to get my
GrahpicsLayer object to see the extension method in the intellisense.
That's some clever coding in the two files for the extension method, by the way.

I have a GraphicsLayer object also which appears as follows:
GraphicsLayer graphicsLayer = WebMap.Layers["aGraphicsLayer"] as GraphicsLayer;

So, I was able to successfully get:
graphicsLayer.SerializeGraphics(writer);

However, the object that I wish to serialize into isolated storage is an ESRI.ArcGIS.Client.Graphic whereas the extension method is for an ESRI.ArcGIS.Client.GraphicsLayer object.

I have:
FeatureSet featureSet = queryEventArgs.FeatureSet;
//...
foreach (Graphic graphicFeature in featureSet.Features)
{
//...
// So I can get:
graphicFeature.SerializeGraphics(writer);

So, what I'm going to try to do is to tweak the extension method so that it works for Grahpic objects also. Hopefully it will be a simple process.

I'll let you all know how that goes.

In the meantime, any suggestions are welcome.
0 Kudos
BabakSekandari1
Deactivated User
Hmmm, I ran into an obstacle.

In the GraphicsLayerExtension.cs file, I tried to convert the following function:

  public static void SerializeGraphics(this GraphicsLayer graphicsLayer, XmlWriter writer)
  {
   XMLSerialize(writer, new SerializableGraphicCollection(graphicsLayer.Graphics));
  }

into the following function:

        public static void SerializeGraphic(this Graphic graphic, XmlWriter writer)
        {
            XMLSerialize(writer, new SerializableGraphicCollection(graphic.Graphics()));
        }

But that doesn't work because the Graphic class does not have a Graphics property the way the GraphicsLayer class has a Graphics property.

I tried using a graphic.Geometry property, but that doesn't work because SerializableGraphicCollection() accepts a GraphicsLayer object, not a Graphic object.

So, I'm going to try to tweak the SerializableGraphicCollection() method in the DataContracts.cs file to accept a Graphic object in the constructor.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
You can tweak the code from a GraphicsLayer extension to a graphics list extension:

public static class GraphicsExtension
{
 public static void SerializeGraphics(this IList<Graphic> graphics, XmlWriter writer)
 {
     XMLSerialize(writer, new SerializableGraphicCollection(graphics));
 } 
 public static void DeserializeGraphics(this IList<Graphic> graphics, XmlReader reader)
 {
     foreach (SerializableGraphic g in XMLDeserialize<SerializableGraphicCollection>(reader))
     { 
        graphics.Add(g.Graphic);
     }
 }
......


You have also to change the argument of SerializableGraphicCollection
public SerializableGraphicCollection(IEnumerable<Graphic> graphicCollection)


This version is even much better since useable in both cases (graphicsLayer.Graphics or featureSet.Features):)
0 Kudos
DorothyMortenson
Deactivated User
Hi Babak,
I found a comment you had on this site, regarding printing to pdf:
http://forums.silverlight.net/t/118030.aspx/2/10#

I wondered if you ever found a resolution to this. It sounds like this thread is related.

Dorothy
0 Kudos