簡體   English   中英

在 WPF 中序列化和反序列化 RichTextBox 時出現問題

[英]Problem when serializing and de-serializing RichTextBox in WPF

我有一個實驗性的 WPF probject,比如 Microsoft Word。 主要組件是RichTextBox 用戶可以編輯內容並將其保存到某個地方。 以下方法用於對遠程數據庫的內容進行序列化和反序列化。

public static byte[] FlowDocumentSerializingV2WithXamlWriter(FlowDocument flowDocument) {
    using(MemoryStream buffer = new MemoryStream()) {
        XamlWriter.Save(flowDocument, buffer);
        return buffer.ToArray();
    }
}

我使用上述方法序列化RichTextBox.Document 此方法返回一個byte[] 我將byte[]結果保存到遠程數據庫。 稍后,我們可以從遠程數據庫中讀取這個byte[]數據,然后使用下面的方法將其加載到RichTextBox中。

public FlowDocument FlowDocumentDeserializingV2WithXamlLoader(byte[] data) {
    MemoryStream ms = new MemoryStream(data);          
    return (FlowDocument)XamlReader.Load(ms);
}

如果FlowDocument中沒有圖像,則上述方法效果很好。 問題是,當在FlowDocument中粘貼任何圖片時, FlowDocumentDeserializingV2WithXamlLoader(byte[] data)將在return (FlowDocument)XamlReader.Load(ms)行上拋出異常。 例外是:

System.Windows.Markup.XamlParseException

H結果=0x80131501
消息=''System.Windows.Media.Imaging.BitmapImage'的初始化引發異常。' 行號 '1' 和行 position '123355'。
來源=PresentationFramework
StackTrace:在 System.Windows.Markup.XamlReader.RewrapException(異常 e,IXamlLineInfo lineInfo,Uri baseUri)

此異常最初是在此調用堆棧中引發的:

 System.Net.WebRequest.Create(System.Uri, bool) MS.Internal.WpfWebRequestHelper.CreateRequest(System.Uri) System.IO.Packaging.PackWebRequest.GetRequest(bool) System.IO.Packaging.PackWebRequest.GetResponse() MS.Internal.WpfWebRequestHelper.GetResponse(System.Net.WebRequest) System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(System.Uri, System.IO.Stream, System.Windows.Media.Imaging.BitmapCacheOption, out System.Guid, out bool, out System.IO.Stream, out System.IO.UnmanagedMemoryStream, out Microsoft.Win32.SafeHandles.SafeFileHandle) System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(System.Uri, System.Uri, System.IO.Stream, System.Windows.Media.Imaging.BitmapCreateOptions, System.Windows.Media.Imaging.BitmapCacheOption, System.Net.Cache.RequestCachePolicy, bool) System.Windows.Media.Imaging.BitmapImage.FinalizeCreation() System.Windows.Media.Imaging.BitmapImage.EndInit() MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(System.Xaml.XamlType, object, bool)

內部異常1:
NotSupportedException:無法識別 URI 前綴。

我不知道如何解決這個問題。 有誰能夠幫我? 任何幫助都值得贊賞。

我創建了一個項目來在 Github 中演示此問題: https://github.com/apchee/richtextbox

readme.md中的圖像未顯示,但應顯示在本地計算機中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM