Search code examples
c#.netsilverlight

Assigning a StrokeDashArray to a Line object cause 4004 Error


I'm using SilverLight on Visual Studio 2008, Ver 3.5 SP1,

I have a small application that creates a new Line with a StrokeDashArray. I have created the StrokeDashArray and stored it in a helper class (together with other objects that get reused). The Line is assigned like this:

var myLine = new Line { 
    X1 = myHelperClass.X, 
    X2 = ..., 
    Y1 = ... 
    Y2 = ..., 
    Fill = myHelperclass.Fill, 
    StrokeThickness = myHelperclass.StrokeThickness, 
    Stroke = myHelperClass.Stroke, 
    StrokeDashArray = myHelperClass.StrokeDashArray
};

_parenCanvas.Children.Add(myLine);

The object containing the line is in a class array. Depending on what the user selects it will draw the shapes on the canvas. Say I have myDrawing1, myDrawing2 and user selects the first one, it draws fine. User then selects the second one, it draws fine, too. User then selects the first again and I get the following error:

An unhandled exception ("Unhandled Error in Silverlight Application Code: 4004. 
  Category: ManagedRuntimeError, 
  Message: System ArgumentException: Value does not fall within the expected range.

If I rewrite the above Line assignment and put in new DoubleCollection {5, 5}, the code works fine. I also checked all the values when I step through and they seem to be perfectly fine. I can't see any reason why the code fails on that line.

The reason I want to use the StrokeDashArray to be assigned from the helper class is so that the values can be customised during runtime. However, at the moment there is no customisation happening so it's exactly the same StrokeDashArray that is assigned to it on the first and subsequent assignments, and it always works the first time.

The values in myHelperClass.StrokeDashArray[0] and myHelperClass.StrokeDashArray[1] are always 5.0. Anybody any idea what's going on here.

Thanks


Solution

  • There are some known bugs regarding the use of StrokeDashArray in Silverlight, for example you cannot assign a StrokeDashArray in a Style from code-behind. See the following blog post with a workaround. You might have to use DoubleCollection rather than StrokeDashArray in your helper class.

    (This is quite an old bug, was initially reported in SL2.0!)