<commit_before>﻿using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Samples
{
    public partial class XamlPreviewPage : ContentPage
    {
        public XamlPreviewPage ()
        {
            InitializeComponent ();

            editor.Text = @"<ContentView
  xmlns=""http://xamarin.com/schemas/2014/forms""
  xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"">
<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height=""*"" />
    <RowDefinition Height=""*"" />
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width=""*"" />
    <ColumnDefinition Width=""*"" />
  </Grid.ColumnDefinitions>
  <StackLayout Grid.Row=""0"" Grid.Column=""0"">
    <Label Text=""Top Left"" />
    <Entry Placeholder=""I'm ready for some text"" />
    <Button Text=""I'm a button, but I don't do anything"" />
  </StackLayout>
  <Label Text=""Top Right"" Grid.Row=""0"" Grid.Column=""1"" TextColor=""White"" BackgroundColor=""#c5000b"" />
  <Label Text=""Bottom Left"" Grid.Row=""1"" Grid.Column=""0"" TextColor=""Black"" BackgroundColor=""#ffd320"" />
  <Label Text=""Bottom Right"" Grid.Row=""1"" Grid.Column=""1"" TextColor=""White"" BackgroundColor=""#008000"" />
</Grid>
</ContentView>";
            editor.TextChanged += (sender, e) => DisplayXaml ();
            DisplayXaml ();
        }

        public void DisplayXaml ()
        {
            try {
                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;
            }
            catch (Exception ex) {
                results.Content = new Label {
                    Text = ex.ToString (),
                };
            }
        }
    }
}<commit_msg>Add cancellation logic to preview<commit_after>﻿using System;
using System.Collections.Generic;
using System.Threading;
using Xamarin.Forms;

namespace Samples
{
    public partial class XamlPreviewPage : ContentPage
    {
        public XamlPreviewPage ()
        {
            InitializeComponent ();

            editor.Text = @"<ContentView
  xmlns=""http://xamarin.com/schemas/2014/forms""
  xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"">
<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height=""*"" />
    <RowDefinition Height=""*"" />
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width=""*"" />
    <ColumnDefinition Width=""*"" />
  </Grid.ColumnDefinitions>
  <StackLayout Grid.Row=""0"" Grid.Column=""0"">
    <Label Text=""Top Left"" />
    <Entry Placeholder=""I'm ready for some text"" />
    <Button Text=""I'm a button, but I don't do anything"" />
  </StackLayout>
  <Label Text=""Top Right"" Grid.Row=""0"" Grid.Column=""1"" TextColor=""White"" BackgroundColor=""#c5000b"" />
  <Label Text=""Bottom Left"" Grid.Row=""1"" Grid.Column=""0"" TextColor=""Black"" BackgroundColor=""#ffd320"" />
  <Label Text=""Bottom Right"" Grid.Row=""1"" Grid.Column=""1"" TextColor=""White"" BackgroundColor=""#008000"" />
</Grid>
</ContentView>";
            editor.TextChanged += (sender, e) => DisplayXaml ();
            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 });

                if (!token.IsCancellationRequested) {
                    results.Content = contentView;
                }
            }
            catch (OperationCanceledException) {
            }
            catch (Exception ex) {
                results.Content = new Label {
                    TextColor = Color.DarkRed,
                    FontSize = 12,
                    Text = ex.ToString (),
                };
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.1492088924494292, 'fraction_numerical': 0.012016823552974164, 'mean_word_length': 2.4135338345864663, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3198172', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1362, 'n_words': 291}