From 05725d193f489105e0ce855a27df240e62e455f0 Mon Sep 17 00:00:00 2001 From: Claire Davis Date: Sun, 19 Jan 2020 16:10:19 -0800 Subject: [PATCH] new feature: auto-render Adaptive Card output from inputted JSON with basic error trapping for bad JSON --- .../Adaptive Card Editor UWP.csproj | 6 ++ Adaptive Card Editor UWP/MainPage.xaml | 30 +++++++- Adaptive Card Editor UWP/MainPage.xaml.cs | 74 +++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) diff --git a/Adaptive Card Editor UWP/Adaptive Card Editor UWP.csproj b/Adaptive Card Editor UWP/Adaptive Card Editor UWP.csproj index 7184169..f76e996 100644 --- a/Adaptive Card Editor UWP/Adaptive Card Editor UWP.csproj +++ b/Adaptive Card Editor UWP/Adaptive Card Editor UWP.csproj @@ -150,6 +150,12 @@ + + 1.2.4 + + + 1.2.5 + 6.2.9 diff --git a/Adaptive Card Editor UWP/MainPage.xaml b/Adaptive Card Editor UWP/MainPage.xaml index 6a11f90..9149cc9 100644 --- a/Adaptive Card Editor UWP/MainPage.xaml +++ b/Adaptive Card Editor UWP/MainPage.xaml @@ -8,7 +8,35 @@ mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> - + + + + + + + + + + + + Adaptive Card JSON Input: + + + Rendered Output (UWP Library): + + + Rendered Output (Template Library): + + + + + + + + + + + diff --git a/Adaptive Card Editor UWP/MainPage.xaml.cs b/Adaptive Card Editor UWP/MainPage.xaml.cs index 112dab4..8dd4ede 100644 --- a/Adaptive Card Editor UWP/MainPage.xaml.cs +++ b/Adaptive Card Editor UWP/MainPage.xaml.cs @@ -12,6 +12,8 @@ using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; +using AdaptiveCards.Rendering.Uwp; +using System.Threading.Tasks; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 @@ -22,9 +24,81 @@ namespace Adaptive_Card_Editor_UWP /// public sealed partial class MainPage : Page { + public DispatcherTimer keyTimer = new DispatcherTimer(); + + public long lastKeyUp; + + public bool isRendered; + + public long interval = 5000000; + public MainPage() { + TimerSetup(); this.InitializeComponent(); } + + public void TimerSetup() + { + keyTimer.Tick += TimerTick; + // keyTimer.Interval = new TimeSpan(0, 0, 1); + keyTimer.Interval = TimeSpan.Parse("00:00:00.05"); + keyTimer.Start(); + } + + private void txtInput_KeyUp(object sender, KeyRoutedEventArgs e) + { + // last key up event = integer value of current time + lastKeyUp = DateTime.Now.Ticks; + + // user is inputting text, so we're going to rerender + isRendered = false; + } + + void TimerTick(object sender, object args) + { + if (isRendered) + { + return; + } + else + { + if (DateTime.Now.Ticks >= lastKeyUp + interval ) + { + // we done got some input! let's render it. + // we don't care what the input is; if it's not valid JSON ignore this keystroke + + // render the text as a plain textbox + TextBlock txtOutput = new TextBlock(); + txtOutput.TextWrapping = TextWrapping.Wrap; + txtOutput.Padding = new Thickness(10); + txtOutput.Text = txtInput.Text; + + // clear the grid of existing content + grdCard.Children.Clear(); + + // render an adaptive card + try + { + AdaptiveCardRenderer cardRenderer = new AdaptiveCardRenderer(); + AdaptiveCardParseResult parsedCard = AdaptiveCard.FromJsonString(txtInput.Text); + RenderedAdaptiveCard theCard = cardRenderer.RenderAdaptiveCard(parsedCard.AdaptiveCard); + grdCard.Children.Add(theCard.FrameworkElement); + } + catch (Exception ex) + { + txtOutput.Text = ex.ToString(); + grdCard.Children.Add(txtOutput); + } + grdCard.UpdateLayout(); + + isRendered = true; + } + else + { + return; + } + } + } } }