Browse Source

new feature: auto-render Adaptive Card output from inputted JSON with basic error trapping for bad JSON

master
Claire Davis 4 years ago
parent
commit
05725d193f
  1. 6
      Adaptive Card Editor UWP/Adaptive Card Editor UWP.csproj
  2. 30
      Adaptive Card Editor UWP/MainPage.xaml
  3. 74
      Adaptive Card Editor UWP/MainPage.xaml.cs

6
Adaptive Card Editor UWP/Adaptive Card Editor UWP.csproj

@ -150,6 +150,12 @@
</Page>
</ItemGroup>
<ItemGroup>
<PackageReference Include="AdaptiveCards">
<Version>1.2.4</Version>
</PackageReference>
<PackageReference Include="AdaptiveCards.Rendering.Uwp">
<Version>1.2.5</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.9</Version>
</PackageReference>

30
Adaptive Card Editor UWP/MainPage.xaml

@ -8,7 +8,35 @@
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid Padding="40">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="33*" />
<ColumnDefinition Width="33*" />
<ColumnDefinition Width="33*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0">
Adaptive Card JSON Input:
</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1">
Rendered Output (UWP Library):
</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="2">
Rendered Output (Template Library):
</TextBlock>
<TextBox Grid.Row="1" Grid.Column="0" x:Name="txtInput" Margin="20" KeyUp="txtInput_KeyUp" TextWrapping="Wrap"></TextBox>
<Border Grid.Row="1" BorderBrush="#777" BorderThickness="1" Grid.Column="1" Margin="20">
<Grid x:Name="grdCard">
</Grid>
</Border>
<Border Grid.Row="1" BorderBrush="#777" BorderThickness="1" Grid.Column="2" Margin="20">
<Grid x:Name="grdTemplated">
</Grid>
</Border>
</Grid>
</Page>

74
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
/// </summary>
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;
}
}
}
}
}

Loading…
Cancel
Save