void Page_Unloaded(object sender, RoutedEventArgs e) { if (inkRecognizer != null) { // Calling Dispose() on InkRecognizer to dispose of resources being used by HttpClient inkRecognizer.Dispose(); inkRecognizer = null; } // Dispose Win2D resources to avoid memory leak // Reference: https://microsoft.github.io/Win2D/html/RefCycles.htm var resultCanvas = this.FindName("resultCanvas") as CanvasControl; resultCanvas.RemoveFromVisualTree(); resultCanvas = null; }
protected override async void OnNavigatedTo(NavigationEventArgs e) { if (!previouslyLoaded) { var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/InkRecognizerSampleInstructions.gif")); if (file != null) { using (var stream = await file.OpenSequentialReadAsync()) { await inkCanvas.InkPresenter.StrokeContainer.LoadAsync(stream); } } previouslyLoaded = true; } // When the page is Unloaded, InkRecognizer and the Win2D CanvasControl are disposed. To preserve the state of the page we need to re-instantiate these objects. // In the case of the Win2D CanvasControl, a new UI Element needs to be created/appended to the page as well string endpoint = localSettings.Values["InkRecognizerEndpoint"] as string; string apiKey = localSettings.Values["InkRecognizerApiKey"] as string; if (string.IsNullOrEmpty(endpoint) || !Uri.IsWellFormedUriString(endpoint, UriKind.Absolute)) { await new MessageDialog("Ensure you have a valid URI set in the Settings page.", "Invalid or Missing URI").ShowAsync(); } else { inkRecognizer = new InkRecognizer(endpoint, apiKey); } var resultCanvas = new CanvasControl(); resultCanvas.Name = "resultCanvas"; resultCanvas.Draw += ResultCanvas_Draw; resultCanvas.SetValue(Grid.RowProperty, 0); resultCanvasGrid.Children.Add(resultCanvas); base.OnNavigatedTo(e); }