In Silverlight I have just discovered by trail by fire the you should not reuse Brushes. Seems really odd coming from a Win32 GDI background to not use the same brush but the following code throws an exception on the second assignment.
SolidColorBrush green = new SolidColorBrush(); green.Color = Colors.Green; Run ra = new Run(); Run rb = new Run(); ra.Text = "Run A"; rb.Text = "Run B"; ra.Foreground = green; rb.Foreground = green;
With this
Value does not fall within the expected range.
at MS.Internal.XcpImports.SetValue[T](IntPtr oPtr, UInt32 iPropertyId, T obj) at System.Windows.DependencyObject.SetValue[T](DependencyProperty property, T obj at System.Windows.Documents.Inline.set_Foreground(Brush value)
Hmm, what means lots to me…
So I changed how I was wiring it all up and found a better SolidColorBrush constructor as well. So it should be done like
Run ra = new Run();Run rb = new Run(); ra.Text = "Run A"; rb.Text = "Run B"; ra.Foreground = new SolidColorBrush(Colors.Green); rb.Foreground = new SolidColorBrush(Colors.Green);
Which feels wrong, but works. I’m guess as Silverlight is not GDI based there are no window handles the manage, so it works better this way.