views:

470

answers:

3

Hello!

I've got the following example running in a simple Silverlight page:

public Page()
{
  InitializeComponent();
  InitializeOther();
}

private DoubleCollection dashes;

public DoubleCollection Dashes
{
  get
  {
    //dashes = new DoubleCollection(); //works ok
    //dashes.Add(2.0);
    //dashes.Add(2.0);

    if (dashes == null)
    {
      dashes = new DoubleCollection(); //causes exception
      dashes.Add(2.0);
      dashes.Add(2.0);
    }
    return dashes;
  }
  set
  {
    dashes = value;
  }
}

private void InitializeOther()
{
  Line line;
  for (int i = 0; i < 10; i++)
  {
    line = new Line();
    line.Stroke = new SolidColorBrush(Colors.Blue);
    line.StrokeDashArray = Dashes; //exception thrown here
    line.X1 = 10;
    line.Y2 = 10;
    line.X2 = 400;
    line.Y2 = 10 + (i * 40);
    canvas1.Children.Add(line);
  }
}

The above code throws a System.ArgumentException on the line marked. One solution to the problem is also marked in the example.

Does anybody know if this problem is related to the fact that the property System.Windows.Shapes.Shape.StrokeDashArray is a dependency property?

Many thanks for any hints!

A: 

The fact that StrokeDashArray is a dependency property shouldn't have anything to do with that code failing, since in XAML you are constantly setting dependency properties which are processed during parsing in InitializeComponent.

I would say that the problem is that in your code you are reusing the same double collection for each line. Whenever you try to set a children to different parents SL fails with an argument exception, same when you reuse a resource that is not a style. Seems like each line needs its own DoubleCollection.

Santiago Palladino
Mmm, I can reuse the same brush without a problem. Try creating a new brush property in the above code and you will see that the same issue doesn't occur. I don't think it's a generic problem with reuse. I think it could be a bug.
Shunyata Kharg
It works with brushes, it works with styles; it will not work with nearly everything else. I agree that it should be a bug since reusing a DoubleCollection does make sense, but it's the best explanation to your error at least.
Santiago Palladino
I see. At least I'm not the only one to have experienced this! Thank you for your time!
Shunyata Kharg
+1  A: 

Thank you for your answers and comments.

I can run exactly the same code in a WPF application and it does not fail. For me, this is a clear indication that it is a Silverlight bug. I don't now think it has anything to do with dependency properties.

Shunyata Kharg
A: 

I guess this real question is, what are you trying to do here? Do you really want all the lines to share the same DoubleCollection? Obviously you're probably doing a lot more and this is just a good way to share the question, but you should probably give each line its own collection. Pretty easy to do with:

line = new Line();    
line.Stroke = new SolidColorBrush(Colors.Blue);
line.StrokeDashArray = **new DoubleCollection() { 2.0, 2.0 };**   
line.X1 = 10;    
...

Do you really need to share the StoreDashArray between lines and then also expose it as a property on your class? I'd look into other ways of writing that code.

Bryant
Well, I was aware of the alternative as I said in my original post. I guess the point is that the code should work, but doesn't. In that sense I have no choice but to look into other ways of writing it :-)
Shunyata Kharg