locked
Saving the Document property of RichEditBox to file and loading it back

    Question

  • Guys, I have a class like this---

    [DataContract]
        public class Notes : INotifyPropertyChanged
        {
            [DataMember]
            public ITextDocument document;
            

            public event PropertyChangedEventHandler PropertyChanged;

            protected void OnPropertyChanged(string property)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(property));
                }
            }


            public ITextDocument Document
            {
                get { return document; }
                set
                {
                    document = value;
                    OnPropertyChanged("Document");
                }
            }
            
        }

    I need to save the Document property of RichEditBox to file by serializing it...and when user opens the app again, the same object gets deserialized and shown to the app page i.e Document property of RichEditBox gets loaded with Document property of the class object that was saved previously. However, the Document property of RichEditBox is Read-Only and value cannot be assigned. So how to achieve that.... I want to do something like the following Pseudocode....

    private void Save_Button_Tapped(object sender, RoutedEventArgs e)

    {

      Notes obj = new Notes(){Document=richEdit.Document};

      SerializeAndSaveToFile(obj);

    private void Load_Button_Tapped(object sender, RoutedEventArgs e)

    {

    Notes obj = LoadAndDeserializeFromFile();

     richEditBox.Document = obj.Document; //However, this line is wrong...What should I write here?

    }

    NOTE :- I do NOT want to save the document as RTF doc...just a simple serialized file

    Friday, January 02, 2015 8:24 AM

All replies

  • This is driving me nuts. Why ain't anybody replying? It has been more than 2 days
    Sunday, January 04, 2015 4:55 PM
  • It is not only a weekend: it is a holiday weekend. Most people probably aren't watching the forums. 

    Your post is also poorly formatted and difficult to read. You'll get better help if you make it easy for people to help you.

    Lastly, what do you mean by saving it as a simple serialized file and not as an RTF doc? 

    You will need to extract the document data from the richEditBox and then save it somehow. Generally the best would be to save it as an RTF doc, but for small documents you could stick the RTF in a string in your data object and then serialize that.

    Sunday, January 04, 2015 6:13 PM
    Owner
  • Hey, I need to serialize the contents of a RichEditBox and store it in file (NOT RTF, a simple serialized RTF)

    I am using this class :- 

    [DataContract]
        public class Notes
        {
            [DataMember]
            public ITextDocument document { get; set; }
            [DataMember]
            public string title { get; set; }
            [DataMember]
            public DateTime date { get; set; }
            [DataMember]
            public string subject { get; set; }
    
        }

    And following is the code snippet for storing data

    Notes notes = new Notes()
                {
                    date=DateTime.Now,
                     title=notes_title_textbox.Text,
                      subject=Subject_textbox.Text
                };
    
               
                notes.document = editor.Document;
                
                AppSystem.Save(notes);

    And following is the snippet of saving

     public static async void Save(Notes notes)
            {
                ObservableCollection<Notes> temp = new ObservableCollection<Notes>();
                StorageFolder folder = ApplicationData.Current.LocalFolder;
                bool exists = await AppSystem.FileExists("Data");
               
                if(exists==true)
                {
                    StorageFile file = await folder.GetFileAsync("Data");
                    using(Stream stream = await file.OpenStreamForReadAsync())
                    {
                        DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(ObservableCollection<Notes>));
                        temp = (ObservableCollection<Notes>)json.ReadObject(stream);
                    }
                    await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
                }
              
                temp.Add(notes);
    
                StorageFile file1 = await folder.CreateFileAsync("Data", CreationCollisionOption.ReplaceExisting);
    
                using(Stream stream = await file1.OpenStreamForWriteAsync())
                {
                    DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(ObservableCollection<Notes>));
                    json.WriteObject(stream, temp);
                }
            }


    But I am receiving the exception

    An exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.dll but was not handled in user code

    Additional information: Type 'System.__ComObject' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.

    please please provide me a solution....

    Thursday, January 08, 2015 9:43 PM
  • please provide solution...i m in dire need of it
    Friday, January 09, 2015 8:33 AM
  • ITextDocument is not serializable. You will need to extract the document data from the richEditBox and then save it somehow. Generally the best would be to save it as an RTF doc, but for small documents you could stick the RTF in a string in your data object and then serialize that.
    Saturday, January 10, 2015 2:02 AM
    Owner
  • Hey....I have managed to extract the document data from RichEditBox and saved it as a string by serializing it. And then by deserialing it I lcan reload the data. This is my code snippet...

    private void Save_Tapped(object sender, RoutedEventArgs arg)
    {
      Notes notes = new Notes();
      string str=String.Empty;
      richEdit.Document.GetText(TextGetOptions.FormatRtf,out str);
      notes.doc = str;
      AppSystem.Save(notes);
    }
    
    private void Load_Tapped(object sender, RoutedEventArgs arg)
    {
      Notes note = AppSystem.GetData();
      richEditBox.Document.SetText(TextSetOption.FormatRtf,note.document);
    
    }

    However, the problem arises when texts are in different color. When I load the data from file and display it to RichEditBox, the font colors of the original document is getting lost. All texts are coming in black although Bold,Italics,Paragraph Formatting etc are preserved. How to solve this issue? Please help

    Saturday, January 10, 2015 11:03 PM