I am sure this will turn out to be something simple. I have the following Silverlight 4 C# code:
Rectangle r = new Rectangle();
r.Stroke = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
r.SetValue(Canvas.LeftProperty, 150f);
r.SetValue(Canvas.TopProperty, 50f);
r.Width = 100;
r.Height = 100;
LayoutRoot.Children.Add(r);
For some reason, when I run my application, it is getting an error on the SetValue lines. The error I am getting is:
Uncaught Error: Unhandled Error in Silverlight Application DependencyProperty of type System.Double cannot be set on an object of type System.Single.
I tried to cast implicitly to Single, but still got the same error. Any ideas?
You’re passing in a boxed float, and the rectangle is then trying to unbox it to a double. Just pass in doubles to start with, and it should be fine:
Note that
Canvas.LeftandCanvas.Topare of typedouble, notfloat.