question

Jramos avatar image
0 Votes"
Jramos asked Jramos answered

Recover the last stroke "InkCanvas" after closing the App

Recover the last stroke "InkCanvas" after closing the App and when opening the last stroke drawn the inkCanvas

windows-uwp-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

danielescipioni avatar image
1 Vote"
danielescipioni answered danielescipioni commented

Given that the name of your InkCanvas is InkCanvas

<InkCanvas  x:Name="InkCanvas" />

To save at each stroke you can

  • use the StrokesCollected and StrokesErased events

  • use Application.Suspending event

Using StrokesCollected and StrokesErased this can be your Page.Loaded event handler:

private async void OnLoaded(object sender, RoutedEventArgs args)
{
    InkCanvas.InkPresenter.StrokesCollected += async (presenter, eventArgs) => { await Save(); };
    InkCanvas.InkPresenter.StrokesErased += async (presenter, eventArgs) => { await Save(); };
    await Load();
}


Using Application.Suspending this can be your Page.Loaded event handler:

private async void OnLoaded(object sender, RoutedEventArgs args)
{
    Application.Current.Suspending += async (o, suspendingEventArgs) =>
    {
        SuspendingDeferral deferral = suspendingEventArgs.SuspendingOperation.GetDeferral();
        await Save();
        deferral.Complete();
    };
    await Load();
}


the Save and Load methods:

private async Task Save()
{
    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("strokes.ink",
            CreationCollisionOption.ReplaceExisting);
    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
    {
        await InkCanvas.InkPresenter.StrokeContainer.SaveAsync(stream);
        await stream.FlushAsync();
    }
}

private async Task Load()
{
    IStorageItem storageItem = await ApplicationData.Current.LocalFolder.TryGetItemAsync("strokes.ink");
    if (!(storageItem is StorageFile storageFile)) return;
    using (IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.Read))
    {
        await InkCanvas.InkPresenter.StrokeContainer.LoadAsync(stream);
    }
}

I'm sure that file access in the Save method can be improved, anyway this works as a basic example.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ok is correct, but in the OnLoaded you have to remove the second line
InkCanvas.InkPresenter.StrokesErased +async (presenter, eventArgs) .

0 Votes 0 ·

InkCanvas.InkPresenter.StrokesErased needs when you delete a stroke. Without that if you add 2 stroke and remove the first stroke, you fail to save only the second stroke and you will load both strokes.

0 Votes 0 ·
Jramos avatar image
0 Votes"
Jramos answered

the problem with putting the second line with += is that it produces an exception therefore I would have less.

InkCanvas.InkPresenter.StrokesErased += async (presenter, eventArgs) .

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.