Problems when re-assigning a PathGeometry in Silverlight
Warning: count(): Parameter must be an array or an object that implements Countable in /nfs/c03/h04/mnt/50561/domains/gergelyorosz.com/html/wp-content/plugins/wp-syntax/wp-syntax.php on line 76
I wasn’t planning in submitting (yet) another workaround for a Silverlight bug, but I stumbled across (yet) another annoying issue that causes no problems in WPF, but results in a exception being thrown in Silverlight. It definitely seems like another Silverlight bug still present in Silverlight 4.
The problem
I was building Paths by re-using a pre-built collection of PathGeometries. However when re-assigning a PathGeometry to another Path like this:
Path path1; Path path2; // These paths are in the visual tree // Create a PathGeometry with some points var points = new PointCollection(); points.Add(new Point(100, 0)); points.Add(new Point(100, 100)); points.Add(new Point(0, 100)); var pathFigure = new PathFigure(); pathFigure.StartPoint = new Point(0, 0); pathFigure.Segments.Add(new PolyLineSegment() { Points = points }); var pathGeometry = new PathGeometry(); pathGeometry.Figures.Add(pathFigure); // Assign this PathGeometry to both Paths path1.Data = pathGeometry; // No you don't you get an ArgumentException path2.Data = pathGeometry;
When trying to re-assign this PathGeometry, an ArgumentException was thrown with the message Value does not fall within the expected range. After googling around it turned out others have come across this issue back in mid 2008. This issue is only present in Silverlight, not WPF, which definititely suggests it is a bug.
However, having received a completely meaningless exception, I decided to dig deeper to at least try to understand what went wrong and find a workaround.
Read More…