protected override void Render() { if (CardJson == null) { CardFrameworkElement = null; return; } try { var renderer = new AdaptiveCardRenderer(); if (HOST_CONFIG_JSON != null) { var hostParseResult = AdaptiveHostConfig.FromJsonString(HOST_CONFIG_JSON); if (hostParseResult.HostConfig != null) { renderer.HostConfig = hostParseResult.HostConfig; } } var renderedCard = renderer.RenderAdaptiveCardFromJsonString(CardJson); CardFrameworkElement = renderedCard.FrameworkElement; } catch { } }
protected override void LoadPayload(string payload) { try { var newHostConfig = AdaptiveHostConfig.FromJsonString(payload).HostConfig; if (newHostConfig != null) { HostConfig = newHostConfig; HostConfigChanged?.Invoke(this, HostConfig); MakeErrorsLike(new List <ErrorViewModel>()); } else { SetSingleError(new ErrorViewModel() { Message = "Invalid Host Config payload", Type = ErrorViewModelType.ErrorButRenderAllowed }); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); SetSingleError(new ErrorViewModel() { Message = ex.ToString(), Type = ErrorViewModelType.ErrorButRenderAllowed }); } }
protected override AdaptiveCardRenderer CreateObject(string str) { var hostConfig = AdaptiveHostConfig.FromJsonString(str).HostConfig; return(new AdaptiveCardRenderer() { HostConfig = hostConfig }); }
private async void TriggerUpdate() { if (_isUpdating) { return; } _isUpdating = true; try { if (_renderer == null) { _renderer = new AdaptiveCardRenderer(); _renderer.SetFixedDimensions(320, 176); var hostConfigsFolder = await Package.Current.InstalledLocation.GetFolderAsync("HostConfigs"); var hostConfigFile = await hostConfigsFolder.GetFileAsync("TimelineWindows.json"); _renderer.HostConfig = AdaptiveHostConfig.FromJsonString(await FileIO.ReadTextAsync(hostConfigFile)).HostConfig; } if (CardPayload == null) { RenderedElement = null; RenderedSuccessfully = false; } else { var renderResult = _renderer.RenderAdaptiveCardFromJsonString(CardPayload.ToString()); RenderedElement = renderResult.FrameworkElement; RenderedSuccessfully = RenderedElement != null; } } catch { RenderedElement = null; RenderedSuccessfully = false; } finally { _isUpdating = false; } }
internal static RenderedAdaptiveCard RenderCard(AdaptiveCard card, TypedEventHandler <RenderedAdaptiveCard, AdaptiveActionEventArgs> actionHandler = null) { try { var renderer = SharedAdaptiveCardRenderer.Create(); var inputTextRenderer = renderer.ElementRenderers.Get("Input.Text"); renderer.ElementRenderers.Set("Input.Text", new ActionCenterInputTextRenderer(inputTextRenderer)); // Constructing HostConfig from object model isn't supported yet, have to use JSON var hostConfigResult = AdaptiveHostConfig.FromJsonString(HOST_CONFIG); if (hostConfigResult.HostConfig != null) { renderer.HostConfig = hostConfigResult.HostConfig; } else { throw new Exception("HostConfig failed to parse"); } var result = renderer.RenderAdaptiveCard(card); if (result.FrameworkElement == null) { throw new Exception("Failed to render card"); } if (actionHandler != null) { // Wire up action click handler result.Action += actionHandler; } return(result); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); PreviewToast.SendRenderingError(ex); return(null); } }
public static async Task <RenderedTestResult> RenderCard(FileViewModel cardFile, FileViewModel hostConfigFile, Dictionary <string, IAdaptiveCardResourceResolver> resourceResolvers) { string error = null; string roundTrippedJsonString = null; FrameworkElement xaml = null; double cardWidth = 400; WeakReference weakRefCard = null; try { AdaptiveHostConfig hostConfig = AdaptiveHostConfig.FromJsonString(hostConfigFile.Contents).HostConfig; if (hostConfig == null) { error = "Parsing hostConfig failed"; } else { AdaptiveCard card = AdaptiveCard.FromJsonString(cardFile.Contents).AdaptiveCard; if (card == null) { error = "Parsing card failed"; } else { roundTrippedJsonString = card.ToJson().ToString(); card = AdaptiveCard.FromJsonString(roundTrippedJsonString).AdaptiveCard; AdaptiveFeatureRegistration featureRegistration = new AdaptiveFeatureRegistration(); featureRegistration.Set("acTest", "1.0"); var renderer = new AdaptiveCardRenderer() { HostConfig = hostConfig, FeatureRegistration = featureRegistration }; foreach (var resourceResolver in resourceResolvers) { renderer.ResourceResolvers.Set(resourceResolver.Key, resourceResolver.Value); } if (hostConfigFile.Name.Contains("windows-timeline")) { renderer.SetFixedDimensions(320, 180); cardWidth = 320; } else if (hostConfigFile.Name.Contains("windows-live-tile")) { renderer.SetFixedDimensions(310, 310); cardWidth = 310; } RenderedAdaptiveCard renderedCard = renderer.RenderAdaptiveCard(card); weakRefCard = new WeakReference(renderedCard); xaml = renderedCard.FrameworkElement as FrameworkElement; if (xaml == null) { error = "Rendering card failed"; } else { xaml = new Border() { Background = new SolidColorBrush(Colors.White), Child = xaml, IsHitTestVisible = false // Disable HitTest so that mouse pointer can't accidently hover over a button }; // The theme is important to set since it'll ensure buttons/inputs appear correctly if (hostConfigFile.Name.Contains("windows-notification")) { xaml.RequestedTheme = ElementTheme.Dark; } else { xaml.RequestedTheme = ElementTheme.Light; } } } } } catch (Exception ex) { error = ex.ToString(); } return(new RenderedTestResult { Error = error, RoundTrippedJSON = roundTrippedJsonString, Tree = xaml, CardWidth = cardWidth, WeakCard = weakRefCard }); }
private async Task <Tuple <string, StorageFile, StorageFile, UIElement> > RenderCard(FileViewModel cardFile, FileViewModel hostConfigFile) { string error = null; RenderTargetBitmap rtb = null; string roundTrippedJsonString = null; try { AdaptiveHostConfig hostConfig = AdaptiveHostConfig.FromJsonString(hostConfigFile.Contents).HostConfig; if (hostConfig == null) { error = "Parsing hostConfig failed"; } else { AdaptiveCard card = AdaptiveCard.FromJsonString(cardFile.Contents).AdaptiveCard; if (card == null) { error = "Parsing card failed"; } else { roundTrippedJsonString = card.ToJson().ToString(); card = AdaptiveCard.FromJsonString(roundTrippedJsonString).AdaptiveCard; var renderer = new AdaptiveCardRenderer() { HostConfig = hostConfig }; if (hostConfigFile.Name.Contains("windows-timeline")) { renderer.SetFixedDimensions(320, 180); CurrentCardVisualWidth = 320; } else if (hostConfigFile.Name.Contains("windows-live-tile")) { renderer.SetFixedDimensions(310, 310); CurrentCardVisualWidth = 310; } else { CurrentCardVisualWidth = 400; } FrameworkElement xaml = renderer.RenderAdaptiveCard(card).FrameworkElement as FrameworkElement; if (xaml == null) { error = "Rendering card failed"; } else { xaml = new Border() { Background = new SolidColorBrush(Colors.White), Child = xaml, IsHitTestVisible = false // Disable HitTest so that mouse pointer can't accidently hover over a button }; // The theme is important to set since it'll ensure buttons/inputs appear correctly if (hostConfigFile.Name.Contains("windows-notification")) { xaml.RequestedTheme = ElementTheme.Dark; } else { xaml.RequestedTheme = ElementTheme.Light; } CurrentCardVisual = xaml; ExpandShowCards(xaml); NormalizeTimePickers(xaml); await WaitOnAllImagesAsync(xaml); rtb = new RenderTargetBitmap(); await rtb.RenderAsync(CurrentCardVisual); } } } } catch (Exception ex) { error = ex.ToString(); } StorageFile file = null; StorageFile file2 = null; if (error == null) { file = await _tempResultsFolder.CreateFileAsync("Result.png", CreationCollisionOption.GenerateUniqueName); file2 = await _tempResultsFolder.CreateFileAsync("Result.json", CreationCollisionOption.GenerateUniqueName); // https://basquang.wordpress.com/2013/09/26/windows-store-8-1-save-visual-element-to-bitmap-image-file/ var buffer = await rtb.GetPixelsAsync(); using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)rtb.PixelWidth, (uint)rtb.PixelHeight, 96, 96, buffer.ToArray()); // Set the size of the card so that it can be rendered just like the bitmap if (CurrentCardVisual is FrameworkElement fe) { fe.Width = rtb.PixelWidth; fe.Height = rtb.PixelHeight; } await encoder.FlushAsync(); } if (roundTrippedJsonString != null) { await Windows.Storage.FileIO.WriteTextAsync(file2, roundTrippedJsonString); } } return(new Tuple <string, StorageFile, StorageFile, UIElement>(error, file, file2, CurrentCardVisual)); }
public static async Task <Tuple <string, string, UIElement, double> > RenderCard(FileViewModel cardFile, FileViewModel hostConfigFile) { string error = null; string roundTrippedJsonString = null; FrameworkElement xaml = null; double cardWidth = 400; try { AdaptiveHostConfig hostConfig = AdaptiveHostConfig.FromJsonString(hostConfigFile.Contents).HostConfig; if (hostConfig == null) { error = "Parsing hostConfig failed"; } else { AdaptiveCard card = AdaptiveCard.FromJsonString(cardFile.Contents).AdaptiveCard; if (card == null) { error = "Parsing card failed"; } else { roundTrippedJsonString = card.ToJson().ToString(); card = AdaptiveCard.FromJsonString(roundTrippedJsonString).AdaptiveCard; var renderer = new AdaptiveCardRenderer() { HostConfig = hostConfig }; if (hostConfigFile.Name.Contains("windows-timeline")) { renderer.SetFixedDimensions(320, 180); cardWidth = 320; } else if (hostConfigFile.Name.Contains("windows-live-tile")) { renderer.SetFixedDimensions(310, 310); cardWidth = 310; } xaml = renderer.RenderAdaptiveCard(card).FrameworkElement as FrameworkElement; if (xaml == null) { error = "Rendering card failed"; } else { xaml = new Border() { Background = new SolidColorBrush(Colors.White), Child = xaml, IsHitTestVisible = false // Disable HitTest so that mouse pointer can't accidently hover over a button }; // The theme is important to set since it'll ensure buttons/inputs appear correctly if (hostConfigFile.Name.Contains("windows-notification")) { xaml.RequestedTheme = ElementTheme.Dark; } else { xaml.RequestedTheme = ElementTheme.Light; } } } } } catch (Exception ex) { error = ex.ToString(); } return(new Tuple <string, string, UIElement, double>(error, roundTrippedJsonString, xaml, cardWidth)); }
public static async Task <RenderedTestResult> RenderCard(FileViewModel cardFile, FileViewModel hostConfigFile) { string error = null; string roundTrippedJsonString = null; FrameworkElement xaml = null; double cardWidth = 400; WeakReference weakRefCard = null; try { AdaptiveHostConfig hostConfig = null; if (hostConfigFile.Contents != null) { hostConfig = AdaptiveHostConfig.FromJsonString(hostConfigFile.Contents).HostConfig; if (hostConfig == null) { error = "Parsing hostConfig failed"; } } if (error == null) { AdaptiveCard card = AdaptiveCard.FromJsonString(cardFile.Contents).AdaptiveCard; if (card == null) { error = "Parsing card failed"; } else { roundTrippedJsonString = card.ToJson().ToString(); card = AdaptiveCard.FromJsonString(roundTrippedJsonString).AdaptiveCard; AdaptiveFeatureRegistration featureRegistration = new AdaptiveFeatureRegistration(); featureRegistration.Set("acTest", "1.0"); var renderer = new AdaptiveCardRenderer() { FeatureRegistration = featureRegistration }; if (hostConfig != null) { renderer.HostConfig = hostConfig; } renderer.ResourceResolvers.Set("symbol", new SampleResourceResolver()); if (hostConfigFile.Name.Contains(FileLoadHelpers.fixedNonInteractiveName)) { renderer.SetFixedDimensions(320, 180); cardWidth = 320; renderer.HostConfig.SupportsInteractivity = false; } RenderedAdaptiveCard renderedCard = renderer.RenderAdaptiveCard(card); weakRefCard = new WeakReference(renderedCard); xaml = renderedCard.FrameworkElement as FrameworkElement; if (xaml == null) { error = "Rendering card failed"; } else { xaml = new Border() { Background = new SolidColorBrush(Colors.White), Child = xaml, IsHitTestVisible = false // Disable HitTest so that mouse pointer can't accidently hover over a button }; // The theme is important to set since it'll ensure buttons/inputs appear correctly if (hostConfigFile.Name.Contains(FileLoadHelpers.testVarientHostConfigName)) { xaml.RequestedTheme = ElementTheme.Dark; } else { xaml.RequestedTheme = ElementTheme.Light; } } } } } catch (Exception ex) { error = ex.ToString(); } return(new RenderedTestResult { Error = error, RoundTrippedJSON = roundTrippedJsonString, Tree = xaml, CardWidth = cardWidth, WeakCard = weakRefCard }); }
private static RenderedAdaptiveCard Render(string cardJson, bool hasActions, TypedEventHandler <RenderedAdaptiveCard, AdaptiveActionEventArgs> actionHandler = null) { try { var renderer = new AdaptiveCardRenderer(); // Constructing HostConfig from object model isn't supported yet, have to use JSON var hostConfigResult = AdaptiveHostConfig.FromJsonString(HOST_CONFIG); if (hostConfigResult.HostConfig != null) { renderer.HostConfig = hostConfigResult.HostConfig; } else { throw new Exception("HostConfig failed to parse"); } //renderer.SetHostConfig(new AdaptiveHostConfig() //{ // AdaptiveCard = new AdaptiveCardConfig() // { // BackgroundColor = Colors.Transparent, // Padding = new AdaptiveSpacingDefinition() // { // Bottom = 0, // Left = 0, // Right = 0, // Top = 0 // } // }, // Colors = new AdaptiveColorsConfig() // { // Default = new AdaptiveColorConfig() // { // Normal = Colors.White, // Subtle = new Color() { A = 153, R = 255, G = 255, B = 255 } // } // } //}); var cardResult = AdaptiveCard.FromJsonString(cardJson); if (cardResult.AdaptiveCard == null) { throw new Exception("Failed to parse card"); } var result = renderer.RenderAdaptiveCard(cardResult.AdaptiveCard); if (result.FrameworkElement == null) { throw new Exception("Failed to render card"); } if (actionHandler != null) { // Wire up action click handler result.Action += actionHandler; } return(result); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); PreviewToast.SendRenderingError(ex); return(null); } }