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 { } }
private void RenderAdaptiveCard(string cardJson, TimeSpan mediaPlayedPosition, string mediaUrl) { IsHitTestVisible = false; // Create a new adaptive card renderer. var renderer = new AdaptiveCardRenderer(); // Customize the font sizes via AdaptiveHostConfig. var hostConfig = new AdaptiveHostConfig { FontFamily = "Tw Cen MT", FontSizes = { Small = 14, Default = 16, Medium = 16, Large = 20, ExtraLarge = 24 } }; renderer.HostConfig = hostConfig; // Get card from json. var card = renderer.RenderAdaptiveCardFromJsonString(cardJson); // Attach the card content to UI. if (card.FrameworkElement is FrameworkElement element) { element.Loaded += (s, e) => { SetupExpressionAnimationsForCard(); var mediaElement = element.Children().OfType <MediaElement>().Single(); // TODO: Why mediaElement.Source is null here?? How do we // auto-play without hacking?? // If the following works, we don't have to pass in the mediaUrl // at all! Let me know if you know a better workaround. :) //mediaElement.Position = mediaPlayedPosition; //mediaElement.Play(); mediaElement.Source = new Uri(mediaUrl); mediaElement.MediaOpened += (o, a) => { mediaElement.Position = mediaPlayedPosition; //mediaElement.Play(); // This doesn't work... }; // Now the UI is ready. IsHitTestVisible = true; }; MediaContainer.Child = element; Card.Visibility = Visibility.Visible; } }
public async Task ColumnFallbackAsync() { string cardStr = "{" + "\"type\": \"AdaptiveCard\"," + "\"version\": \"1.0\"," + "\"body\": [" + "{" + " \"type\": \"ColumnSet\"," + " \"columns\": [" + " {" + " \"type\": \"Column\"," + " \"items\": [" + " {" + " \"type\": \"Graph\"" + " }]," + " \"fallback\": {" + " \"type\": \"Column\"," + " \"items\": [" + " {" + " \"type\": \"TextBlock\"," + " \"text\": \"Fallback\"" + " }]" + " }" + " }]" + "}]}"; var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher; Exception testException = null; // Need to move the test to the UI Thread await dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { try { AdaptiveCardRenderer renderer = new AdaptiveCardRenderer(); RenderedAdaptiveCard renderedCard = renderer.RenderAdaptiveCardFromJsonString(cardStr); Assert.AreEqual(1, renderedCard.Warnings.Count); Assert.AreEqual("Performing fallback for element of type \"Column\" (fallback element type \"Column\")", renderedCard.Warnings[0].Message); } catch (Exception thrown) { testException = thrown; } }); if (testException != null) { throw testException; } }
private void RenderAdaptiveCard() { // We want to make sure _mediaElement and _cardJson are populated before any interaction to the UI. IsHitTestVisible = false; // Create a new adaptive card renderer. var renderer = new AdaptiveCardRenderer(); // Customize the font sizes via AdaptiveHostConfig. var hostConfig = new AdaptiveHostConfig { FontFamily = "Tw Cen MT", FontSizes = { Small = 14, Default = 16, Medium = 16, Large = 20, ExtraLarge = 24 } }; renderer.HostConfig = hostConfig; // Get card from json. _cardJson = CreateMediaAdaptiveCardJson(); var card = renderer.RenderAdaptiveCardFromJsonString(_cardJson); // Monitor when video is played... // TODO: Not fired... Bug??? card.MediaClicked += (c, e) => { }; // Attach the card content to UI. if (card.FrameworkElement is FrameworkElement element) { element.Loaded += (s, e) => { _mediaElement = element.Children().OfType <MediaElement>().Single(); // Now the UI is ready. IsHitTestVisible = true; SetupInteractionTracker(); }; MediaContainer.Child = element; } }
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; } }
private async void LayoutRoot_Loaded(object sender, RoutedEventArgs e) { var bungie = new BungieService(Shared.SharedData.BungieApiKey); var adaptiveCardRenderer = new AdaptiveCardRenderer { HostConfig = new AdaptiveHostConfig { FontFamily = "Segoe UI", FontSizes = new AdaptiveFontSizesConfig { Small = 12, Default = 14, Large = 20, ExtraLarge = 24 }, FontWeights = new AdaptiveFontWeightsConfig { Lighter = 200, Default = 400, Bolder = 700 }, ContainerStyles = new AdaptiveContainerStylesDefinition { Default = new AdaptiveContainerStyleDefinition { BackgroundColor = Color.FromArgb(255, 83, 84, 84), ForegroundColors = new AdaptiveColorsConfig { Default = new AdaptiveColorConfig { Default = Color.FromArgb(255, 255, 255, 255), Subtle = Color.FromArgb(255, 156, 158, 159) }, Attention = new AdaptiveColorConfig { Default = Color.FromArgb(255, 255, 0, 0), Subtle = Color.FromArgb(221, 255, 0, 0) }, Good = new AdaptiveColorConfig { Default = Color.FromArgb(255, 0, 255, 0), Subtle = Color.FromArgb(221, 0, 255, 0) } } } }, ImageSizes = new AdaptiveImageSizesConfig { Small = 40, Medium = 80, Large = 120 }, ImageSet = new AdaptiveImageSetConfig { ImageSize = ImageSize.Medium, MaxImageHeight = 100 } } }; var getActivityDefinition = bungie.GetActivityDefinitionAsync(Pgcr.ActivityDetails.ReferenceId); var getModeDefinition = bungie.GetActivityModeDefinitionAsync(Pgcr.ActivityDetails.Mode); var activity = new DestinyUserActivity(Pgcr, await getActivityDefinition, await getModeDefinition, CharacterId); var renderedCard = adaptiveCardRenderer.RenderAdaptiveCardFromJsonString(activity.Activity.VisualElements.Content.ToJson()); if (renderedCard.FrameworkElement != null) { Grid uiCard = renderedCard.FrameworkElement as Grid; uiCard.Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0)); var background = uiCard.Children.OfType <Image>().FirstOrDefault(); if (background != null) { background.Opacity = 0.5; } uiCard.Width = 316; uiCard.Height = 174; uiCard.Margin = new Thickness(0, 0, 0, 24); LayoutRoot.Child = uiCard; } }