0

I am trying to create a note app myself. I want the app to have several pages, each with their own 'InkCanvas'. I've managed this, but whenever I leave the page the 'collection/list' of 'InkStrokes' gets cleared.

My solution is to create a list of 'InkStrokes'. Save that list in the pages.xaml.cs and then load that list into the 'InkCanvas' whenever the page is navigated onto again.

Here I am trying to first get the strokes and then add them back:

var strokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();

        foreach (var stroke in strokes)
        {
            inkCanvas.InkPresenter.StrokeContainer.AddStroke(stroke);

        }

I've also tried this without luck:

private IEnumerable<InkStroke> pageStrokes;

      pageStrokes = new IEnumerable<InkStroke> //Line continues
      inkCanvas.InkPresenter.StrokeContainer.GetStrokes();

      inkCanvas.InkPresenter.StrokeContainer.AddStrokes(pageStrokes);

I suspect my problem is due to mismatched types of data. It would be very helpful if anyone answering this could explain a little about a possible solution and why it works.

In advance, many thanks to you kind internet strangers.

Magnus
  • 5
  • 2

1 Answers1

1

My solution is to create a list of 'InkStrokes'. Save that list in the pages.xaml.cs and then load that list into the 'InkCanvas' whenever the page is navigated onto again.

You can save the StrokeContainer instead of saving Strokes. I made a basic demo and save the StrokeContainer to Application Resource and load them in OnNavigatedTo and it works.

MainPage.xaml.cs:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    //if the mainpage resource exists then load the container
    if (Application.Current.Resources.ContainsKey("mainpage"))
    {
        var resource = Application.Current.Resources["mainpage"];
        myCanvas.InkPresenter.StrokeContainer = (InkStrokeContainer)resource;
    }
}
//I save the StrokeContainer in a button click event
private void myBtn_Click(object sender, RoutedEventArgs e)
{
    Application.Current.Resources.Add(new KeyValuePair<object, object>("mainpage", myCanvas.InkPresenter.StrokeContainer));
}

And here is my demo project: SaveStrokeSample.

Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24
  • Thank you for this. Ironically I had just now found a solution and came to post my answer, but this one was so much cleaner and easier. Mine involved a separate strokecontainer, multiple lines for type conversions and just other mess. You're awesome! – Magnus Aug 25 '16 at 10:43