Select to view content in your preferred language

Exception on Draw.IsEnabled = true

4297
18
06-29-2010 02:49 AM
egismazeika
Emerging Contributor
Hi,
I have a Draw object for simple drawing on the map and a Button for enabling it:
ctrlDrawItem.IsEnabled = true;

Sometimes it works as expected but sometimes fails:

{System.NullReferenceException: Object reference not set to an instance of an object.
   at ESRI.ArcGIS.Client.LayerCanvas.PrepareAnimation()
   at ESRI.ArcGIS.Client.Map.beginZoomToExtent(Envelope targetExtent, Boolean skipAnimation)
   at ESRI.ArcGIS.Client.Map.zoomToResolution(Double resolution, MapPoint center, Boolean skipAnimation)
   at ESRI.ArcGIS.Client.Map.ZoomToResolution(Double resolution, MapPoint center)
   at ESRI.ArcGIS.Client.Map.ZoomToResolution(Double resolution)
   at ESRI.ArcGIS.Client.Map.Layers_LayersInitialized(Object sender, EventArgs args)
   at ESRI.ArcGIS.Client.LayerCollection.LayersInitializedHandler.Invoke(Object sender, EventArgs args)
   at ESRI.ArcGIS.Client.LayerCollection.calculateLevelScheme()
   at ESRI.ArcGIS.Client.LayerCollection.LayerCollection_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
   at System.Collections.ObjectModel.Collection`1.Add(T item)
   at ESRI.ArcGIS.Client.Draw.Activate()
   at ESRI.ArcGIS.Client.Draw.OnIsEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
   at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at ESRI.ArcGIS.Client.Draw.set_IsEnabled(Boolean value)
   at ViewPage.EditGeometry()
   at ViewPageXAML.cmdEditGeometry_Click(Object sender, RoutedEventArgs e)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Button.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)}

Still can't find any workarounds, just restarting the app and hopping that it will work this time.

Using SL 2.0 RC. Any ideas?
0 Kudos
18 Replies
JenniferNery
Esri Regular Contributor
Before you zip the file can you Clean Solution first? Maybe that will reduce the file size.

Are you able to reproduce the issue using this sample: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#DrawGraphics just by adding SnapToLevels, Minimum and MaximumResolution?
0 Kudos
AndyWright
Frequent Contributor
I did a clean and I'm still not able to upload.  The zip file is 1.6MB, so I'm not sure why it's not letting me upload it.  Can I put it on the ESRI FTP site for you?

And yes, I am able to replicate the problem in the sample by adding SnapToLevels and Min/Max resolution.
0 Kudos
JenniferNery
Esri Regular Contributor
And yes, I am able to replicate the problem in the sample by adding SnapToLevels and Min/Max resolution.


Oh cool then you can just copy-paste relevant code so I can replicate the issue with this same sample: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#DrawGraphics.
0 Kudos
AndyWright
Frequent Contributor
Sure, here you go.  I'm really convinced that the SnapToLevels="true" is the real culprit here.  There are cases throughout my application where map extent changed events fire for no apparent reason when that property is set to true.  When I set it to false they no longer fire.  I'm interested to see what you can come up with on this.  It's turned into a royal pain in the arse for us.

Here's the XAML ...

<UserControl x:Class="ESRI_Test.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:esri="http://schemas.esri.com/arcgis/client/2009">
    <Grid x:Name="LayoutRoot" Background="White">

        <Grid.Resources>
            <esri:SimpleLineSymbol x:Key="DrawLineSymbol" Color="Green" Width="4" />
            <esri:SimpleFillSymbol x:Key="DrawFillSymbol" Fill="#3300FF00" BorderBrush="Green" BorderThickness="2" />
            <esri:SimpleMarkerSymbol x:Key="DefaultMarkerSymbol" Color="Red" Size="12" Style="Circle" />
            <esri:SimpleLineSymbol x:Key="DefaultLineSymbol" Color="Red" Width="4" />
            <esri:SimpleFillSymbol x:Key="DefaultFillSymbol" Fill="#33FF0000" BorderBrush="Red" BorderThickness="2" />
        </Grid.Resources>

        <esri:Map x:Name="MyMap" Extent="-15000000,2000000,-7000000,8000000" SnapToLevels="True" MinimumResolution="0.0746126492252985" MaximumResolution="1222.99245256249">
            <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
                      Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
            <esri:GraphicsLayer ID="MyGraphicsLayer" />
        </esri:Map>

        <Canvas Width="370" Height="110" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,15,0" >
            <Rectangle Stroke="Gray"  RadiusX="10" RadiusY="10" Fill="#77919191" Canvas.Left="0" 
                       Canvas.Top="0" Width="370" Height="110" >
                <Rectangle.Effect>
                    <DropShadowEffect/>
                </Rectangle.Effect>
            </Rectangle>
            <Rectangle Fill="#FFFFFFFF" Stroke="DarkGray" RadiusX="5" RadiusY="5" Canvas.Left="10" 
                       Canvas.Top="10" Width="350" Height="90"  />
            <TextBlock x:Name="ToolTile" Foreground="Black" Width="340" TextAlignment="Center" 
                       FontSize="9" FontWeight="Bold" Canvas.Left="20" Canvas.Top="10" 
                       Text="Use these tools to add graphics to the map." />
            <StackPanel Orientation="Vertical" Canvas.Top="5" Canvas.Left="20">
                <esri:Toolbar x:Name="MyToolbar" MaxItemHeight="80" MaxItemWidth="80"
                    Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Center"
                    ToolbarItemClicked="MyToolbar_ToolbarItemClicked" 
                    ToolbarIndexChanged="MyToolbar_ToolbarIndexChanged"
                    Width="330" Height="80">
                    <esri:Toolbar.Items>
                        <esri:ToolbarItemCollection>
                            <esri:ToolbarItem Text="Add a point">
                                <esri:ToolbarItem.Content>
                                    <Image Source="Assets/images/i_draw_point.png" Stretch="UniformToFill" Margin="5" />
                                </esri:ToolbarItem.Content>
                            </esri:ToolbarItem>
                            <esri:ToolbarItem Text="Add a Polyline">
                                <esri:ToolbarItem.Content>
                                    <Image Source="Assets/images/i_draw_line.png" Stretch="UniformToFill" Margin="5" />
                                </esri:ToolbarItem.Content>
                            </esri:ToolbarItem>
                            <esri:ToolbarItem Text="Add a Polygon">
                                <esri:ToolbarItem.Content>
                                    <Image Source="Assets/images/i_draw_poly.png" Stretch="UniformToFill" Margin="5" />
                                </esri:ToolbarItem.Content>
                            </esri:ToolbarItem>
                        </esri:ToolbarItemCollection>
                    </esri:Toolbar.Items>
                </esri:Toolbar>
                <TextBlock x:Name="StatusTextBlock" Text="" FontWeight="Bold" HorizontalAlignment="Center"/>
            </StackPanel>
        </Canvas>

        
    </Grid>
</UserControl>




Here's the code behind ...

using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Symbols;

namespace ESRI_Test
{
    public partial class MainPage : UserControl
    {
        private Draw MyDrawObject;
        private Symbol _activeSymbol = null;

        public MainPage()
        {
            InitializeComponent();

            MyDrawObject = new Draw(MyMap)
            {
                LineSymbol = LayoutRoot.Resources["DrawLineSymbol"] as LineSymbol,
                FillSymbol = LayoutRoot.Resources["DrawFillSymbol"] as FillSymbol
            };

            MyDrawObject.DrawComplete += MyDrawObject_DrawComplete;
        }

        private void MyToolbar_ToolbarItemClicked(object sender, ESRI.ArcGIS.Client.Toolkit.SelectedToolbarItemArgs e)
        {
            switch (e.Index)
            {
                case 0: // Point
                    MyDrawObject.DrawMode = DrawMode.Point;
                    _activeSymbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as Symbol;
                    break;
                case 1: // Polyline
                    MyDrawObject.DrawMode = DrawMode.Polyline;
                    _activeSymbol = LayoutRoot.Resources["DefaultLineSymbol"] as Symbol;
                    break;
                case 2: // Polygon
                    MyDrawObject.DrawMode = DrawMode.Polygon;
                    _activeSymbol = LayoutRoot.Resources["DefaultFillSymbol"] as Symbol;
                    break;
                case 3: // Rectangle
                    MyDrawObject.DrawMode = DrawMode.Rectangle;
                    _activeSymbol = LayoutRoot.Resources["DefaultFillSymbol"] as Symbol;
                    break;
                case 4: // Freehand
                    MyDrawObject.DrawMode = DrawMode.Freehand;
                    _activeSymbol = LayoutRoot.Resources["DefaultLineSymbol"] as Symbol;
                    break;
                default: // Clear Graphics
                    MyDrawObject.DrawMode = DrawMode.None;
                    GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
                    graphicsLayer.ClearGraphics();
                    break;
            }
            MyDrawObject.IsEnabled = (MyDrawObject.DrawMode != DrawMode.None);
        }

        private void MyToolbar_ToolbarIndexChanged(object sender, ESRI.ArcGIS.Client.Toolkit.SelectedToolbarItemArgs e)
        {
            StatusTextBlock.Text = e.Item.Text;
        }


        private void MyDrawObject_DrawComplete(object sender, ESRI.ArcGIS.Client.DrawEventArgs args)
        {
            GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
            ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic()
            {
                Geometry = args.Geometry,
                Symbol = _activeSymbol,
            };
            graphicsLayer.Graphics.Add(graphic);
        }
    }
}

0 Kudos
JenniferNery
Esri Regular Contributor
I am using the code you provided against assemblies from v2.1 Final. Choosing draw mode should enable/disable draw but no exception is thrown for me.

Could you be more specific with steps to reproduce?
Something like:
1. Choose Point from ToolBar.
2. Draw the point on map.
3. Choose Clear graphics from ToolBar. Exception should be thrown.
0 Kudos
AndyWright
Frequent Contributor
Jennifer,

You are right there is no exception that is getting thrown.  The issue here is not about errors, it's about the map extent changed event getting fired for no apparent reason.  Put a breakpoint in the map extent changed event then click one of the tools in the toolbar.  Why on earth would that event fire because you enabled a draw object?  Now set the SnapToLevels property to false and/or remove the min/max scale thresholds.  Now the map extent changed event doesn't fire when you click one of the tools in the toolbar.

Like I said in my previous post, we have issues scattered throughout our application with the map extent changed event firing for no good reason.  And we're not willing to set SnapToLevels equal to false to remedy that.  I'm interested to hear why this event fires when the map extent doesn't change and what possible workarounds there might be until this issue is fixed in an upcoming version of the API.

Thanks ...
0 Kudos
JenniferNery
Esri Regular Contributor
Ahh okay. I'm sorry about misunderstanding. I guess the title of this thread no longer applies.

Anyway, I was able to repro the issue with v2.1 Final. Map ExtentChanged got fired everytime the Draw is enabled. Thank you for reporting this bug. This will be fixed in future version of the API. I'm no longer able to repro the issue with our latest source code.

Notice that when ExtentChanged is raised by enabling/disabling draw, the following is true:
e.NewExtent.Equals(e.OldExtent). For a workaround then, you can ignore cases when there is no real extent change.
0 Kudos
AndyWright
Frequent Contributor
Yeh sorry for the confusion there.  I posted to this thread because it seemed like my issue may have been related to the original issue.

Anyway, I am currently doing the workaround you suggested.  It works for the most part, but there are still some very weird things that happen from time to time that are related to this issue.  I guess at this point I'll wait for version 2.2 to come out and test everything against that.  Do you have any idea when that might be released or if I could get my hands on a beta version?
0 Kudos
DenisT
by
Deactivated User
Checked API 2.3 and 2.4, it looks like the bug still exists.

When I enable my Draw instance, the Map raises ExtentChanged event. I examined event args and noticed that the old extent and the new one are the same.
The raised event is not the problem for me. The problem is that my dynamic map layers blink.

Normally when a dynamic layer redraws, for example when you pan, the old image stay visible (with empty borders) until the new one arrives. Then the old image is replaced with the new one.
But not in this case. In this case the dynamic layer goes blank, then the web-request is made, and when response is arrived the image will appear.
Tiled layers work fine.

This happens only if Map.SnapToLevels is set to true.

I don't know why but I can't reproduce that behavior in a simple example. It looks like there are some additional conditions.

P.S. Sorry for my English.
0 Kudos