How can I properly insert InlineUIContainer into RichTextBox in code?

0
private void SetSelectedElementTo(TextElement text) {
    using(var s = new MemoryStream()) {
        new TextRange(text.ContentStart, text.ContentEnd).Save(s, DataFormats.XamlPackage, true);
        s.Seek(0, SeekOrigin.Begin);
        this.RichTextBox.Selection.Load(s, DataFormats.XamlPackage);
    }
}

I am currently trying to make a small text editor for specific use. Code above is part where I couldn't make it to work. This code was intended to insert InlineUIContainer with ContentPresenter inside into selection. But every time I run the code above, this weird exception happens:

Managed Debugging Assistant 'FatalExecutionEngineError' : 'The runtime has encountered a fatal error. The address of the error was at 0x68ac623f, on thread 0x1e30. The error code is 0x80131623. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.'

It seems any interaction with MemoryStream after TextRange.Save(...), in this case MemoryStream.Seek(...) causes FatalExecutionEngineError. TextElement in parameter is always type of InlineUIContainer, generated in code.

wpf
richtextbox
asked on Stack Overflow Oct 16, 2019 by Iwasa Mistake

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0