public static FrameworkElement Render(AdaptiveContainer container, AdaptiveRenderContext context) { var uiContainer = new Grid(); uiContainer.Style = context.GetStyle("Adaptive.Container"); uiContainer.SetBackgroundSource(container.BackgroundImage, context); // Keep track of ContainerStyle.ForegroundColors before Container is rendered var outerStyle = context.ForegroundColors; var parentContainerStyle = context.ParentStyle; if (container.Style != null) { ApplyPadding(uiContainer, container, parentContainerStyle, context); // Apply background color ContainerStyleConfig containerStyle = context.Config.ContainerStyles.GetContainerStyleConfig(container.Style); uiContainer.SetBackgroundColor(containerStyle.BackgroundColor, context); context.ForegroundColors = containerStyle.ForegroundColors; } switch (container.VerticalContentAlignment) { case AdaptiveVerticalContentAlignment.Center: uiContainer.VerticalAlignment = VerticalAlignment.Center; break; case AdaptiveVerticalContentAlignment.Bottom: uiContainer.VerticalAlignment = VerticalAlignment.Bottom; break; case AdaptiveVerticalContentAlignment.Top: default: break; } // Modify context outer parent style so padding necessity can be determined AdaptiveContainerStyle containerContainerStyle = container.Style ?? parentContainerStyle; if (containerContainerStyle == AdaptiveContainerStyle.None) { containerContainerStyle = parentContainerStyle; } context.ParentStyle = containerContainerStyle; AddContainerElements(uiContainer, container.Items, context); if (container.SelectAction != null) { return(context.RenderSelectAction(container.SelectAction, uiContainer)); } Grid uiOuterContainer = new Grid(); uiOuterContainer.Children.Add(uiContainer); Border border = new Border(); border.Child = uiOuterContainer; if (!container.IsVisible) { border.Visibility = Visibility.Collapsed; } // Revert context's value to that of outside the Container context.ForegroundColors = outerStyle; context.ParentStyle = parentContainerStyle; return(border); }
public static FrameworkElement RenderAdaptiveCardWrapper(AdaptiveCard card, AdaptiveRenderContext context) { var outerGrid = new Grid(); outerGrid.Style = context.GetStyle("Adaptive.Card"); outerGrid.Background = context.GetColorBrush(context.Config.ContainerStyles.Default.BackgroundColor); outerGrid.SetBackgroundSource(card.BackgroundImage, context); if (context.CardRoot == null) { context.CardRoot = outerGrid; } AdaptiveContainerStyle cardContainerStyle = AdaptiveContainerStyle.Default; if (context.Config.AdaptiveCard.AllowCustomStyle && (card.Style != AdaptiveContainerStyle.None)) { cardContainerStyle = card.Style; } context.RenderArgs.ParentStyle = cardContainerStyle; var grid = new Grid(); grid.Style = context.GetStyle("Adaptive.InnerCard"); grid.Margin = new Thickness(context.Config.Spacing.Padding); grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); switch (card.VerticalContentAlignment) { case AdaptiveVerticalContentAlignment.Center: grid.VerticalAlignment = VerticalAlignment.Center; break; case AdaptiveVerticalContentAlignment.Bottom: grid.VerticalAlignment = VerticalAlignment.Bottom; break; case AdaptiveVerticalContentAlignment.Top: default: break; } AdaptiveContainerRenderer.AddContainerElements(grid, card.Body, context); AdaptiveActionSetRenderer.AddActions(grid, card.Actions, context); // Only handle Action show cards for the main card if (context.CardDepth == 1) { // Define a new row to contain all the show cards if (context.ActionShowCards.Count > 0) { grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); } foreach (var showCardTuple in context.ActionShowCards) { var currentShowCard = showCardTuple.Item1; var uiButton = showCardTuple.Item2; Grid.SetRow(currentShowCard, grid.RowDefinitions.Count - 1); grid.Children.Add(currentShowCard); // Assign on click function to all button elements uiButton.Click += (sender, e) => { bool isCardCollapsed = (currentShowCard.Visibility != Visibility.Visible); // Collapse all the show cards foreach (var t in context.ActionShowCards) { var showCard = t.Item1; showCard.Visibility = Visibility.Collapsed; } // If current card is previously collapsed, show it if (isCardCollapsed) { currentShowCard.Visibility = Visibility.Visible; } }; } } outerGrid.Children.Add(grid); if (card.SelectAction != null) { var outerGridWithSelectAction = context.RenderSelectAction(card.SelectAction, outerGrid); return(outerGridWithSelectAction); } return(outerGrid); }
public static void ApplyPadding(Grid uiElement, AdaptiveTypedElement element, AdaptiveContainerStyle parentStyle, AdaptiveRenderContext context) { bool canApplyPadding = false; // AdaptiveColumn inherits from AdaptiveContainer so only one check is required for both if (element is AdaptiveContainer container) { canApplyPadding = ((container.BackgroundImage != null) || ((container.Style != AdaptiveContainerStyle.None) && (container.Style != parentStyle))); } else if (element is AdaptiveColumnSet columnSet) { canApplyPadding = ((columnSet.Style != AdaptiveContainerStyle.None) && (columnSet.Style != parentStyle)); } if (canApplyPadding) { int padding = context.Config.Spacing.Padding; Thickness paddingThickness = new Thickness { Left = padding, Right = padding }; uiElement.Margin = paddingThickness; } }
public static FrameworkElement Render(AdaptiveColumnSet columnSet, AdaptiveRenderContext context) { var uiColumnSet = new Grid(); uiColumnSet.Style = context.GetStyle($"Adaptive.{columnSet.Type}"); // Keep track of ContainerStyle.ForegroundColors before Container is rendered var outerStyle = context.ForegroundColors; var parentContainerStyle = context.ParentStyle; if (columnSet.Style != null) { AdaptiveContainerRenderer.ApplyPadding(uiColumnSet, columnSet, parentContainerStyle, context); // Apply background color var columnSetStyle = context.Config.ContainerStyles.GetContainerStyleConfig(columnSet.Style); uiColumnSet.SetBackgroundColor(columnSetStyle.BackgroundColor, context); context.ForegroundColors = columnSetStyle.ForegroundColors; } AdaptiveContainerStyle columnSetContainerStyle = columnSet.Style ?? parentContainerStyle; if (columnSetContainerStyle == AdaptiveContainerStyle.None) { columnSetContainerStyle = parentContainerStyle; } context.ParentStyle = columnSetContainerStyle; foreach (var column in columnSet.Columns) { FrameworkElement uiContainer = context.Render(column); // Add vertical Seperator if (uiColumnSet.ColumnDefinitions.Count > 0) { if (column.Separator || column.Spacing != AdaptiveSpacing.None) { var uiSep = new Grid(); uiSep.Style = context.GetStyle($"Adaptive.VerticalSeparator"); uiSep.VerticalAlignment = VerticalAlignment.Stretch; int spacing = context.Config.GetSpacing(column.Spacing); uiSep.Margin = new Thickness(spacing / 2.0, 0, spacing / 2.0, 0); uiSep.Width = context.Config.Separator.LineThickness; if (column.Separator && context.Config.Separator.LineColor != null) { uiSep.Background = context.GetColorBrush(context.Config.Separator.LineColor); } uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto }); Grid.SetColumn(uiSep, uiColumnSet.ColumnDefinitions.Count - 1); uiColumnSet.Children.Add(uiSep); } } // do some sizing magic using the magic GridUnitType.Star var width = column.Width?.ToLower(); if (string.IsNullOrEmpty(width)) #pragma warning disable CS0618 // Type or member is obsolete { width = column.Size?.ToLower(); } #pragma warning restore CS0618 // Type or member is obsolete if (width == null || width == AdaptiveColumnWidth.Stretch.ToLower()) { uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); } else if (width == AdaptiveColumnWidth.Auto.ToLower()) { uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto }); } else { if (double.TryParse(width, out double val) && val >= 0) { // Weighted proportion (number only) uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(val, GridUnitType.Star) }); } else if (width.EndsWith("px") && double.TryParse(width.Substring(0, width.Length - 2), out double pxVal) && pxVal >= 0) { // Exact pixel (number followed by "px") uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength((int)pxVal, GridUnitType.Pixel) }); } else { uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto }); } } Grid.SetColumn(uiContainer, uiColumnSet.ColumnDefinitions.Count - 1); uiColumnSet.Children.Add(uiContainer); } if (columnSet.SelectAction != null) { return(context.RenderSelectAction(columnSet.SelectAction, uiColumnSet)); } if (!columnSet.IsVisible) { uiColumnSet.Visibility = Visibility.Collapsed; } // Revert context's value to that of outside the Container context.ForegroundColors = outerStyle; context.ParentStyle = parentContainerStyle; return(uiColumnSet); }