How do you load and save content from a Silverlight 4 RichTextBox control?
I've been reviewing the features of the RichTextBox control in Silverlight 4.
What I've yet to find is any examples of Loading and Saving content in the RichTextBox.
Anyone come across any or can shed some light on it?
The control has a BlocksCollection in which I guess one could use the XamlReader
to load a bunch of markup assuming that markup has a single top level node of type Block
. Then add that block to the Blocks collection. Seems a shame that the RichTextBox bothers to have a "collection" in this case, why not 开发者_JAVA技巧simply a top-level Block
item?
Never-the-less that still leaves saving the content of a RichTextBox, I have no idea where to begin with that one?
I'm sure I must be missing the obvious here but unless loading and saving data in to and from RichTextBox is at least possible if not easy I can't see how we can actually put it to use.
Edit
Thanks to DaveB's answer I found discussion of something called the DocumentPersister
. However no reference to this class can be found in the MSDN documentation nor can I find it in the installed dlls via object browser search. Anyone, anyone at all?
Check out this tutorial on the RichTextArea control. Persisting content is described in exercise 2, task 3. The code for the tutorial includes a helper class.
Edit:
The question was raised about the DocumentPersister
class referenced in the tutorial. It is found in the source code download for the tutorial. I think the author created it. By looking at the code you will get an idea as to persisting your data. The only downside was that if your data contained images, the helper class did not support them. Here is the link to the download.
http://ecn.channel9.msdn.com/o9/learn/Silverlight4/Labs/TextEditor/Source.zip
Just to update the link in the accepted answer, it's moved to here: http://channel9.msdn.com/learn/courses/Silverlight4/NewFeatures/RichTextBox/Introduction/
Be wary of investing too much in the Silverlight 4 RichTextArea until it's confirmed that it will support full RichEdit functionality like bullet points/lists etc which it currently does in SL Beta 1... although I'm sure it will in RTM?
I have a sample from Microsoft to persist the contents which I have to find which I will do tomorrow. I got this sample in october when beta 4 was not even announces due to which there were no tutorials available. Since that project I have not worked on silverlight so I don't know how many tutorials are available now.
ok I have found it. Where should I upload it?
One option for loading text into RichTextBox is to use XamlReader. Dependent on the text which you are planning to load, you might need to add tag around it
public class TextToXamlConverter
{
private const String ParaHead = "<Paragraph xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">";
private const String ParaTail = "</Paragraph>";
static public Paragraph Convert(string text)
{
String formattedText = ParaHead + text + ParaTail;
Paragraph p = (Paragraph)XamlReader.Load(formattedText);
return p;
}
}
精彩评论