using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Runtime.InteropServices.WindowsRuntime ;
using Windows.Foundation ;
using Windows.Foundation.Collections ;
using Windows.UI.Xaml ;
using Windows.UI.Xaml.Controls ;
using Windows.UI.Xaml.Controls.Primitives ;
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
namespace Adaptive_Card_Editor_UWP
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public DispatcherTimer keyTimer = new DispatcherTimer ( ) ;
public long lastKeyUp ;
public bool isRendered ;
public long interval = 5 0 0 0 0 0 0 ;
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 ( 1 0 ) ;
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 ;
}
}
}
}
}