简体   繁体   中英

Storing InkCanvas in custom class (?)

I'm writing a simple application for my students and I'm stuck in a middle of my work. So, here's what is it about - it's a simple app for managing also very simple notebooks. Each notebook contains a textbox and InkCanvas, in which users can draw simple shapes. Clicking on a desired notebook binds it to țe fields in app, so they're updated in fly. Saving is done via xml serialization.

Notebook is a custom class, and creating a new one, creates a new instance of this class. Here's the code:

namespace Notes.Classes
{
[Serializable]
public class Notebook : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    [XmlAttribute("Notebook")]
    private string Name;

    [XmlElement("Notebook content")]
    private string Content;

    private DateTime Date;


    #region OnPropertyChanged
    protected void OnPropertyChanged(string info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }
    #endregion

    #region Constructors
    public Notebook(string Name, string Content)
    {
        this.Name = Name;
        this.Date = DateTime.Now;
        if (Content == "")
            this.Content = "Write something";
        else
            this.Content = Content;
    }

    //This is never used private parameterless constructor created only because Serialization required one
    //It is private, so it can't create new notebooks when not needed
    private Notebook()
    {
        this.Name = "bez nazwy";
        this.Content = "bez zawartości";
    }

    #endregion

    [XmlElement("Notebook date")]
    public string GetDate
    {
        get { return Date.ToString(); }
        set { Date = DateTime.Parse(value); OnPropertyChanged("Date"); }
    }

    public string GetName
    {
        get { return Name; }
        set { Name = value; OnPropertyChanged("Name"); }
    }
    public string GetContent
    {
        get { return Content; }
        set { Content = value; OnPropertyChanged("Content"); }
    }

    public string GetPath
    {
        get { return InkPath;}
        set { InkPath = value; OnPropertyChanged("InkPath"); }
    }
   }
 }

Now what I need done is somehow vinding InkCanvas Strokes to a property of the class, saving it each time I close the app and loading everytime I choose a different notebook. It would be soo cool if I could simpky serialize it. Nothing fancy in the onkcanvas, no image uploading or bitmaps. Any quick and efficient way to do this?

You can simply save the StrokeCollection from your InkCanvas . The StrokeCollection Class has an overloaded Save method that you can use to save the Stroke data to a Stream . From the linked Save method page:

FileStream fs = null;

try
{
    fs = new FileStream(inkFileName, FileMode.Create);
    inkCanvas1.Strokes.Save(fs);
}
finally
{
    if (fs != null)
    {
        fs.Close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM