'Graphic is already associated with another layer' error message

1037
3
01-24-2014 07:20 AM
Labels (1)
AlanDrury
New Contributor
I'm creating a WPF application using the MVVM framework and I've hit a problem I can't solve. The application has a main window that displays a user control containing a map control with two Graphic layers, XAML below.

<esri:Map x:Name="map" IsLogoVisible="False" Background="White" WrapAround="True" MaximumResolution="2.5"
   Height="{Binding ElementName=dckPnl_Map, Path=ActualHeight}"
   Width="{Binding ElementName=dckPnl_Map, Path=ActualWidth}"
   Extent="-401169.043457629, 6536918.47363432, -398878.321265893, 6538542.76048561"
   local:MapHelpers.Extent="{Binding MapExtent}">
 <!--<esri:AcceleratedDisplayLayers>-->
 <esri:ArcGISLocalTiledLayer ID="OSStreetView" Path="./MapTiles/OsStreetView2013_18_11/v101/Layers/" />
 <!--</esri:AcceleratedDisplayLayers>-->
 <esri:GraphicsLayer ID="GPSLayer" x:Name="gpsLayer" IsHitTestVisible="True" RendererTakesPrecedence="True"
                        GraphicsSource="{Binding VehicleLocations, Source={StaticResource RTControlViewModel}}">
  <esri:GraphicsLayer.Renderer>
   <esri:SimpleRenderer Symbol="{StaticResource esriMarker}" />
  </esri:GraphicsLayer.Renderer>
  <esri:GraphicsLayer.MapTip> 
   <Border Padding="4,2" Background="Black">
    <StackPanel>
     <TextBlock Text="{Binding [Description]}" Foreground="White"/>
    </StackPanel>
   </Border>
  </esri:GraphicsLayer.MapTip>
 </esri:GraphicsLayer>
    <!--<esri:GraphicsLayer ID="MarkerLayer" x:Name="markerLayer" IsHitTestVisible="True" RendererTakesPrecedence="True"
                        GraphicsSource="{Binding EditableLocations, Source={StaticResource RTControlViewModel}}">
        <esri:GraphicsLayer.Renderer>
            <esri:SimpleRenderer Symbol="{StaticResource esriMarker}" />
        </esri:GraphicsLayer.Renderer>
        <esri:GraphicsLayer.MapTip>
            <Border Padding="4,2" Background="Black">
                <StackPanel>
                    <TextBlock Foreground="White" Text="{Binding [Description]}"/>
                </StackPanel>
            </Border>
        </esri:GraphicsLayer.MapTip>
    </esri:GraphicsLayer>-->
    <i:Interaction.Triggers>
  <i:EventTrigger EventName="TouchDown">
            <cmd:EventToCommand Command="{Binding TapLocation}" PassEventArgsToCommand="True"/>
  </i:EventTrigger>
        <i:EventTrigger EventName="MouseClick">
            <cmd:EventToCommand Command="{Binding ClickLocation}" PassEventArgsToCommand="True"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</esri:Map>


I have just added the second GraphicsLayer after having just one, bound to a point data source, which I didn't have any problems with. Now, when I display the control for the first time everything works as expected, when I then display the control for a second time I get the the following error.

Set property 'ESRI.ArcGIS.Client.GraphicsLayer.GraphicsSource' threw an exception. The inner exception has the following message.
Specified argument was out of the range of valid values. Parameter name: Graphic is already associated with another layer.

I'm saving my view models in memory and trying to reuse them for each instance of the controls. Does anyone have an idea what might be going wrong?

Thanks

Al
0 Kudos
3 Replies
AlanDrury
New Contributor
This problem has been troubling me for the past few days but I seem to have found a work around / solution.

I thought that in WPF when a user control is no longer being displayed then it will be disposed of, so it was strange to find the error being thrown when the InitializeComponent method in my view code behind class was being called. My map control is in the main control displayed for my application. When displaying other controls then returning to the main one with the map, the exception would be thrown.

After searching around for a solution I came up with adding the following code in the main control code behind class.

protected override void OnUnloaded(EventArgs e)
        {
            //  Tidy up map graphic layers
            //  Need to do this to avoid the Graphic is already associated with another layer message

            foreach (Layer layer in map.Layers)
            {
                if (layer is GraphicsLayer)
                {
                    ((GraphicsLayer)layer).GraphicsSource = null;
                }
            }

            base.OnUnloaded(e);
        }


This seems to cure the problem but I'm open to suggestions as to what I'm doing wrong if this seems unnecessary.

Cheers

Al
0 Kudos
AnttiKajanus1
Occasional Contributor III
Hey,

I will try to check this. Any change you can provide simplified repro?
0 Kudos
AlanDrury
New Contributor
Hi Antti,

I'm trying to catch up the time I've lost fixing this problem but I'll try to put something together and post it soon.

Al
0 Kudos