I'm dealing with the dreaded <Run/>
in Silverlight 3 and having to programmatically create a <TextBlock>
and its inlines:
Why dreaded? Because it doesn't work, I guess, the way you'd expect. Exhibit A, below, should produce
BARN(with fancy colors for each character), but instead it produces:
B A R N
EXHIBIT A
<TextBlock FontFamily="Comic Sans MS" FontSize="88">
<Run Foreground="#A200FF">B</Run>
<Run Foreground="#FF0000">A</Run>
<Run Foreground="#FFC000">R</Run>
<Run Foreground="#FFFF00">N</Run>
</TextBlock>
What does produce the desired result, however, is:
EXHIBIT B
<TextBlock FontFamily="Comic Sans MS" FontSize="88">
<Run Foreground="#A200FF">B</Run><Run Foreground="#FF0000">A</Run><Run Foreground="#FFC000">R</Run><Run Foreground="#FFFF00">N</Run>
</TextBlock>
Stupid, eh? Anyway, this is documented @ XAML Processing Differences Between Silverlight 3 and Silverlight 4 under Whitespace Handling where it says:
Silverlight 3 treats whitespace more literally in a wider range, including some cases where CLRF is considered significant. This sometimes led to file-format XAML with omitted CRLF in order to avoid unwanted whitespace in the presentation, but which was not human-readable in editing environments. Silverlight 4 uses a more intuitive significant-whitespace model that is similar to WPF. This model collapses file-formatting whitespace in most cases, with exception of certain CLR-attributed containers that treat all whitespace as significant. This whitespace model gives editing environments greater freedom to introduce whitespace that can improve XAML code formatting. Also, Silverlight 4 has text elements that permit even greater control over whitespace presentation issues.
Great, but I'm not using SL4 because I'm creating a WP7 app programmatically. Yeah, my XAML is generated. Using XML Literals. Then sent to a string. Like this:
Dim r1 As XElement = <Run Foreground="#A200FF">B</Run>
Dim r2 As XElement = <Run Foreground="#FF0000">A</Run>
Dim r3 As XElement = <Run Foreground="#FFC000">R</Run>
Dim r4 As XElement = <Run Foreground="#FFFF00">N</Run>
Dim tb = <TextBlock FontFamily="Comic Sans MS" FontSize="88">
<%= r1 %><%= r2 %><%= r3 %><%= r4 %>
</TextBlock>
Dim result = tb.ToString
After all this, here's my question: How can I produce Exhibit B instead of Exhibit A. That textblock will become part of a greater number of elements in a XAML page, so the .ToString
part isn't exactly accurate in this location - that happens when all of the XAML for the user control page is kicked out to file.
EDIT (6 May 2011): A little progress and a bounty
I've made a bit of progress as below, but I'm running up against a mental block here on how to accomplish an unusual split and processing of XML to output a string. Take this new example:
<Canvas>
<Grid>
<TextBlock>
<Run Text="r"/>
<Run Text="u"/>
<Run Text="n"/>
</TextBlock>
<TextBlock>
<Run Text="far a"/>
<Run Text="way"/>
<Run Text=" from me"/>
</TextBlock>
</Grid>
<Grid>
<TextBlock>
<Run Text="I"/>
<Run Text=" "/>
<Run Text="want"/>
<LineBreak/>
</TextBlock>
<TextBlock>
<LineBreak/>
<Run Text="...thi"/>
<Run Text="s to"/>
<LineBreak/>
<Run Text=" work"/>
</TextBlock>
</Grid>
</Canvas>
I want the output string to be:
<Canvas>
<Grid>
<TextBlock>
<Run Text="r"/><Run Text="u"/><Run Text="n"/>
</TextBlock>
<TextBlock>
<Run Text="far a"/><Run Text="way"/><Run Text=" from me"/>
</TextBlock>
</Grid>
<Grid>
<TextBlock>
<Run Text="I"/><Run Text=" "/><Run Text="want"/>
<LineBreak/>
</TextBlock>
<TextBlock>
<LineBreak/>
<Run Text="...thi"/><Run Text="s to"/>
<LineBreak/>
<Run Text=" work"/>
</TextBlock>
</Grid>
</Canvas>
I've been looking at the XMLWriter
and XMLWriterSettings
, based on Eric White's post, which seems to be a good start for the runs (not including the potential <LineBreak/>
s yet, which also stumps me). Like this:
Sub Main()
Dim myXML As XElement = <Canvas>
<Grid>
<TextBlock>
<Run Text="r"/>
<Run Text="u"/>
<Run Text="n"/>
</TextBlock>
<TextBlock>
<Run Text="far a"/>
<Run Text="way"/>
<Run Text=" from me"/>
</TextBlock>
</Grid>
</Canvas>
Console.Write(ToXMLString(myXML))
Console.ReadLine()
End Sub
Public Function ToXMLString(xml As XElement) As String
Dim tb As XElement = xml.Elements.<TextBlock>.FirstOrDefault
Dim xmlWriterSettings As New XmlWriterSettings
XmlWriterSettings.NewLineHandling = NewLineHandling.None
XmlWriterSettings.OmitXmlDeclaration = True
Dim sb As New StringBuilder
Using xmlwriter As XmlWriter = xmlwriter.Create(sb, XmlWriterSettings)
tb.WriteTo(xmlwriter)
End Using
Return sb.ToString
End Function
But I'm having a huge problem going much further with figuring out how to parse this to produce the desired output above.