Pregunta

Quiero guardar el contenido de un RichTextBox en varbinary (= matriz de bytes) en formato XamlPackage. Necesito asesoramiento técnico sobre cómo hacerlo.

Realmente necesito saber cómo convertir entre FlowDocument a una matriz de bytes.

¿Se recomienda incluso almacenarlo como varbinary, o esta es una mala idea?


Update

Fragmento de código:

///Load
byte[] document = GetDocumentFromDataBase();
RickTextBox tb = new RickTextBox();
TextRange tr = new TextRange(tb.Document.ContentStart, tb.Document.ContentEnd)
tr.Load(--------------------------) //Load from the byte array.


///Save
int maxAllowed = 1024;
byte[] document;
RichTextBox tb = new RichTextBox();
//User entered text and designs in the rich text
TextRange tr = new TextRange(tb.Document.ContentStart, tb.Document.ContentEnd)   
tr.Save(--------------------------) //Save to byte array
if (document.Length > maxAllowed) 
{
    MessageBox.Show((document.Length - maxAllowed) + " Exceeding limit.");
    return;
}
SaveToDataBase();
TextRange
¿Fue útil?

Solución

No puedo encontrar mi ejemplo completo en este momento, pero puedes usar XamlReader y XamlWriter para que el documento entre y salga de una cadena. A partir de ahí, puede usar UnicodeEncoding, AsciiEncoding o cualquier codificador que desee que entre y salga de bytes.

Mi ejemplo más corto para configurar el documento desde una cadena ... docReader es mi lector de documentos de flujo

        private void SetDetails(string detailsString)
    {
        if (docReader == null)
            return;
        if (String.IsNullOrEmpty(detailsString))
        {
            this.docReader.Document = null;
            return;
        }
        using (
        StringReader stringReader = new StringReader(detailsString))
        {
            using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(stringReader))
            {
                this.docReader.Document = XamlReader.Load(reader) as FlowDocument;
            }
        }
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top