[GIS] Can draw points but not polylines in WPF Runtime code

arcgis-api-wpfmap-drawing

The following C# code works when drawing in Point mode, but not Polyline . . .

  // "Draw" is ESRI.ArcGIS.Client.Draw object
       _draw = new Draw(_map);
       _draw.DrawMode = DrawMode.Polyline;
       _draw.DrawComplete += (senderObject, drawEventArgs) =>
        {
            _barrierLine = new Graphic();
            _barrierLine.Symbol = this.Resources["LineSymbol"] as SimpleLineSymbol;
            _barrierLine.Geometry = drawEventArgs.Geometry;
            _lineGraphicsLayer.Graphics.Add(_barrierLine);
        };

This is what the XML code looks like (I tried several different methods):

   <Window.Resources>
        <esri:SimpleMarkerSymbol x:Key="PointSymbol" Color="DarkViolet" Size="15" Style="Diamond"></esri:SimpleMarkerSymbol>
        <esri:SimpleLineSymbol x:Key="LineSymbol" Color="Aqua" Width="5" Style="Solid"></esri:SimpleLineSymbol>
    </Window.Resources>
    <Grid>
        <Grid.Resources>
            <esri:SimpleMarkerSymbol x:Key="RedMarkerSymbol" Color="Red"/>
            <esri:SpatialReference x:Key="theSpatialReference" WKID="4326"/>
            <esri:SimpleLineSymbol x:Key="LineSymbol" Color="Aqua" Width="3" Style="Solid"/>      
        </Grid.Resources>
            <!--Graphics Layer-->
            <esri:GraphicsLayer ID="graphicsLayer" x:Name="_graphicsLayer">
                <esri:Graphic>                    
                    <esri:Graphic.Symbol>                        
                        <esri:SimpleMarkerSymbol Style="Circle" Color="Red"/>
                    </esri:Graphic.Symbol>
                    <esri:Graphic.Geometry>
                        <esri:MapPoint  X="0" Y="0" >
                            <esri:MapPoint.SpatialReference>
                                <esri:SpatialReference WKID="4326"/>
                            </esri:MapPoint.SpatialReference>
                        </esri:MapPoint>                    
                        </esri:Graphic.Geometry>                  
                </esri:Graphic> 
            </esri:GraphicsLayer>
            <esri:GraphicsLayer ID="lineGraphicsLayer" x:Name="_lineGraphicsLayer">
                <esri:Graphic>
                    <esri:Graphic.Symbol>
                        <esri:SimpleLineSymbol Style="Solid" Color="Aqua" Width="3" />
                    </esri:Graphic.Symbol>
                    <esri:Graphic.Geometry>
                        <esri:Polyline>
                            <esri:Polyline.SpatialReference>
                                <esri:SpatialReference WKID="4326"/>
                            </esri:Polyline.SpatialReference>
                        </esri:Polyline>
                    </esri:Graphic.Geometry>
                </esri:Graphic>

            </esri:GraphicsLayer>
        </esri:Map>
        <StackPanel Orientation="Vertical" HorizontalAlignment="Left">
            <Button Content="Stops" VerticalAlignment="Top" Name="StopsButton" Margin="5,5,5,0" Click="StopsButton_Click" />
            <Button Content="Point Barriers" Height="23" Name="PointBarriersButton" Width="76" Click="PointBarriersButton_Click" />
            <Button Content="Solve" Name="SolveButton" Margin="5,5,5,0" Click="SolveButton_Click" />
            <Button Content="Line Barriers" Height="23" Name="LineBarriersButton" Width="76" Click="LineBarriersButton_Click" />
        </StackPanel>
    </Grid>

Best Answer

    _draw.DrawComplete += (senderObject, drawEventArgs) =>
    {
        _barrierLine = new Graphic();
        _barrierLine.Symbol = this.Resources["LineSymbol"] as SimpleLineSymbol;
        _barrierLine.Geometry = drawEventArgs.Geometry;
        _lineGraphicsLayer.Graphics.Add(_barrierLine);
    };

I suspect this is not firing properly. When you draw a point releasing the mouse would be a DrawComplete action. I suspect with a polyline the system doesn't know when you are done and doesn't execute this code. For testing change the listener to a mouse up and see what happens. I would add further that this will only verify that your event is not firing. It will crash horribly. To avoid this you should create the polyline object outside the listener and add points after the mouse up. On your terminator event (double_click usually) you should execute the code needed to signal the end of the polyline.