Save Map extent to a file

809
2
01-15-2014 06:06 AM
Labels (1)
FrancoisGilbert
New Contributor
Hello,

I would like to save my map extent to a ini file to allow the user to start where he was the last time he worked with the application. I have a map packaged in a UserControl x:Class="WpfArcGis.GisControl" and the map is defined in the XAML as <esri:Map x:Name="MyMap".

In the GisControl class I have a the following method

Namespace WpfArcGis
    Public Class GisControl
        Protected Overrides Sub Finalize()
             MyBase.Finalize()
        End Sub
    End Class
End Namespace

If I try to access the MyMap.Extent to save the values, I receive the following message: "The current thread is not accessible."

Do you know what function should I use to save my map extent.

Regards
0 Kudos
2 Replies
BKuiper
Occasional Contributor III
The error is related due to a thread trying to access data from a different thread. This is a common problem developers run into when developing in WPF. Your Map is created on the UI thread and you are presumably trying the save the extent through a background thread or something.

Something like the following pseudo code should probably solve your problem

MyMap.Dispatcher.BeginInvoke(new Action(() =>{
 string json = MyMap.Extent.ToJson();
 // save json string to file.
}));
0 Kudos
FrancoisGilbert
New Contributor
Hello,

As I am a vb person, so I have written the following code

        Protected Overrides Sub Finalize()
            MyMap.Dispatcher.BeginInvoke(New Action(Function()
                                                        ' save json string to file.
                                                        Dim sExtent As String = MyMap.Extent.ToString

                                                    End Function))

            MyBase.Finalize()
        End Sub

However, I cannot break and look at the code to see the value of my sExtent.
0 Kudos