From 0203e3c4b8f241f5ccf1ed21766c06f7faf5e81b Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Sun, 10 Dec 2017 20:37:16 -0800 Subject: [PATCH] Add cancellation logic to preview --- Samples/XamlPreviewPage.xaml.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Samples/XamlPreviewPage.xaml.cs b/Samples/XamlPreviewPage.xaml.cs index 9059a88..21f9318 100644 --- a/Samples/XamlPreviewPage.xaml.cs +++ b/Samples/XamlPreviewPage.xaml.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; - +using System.Threading; using Xamarin.Forms; namespace Samples @@ -37,19 +37,33 @@ namespace Samples DisplayXaml (); } + CancellationTokenSource lastCts = null; + public void DisplayXaml () { try { + var cts = new CancellationTokenSource (); + var token = cts.Token; + lastCts?.Cancel (); + lastCts = cts; + var asm = typeof (Xamarin.Forms.Xaml.Internals.XamlTypeResolver).Assembly; var xamlLoaderType = asm.GetType ("Xamarin.Forms.Xaml.XamlLoader"); var loadArgTypes = new[] { typeof (object), typeof (string) }; var loadMethod = xamlLoaderType.GetMethod ("Load", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public, null, System.Reflection.CallingConventions.Any, loadArgTypes, null); var contentView = new ContentView (); loadMethod.Invoke (null, new object[] { contentView, editor.Text }); - results.Content = contentView; + + if (!token.IsCancellationRequested) { + results.Content = contentView; + } + } + catch (OperationCanceledException) { } catch (Exception ex) { results.Content = new Label { + TextColor = Color.DarkRed, + FontSize = 12, Text = ex.ToString (), }; }