示例#1
0
        protected static void AddSelectAction(HtmlTag tag, AdaptiveAction selectAction, AdaptiveRenderContext context)
        {
            if (context.Config.SupportsInteractivity && selectAction != null)
            {
                tag.AddClass("ac-selectable");
                AddActionAttributes(selectAction, tag, context);

                // Create the additional card below for showCard actions
                if (selectAction is AdaptiveShowCardAction showCardAction)
                {
                    var cardId = tag.Attributes["data-ac-showCardId"];

                    var uiShowCard = context.Render(showCardAction.Card);
                    if (uiShowCard != null)
                    {
                        uiShowCard.Attr("id", cardId)
                        .AddClass("ac-showCard")
                        .Style("padding", "0")
                        .Style("display", "none")
                        .Style("margin-top", $"{context.Config.Actions.ShowCard.InlineTopMargin}px");

                        // Store all showCard tags inside context
                        context.ShowCardTags.Add(uiShowCard);
                    }
                }
            }
        }
示例#2
0
        public static Button CreateActionButton(AdaptiveAction action, AdaptiveRenderContext context)
        {
            ActionsConfig styling  = context.Config.Actions;
            var           uiButton = new Button()
            {
                HorizontalAlignment = HorizontalAlignment.Stretch,
            };

            uiButton.SetBackgroundColor(styling.BackgroundColor, context);
            uiButton.SetBorderColor(styling.BorderColor, context);
            uiButton.SetBorderThickness(styling.BorderThickness);
            uiButton.Style = context.GetStyle($"Adaptive.{action.Type}");

            TextBlock uiTitle = new TextBlock()
            {
                Text     = action.Title,
                FontSize = styling.FontSize,
                Margin   = new Thickness(6)
                           // TODO: Should this be from HostConfig?
            };

            uiTitle.SetFontWeight(styling.FontWeight);
            uiTitle.SetColor(styling.TextColor, context);
            uiTitle.Style    = context.GetStyle($"Adaptive.Action.Title");
            uiButton.Content = uiTitle;
            string name = context.GetType().Name.Replace("Action", String.Empty);

            return(uiButton);
        }
        public static Button CreateActionButton(AdaptiveAction action, AdaptiveRenderContext context)
        {
            //var button = new ContentButton()
            //{
            //    Style = context.GetStyle(string.Format("Adaptive.{0}", action.Type)),
            //    Padding = new Thickness(6, 4, 6, 4),
            //    Content = new Label()
            //    {
            //        Text = action.Title,
            //        FontSize = (double)context.Config.FontSizes.Default,
            //        Style = context.GetStyle("Adaptive.Action.Title")
            //    }
            //};
            var button = new Button()
            {
                //Style = context.GetStyle(string.Format("Adaptive.{0}", action.Type)),
                Text       = action.Title,
                FontSize   = (double)context.Config.FontSizes.Medium,
                FontFamily = "Segoe UI",
            };

            context.GetType().Name.Replace("Action", string.Empty);

            MessagingCenter.Subscribe <Page>(button, "HidePreviousButtons", (sender) =>
            {
                button.IsVisible     = false;
                button.HeightRequest = 0;
                MessagingCenter.Unsubscribe <Page>(button, "HidePreviousButtons");
            });

            return(button);
        }
        public void ParseToggleVisibility()
        {
            string url  = "http://adaptivecards.io/content/cats/1.png";
            var    json = @"{
  ""$schema"": ""http://adaptivecards.io/schemas/adaptive-card.json"",
  ""type"": ""AdaptiveCard"",
  ""version"": ""1.2"",
  ""body"": [
    {
      ""type"": ""Image"",
      ""url"": """ + url + @""",
      ""selectAction"": {
        ""type"": ""Action.ToggleVisibility"",
        ""targetElements"": [
          ""id1"",
          { ""elementId"": ""id2"", ""isVisible"": false },
          { ""elementId"": ""id3"", ""isVisible"": true }
        ]
      }
    }
  ],
  ""actions"": [ ]
}";

            var result = AdaptiveCard.FromJson(json);

            Assert.IsNotNull(result.Card);

            var body = result.Card.Body;

            Assert.AreEqual(1, body.Count);

            AdaptiveAction toggleVisibilityAction = (body[0] as AdaptiveImage).SelectAction;

            Assert.IsNotNull(toggleVisibilityAction);

            Assert.IsInstanceOfType(toggleVisibilityAction, typeof(AdaptiveToggleVisibilityAction));

            List <object> targetElements = (toggleVisibilityAction as AdaptiveToggleVisibilityAction).TargetElements;

            Assert.AreEqual(3, targetElements.Count);

            Assert.IsTrue(targetElements[0] is string);
            Assert.AreEqual("id1", targetElements[0] as string);

            Assert.IsTrue(targetElements[1] is AdaptiveTargetElement);
            AdaptiveTargetElement targetElement = targetElements[1] as AdaptiveTargetElement;

            Assert.AreEqual("id2", targetElement.ElementId);
            Assert.IsTrue(targetElement.IsVisible == false);

            Assert.IsTrue(targetElements[2] is AdaptiveTargetElement);
            targetElement = targetElements[2] as AdaptiveTargetElement;
            Assert.AreEqual("id3", targetElement.ElementId);
            Assert.IsTrue(targetElement.IsVisible == true);
        }
示例#5
0
        protected static HtmlTag AddActionAttributes(AdaptiveAction action, HtmlTag tag, AdaptiveRenderContext context)
        {
            tag.AddClass(GetActionCssClass(action))
            .Attr("role", "button")
            .Attr("aria-label", action.Title ?? "");

            ActionTransformers.Apply(action, tag, context);

            return(tag);
        }
        public static FrameworkElement Render(AdaptiveAction action, AdaptiveRenderContext context)
        {
            if (context.Config.SupportsInteractivity && context.ActionHandlers.IsSupported(action.GetType()))
            {
                var uiButton = CreateActionButton(action, context);
                uiButton.Click += (sender, e) =>
                {
                    if (action is AdaptiveToggleVisibilityAction toggleVisibilityAction)
                    {
                        foreach (object targetElement in toggleVisibilityAction.TargetElements)
                        {
                            string targetElementId        = "";
                            bool?  targetElementIsVisible = null;

                            if (targetElement is string targetElementString)
                            {
                                targetElementId = targetElementString;
                            }
                            else if (targetElement is AdaptiveTargetElement targetElementObject)
                            {
                                targetElementId        = targetElementObject.ElementId;
                                targetElementIsVisible = targetElementObject.IsVisible;
                            }

                            var element = LogicalTreeHelper.FindLogicalNode(context.CardRoot, targetElementId);

                            if (element != null && element is FrameworkElement elementFrameworkElement)
                            {
                                Visibility visibility = elementFrameworkElement.Visibility;
                                // if we read something with the format {"elementId": <id>", "isVisible": true} or we just read the id and the element is not visible
                                if ((targetElementIsVisible.HasValue && targetElementIsVisible.Value) || (!targetElementIsVisible.HasValue && visibility != Visibility.Visible))
                                {
                                    elementFrameworkElement.Visibility = Visibility.Visible;
                                }
                                // otherwise if we read something with the format {"elementId": <id>", "isVisible": false} or we just read the id and the element is visible
                                else if ((targetElementIsVisible.HasValue && !targetElementIsVisible.Value) || (!targetElementIsVisible.HasValue && visibility == Visibility.Visible))
                                {
                                    elementFrameworkElement.Visibility = Visibility.Collapsed;
                                }
                            }
                        }
                    }
                    else
                    {
                        context.InvokeAction(uiButton, new AdaptiveActionEventArgs(action));
                    }

                    // Prevent nested events from triggering
                    e.Handled = true;
                };
                return(uiButton);
            }
            return(null);
        }
 public static FrameworkElement Render(AdaptiveAction action, AdaptiveRenderContext context)
 {
     if (context.Config.SupportsInteractivity && context.ActionHandlers.IsSupported(action.GetType()))
     {
         var uiButton = CreateActionButton(action, context);
         uiButton.Click += (sender, e) =>
         {
             context.InvokeAction(uiButton, new AdaptiveActionEventArgs(action));
         };
         return(uiButton);
     }
     return(null);
 }
示例#8
0
        public static View RenderSelectAction(this AdaptiveRenderContext context, AdaptiveAction selectAction, View uiElement)
        {
            if (!context.Config.SupportsInteractivity)
            {
                return(uiElement);
            }
            ContentButton button = (ContentButton)context.Render(selectAction);

            button.HorizontalOptions = LayoutOptions.FillAndExpand;
            button.VerticalOptions   = LayoutOptions.FillAndExpand;
            button.BackgroundColor   = Color.Transparent;
            button.Margin            = 0;
            button.Content           = uiElement;
            button.Style             = context.GetStyle("Adaptive.Action.Tap");
            return(button);
        }
        public virtual void Visit(AdaptiveAction action)
        {
            if (action is AdaptiveOpenUrlAction urlAction)
            {
                Visit(urlAction);
            }

            if (action is AdaptiveSubmitAction submitAction)
            {
                Visit(submitAction);
            }

            if (action is AdaptiveShowCardAction cardAction)
            {
                Visit(cardAction);
            }
        }
        public static Button CreateActionButton(AdaptiveAction action, AdaptiveRenderContext context)
        {
            var uiButton = new Button
            {
                //HorizontalAlignment = HorizontalAlignment.Stretch,
                Style   = context.GetStyle($"Adaptive.{action.Type}"),
                Padding = new Thickness(6, 4, 6, 4),
                Content = new TextBlock
                {
                    Text     = action.Title,
                    FontSize = context.Config.FontSizes.Default,
                    Style    = context.GetStyle($"Adaptive.Action.Title")
                }
            };

            string name = context.GetType().Name.Replace("Action", String.Empty);

            return(uiButton);
        }
示例#11
0
        public static FrameworkElement ApplySelectAction(FrameworkElement uiElement, AdaptiveElement element, AdaptiveRenderContext context)
        {
            AdaptiveAction selectAction = null;

            if (element is AdaptiveCollectionElement)
            {
                selectAction = (element as AdaptiveCollectionElement).SelectAction;
            }
            else if (element is AdaptiveImage)
            {
                selectAction = (element as AdaptiveImage).SelectAction;
            }

            if (selectAction != null)
            {
                return(context.RenderSelectAction(selectAction, uiElement));
            }

            return(uiElement);
        }
        protected static Element AdaptiveActionRender(AdaptiveAction action, ElementAdaptiveRenderContext context)
        {
            if (context.Config.SupportsInteractivity)
            {
                var buttonElement = new Ooui.Anchor()
                {
                    Text = action.Title
                }
                .Style("overflow", "hidden")
                .Style("white-space", "nowrap")
                .Style("text-overflow", "ellipsis")
                .Style("flex",
                       context.Config.Actions.ActionAlignment == AdaptiveHorizontalAlignment.Stretch ? "0 1 100%" : "0 1 auto")
                .AddClass("ac-pushButton");

                AddActionAttributes(action, buttonElement, context);
                return(buttonElement);
            }

            return(null);
        }
示例#13
0
        public static View Render(AdaptiveAction action, AdaptiveRenderContext context)
        {
            if (!context.Config.SupportsInteractivity || !context.ActionHandlers.IsSupported(action.GetType()))
            {
                return(null);
            }
            var button = CreateActionButton(action, context);
            var view   = CreateActionButton(action, context);

            button.Clicked += new EventHandler((object sender, EventArgs e) =>
            {
                bool isInputMissing = false;
                if (context.InputBindings.Count > 0)
                {
                    isInputMissing = true;
                    var dict       = context.UserInputs.AsDictionary();
                    foreach (var binding in dict.Values)
                    {
                        if (!string.IsNullOrWhiteSpace(binding))
                        {
                            isInputMissing = false;
                            break;
                        }
                    }
                }
                if (isInputMissing)
                {
                    var missingInput     = new MissingAdaptiveInput();
                    missingInput.Message = "Select at least one option.";
                    context.InvokeMissingInput(action, new MissingInputEventArgs(missingInput, button));
                }
                else
                {
                    context.InvokeAction(button, new AdaptiveActionEventArgs(action));
                    button.IsEnabled = false;
                }
            });
            return(button);
        }
示例#14
0
        protected static HtmlTag AdaptiveActionRender(AdaptiveAction action, AdaptiveRendererContext context)
        {
            if (context.Config.SupportsInteractivity)
            {
                var buttonElement = new HtmlTag("button", false)
                {
                    Text = action.Title
                }
                .Attr("type", "button")
                .Style("overflow", "hidden")
                .Style("white-space", "nowrap")
                .Style("text-overflow", "ellipsis")
                .Style("flex",
                       context.Config.Actions.ActionAlignment == AdaptiveHorizontalAlignment.Stretch ? "0 1 100%" : "0 1 auto")
                .AddClass("ac-pushButton")
                .AddClass(GetActionCssClass(action));

                return(buttonElement);
            }

            return(null);
        }
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            AdaptiveAction action = (sender as FrameworkElement).DataContext as AdaptiveAction;

            if (action != null && action.Inputs.Any())
            {
                var transformer = new AdaptiveBlockToCardTransformer();
                var sharedCard  = (await transformer.TransformAsync(new AdaptiveBlock(new AdaptiveBlockContent()
                {
                    Actions =
                    {
                        action
                    }
                }))).Result;

                InputCollectionFields.Child = SharedAdaptiveCardRenderer.RenderFromCard(sharedCard);
                InputCollection.Visibility  = Visibility.Visible;
            }
            else
            {
                await new MessageDialog("Action performed!").ShowAsync();
            }
        }
        public static Button CreateActionButton(AdaptiveAction action, AdaptiveRenderContext context)
        {
            var uiButton = new Button
            {
                Style = context.GetStyle($"Adaptive.{action.Type}"),
            };

            if (action.Sentiment == AdaptiveSentiment.Positive || action.Sentiment == AdaptiveSentiment.Destructive)
            {
                Style sentimentStyle = context.GetStyle($"Adaptive.{action.Type}.{action.Sentiment}");

                if (sentimentStyle == null)
                {
                    if (action.Sentiment == AdaptiveSentiment.Positive)
                    {
                        sentimentStyle = context.GetStyle("PositiveActionDefaultStyle");
                    }
                    else if (action.Sentiment == AdaptiveSentiment.Destructive)
                    {
                        sentimentStyle = context.GetStyle("DestructiveActionDefaultStyle");
                    }
                }

                uiButton.Style = sentimentStyle;
            }

            var contentStackPanel = new StackPanel();

            if (!context.IsRenderingSelectAction)
            {
                // Only apply padding for normal card actions
                uiButton.Padding = new Thickness(6, 4, 6, 4);
            }
            else
            {
                // Remove any extra spacing for selectAction
                uiButton.Padding         = new Thickness(0, 0, 0, 0);
                contentStackPanel.Margin = new Thickness(0, 0, 0, 0);
            }
            uiButton.Content = contentStackPanel;
            FrameworkElement uiIcon = null;

            var uiTitle = new TextBlock
            {
                Text     = action.Title,
                FontSize = context.Config.GetFontSize(AdaptiveFontStyle.Default, AdaptiveTextSize.Default),
                Style    = context.GetStyle($"Adaptive.Action.Title")
            };

            if (action.IconUrl != null)
            {
                var actionsConfig = context.Config.Actions;

                var image = new AdaptiveImage(action.IconUrl)
                {
                    HorizontalAlignment = AdaptiveHorizontalAlignment.Center
                };
                uiIcon = AdaptiveImageRenderer.Render(image, context);
                if (actionsConfig.IconPlacement == IconPlacement.AboveTitle)
                {
                    contentStackPanel.Orientation = Orientation.Vertical;
                    uiIcon.Height = (double)actionsConfig.IconSize;
                }
                else
                {
                    contentStackPanel.Orientation = Orientation.Horizontal;
                    //Size the image to the textblock, wait until layout is complete (loaded event)
                    uiIcon.Loaded += (sender, e) =>
                    {
                        uiIcon.Height = uiTitle.ActualHeight;
                    };
                }
                contentStackPanel.Children.Add(uiIcon);

                // Add spacing for the icon for horizontal actions
                if (actionsConfig.IconPlacement == IconPlacement.LeftOfTitle)
                {
                    int spacing = context.Config.GetSpacing(AdaptiveSpacing.Default);
                    var uiSep   = new Grid
                    {
                        Style             = context.GetStyle($"Adaptive.VerticalSeparator"),
                        VerticalAlignment = VerticalAlignment.Stretch,
                        Width             = spacing,
                    };
                    contentStackPanel.Children.Add(uiSep);
                }
            }

            contentStackPanel.Children.Add(uiTitle);

            string name = context.GetType().Name.Replace("Action", String.Empty);

            return(uiButton);
        }
示例#17
0
        public static FrameworkElement RenderSelectAction(this AdaptiveRenderContext context, AdaptiveAction selectAction, FrameworkElement uiElement)
        {
            if (context.Config.SupportsInteractivity)
            {
                context.IsRenderingSelectAction = true;
                var uiButton = (Button)context.Render(selectAction);
                context.IsRenderingSelectAction = false;

                // Stretch both the button and button's content to avoid empty spaces
                uiButton.HorizontalAlignment        = HorizontalAlignment.Stretch;
                uiButton.HorizontalContentAlignment = HorizontalAlignment.Stretch;

                // Adopt the child element's background to parent and set the child's background
                // to be transparent in order for button's on mouse hover color to work properly
                if (uiElement is Panel p)
                {
                    uiButton.Background = p.Background;
                    p.Background        = new SolidColorBrush(Colors.Transparent);
                }
                else
                {
                    uiButton.Background = new SolidColorBrush(Colors.Transparent);
                }
                uiButton.BorderThickness = new Thickness(0);
                uiButton.Content         = uiElement;
                uiButton.Style           = context.GetStyle("Adaptive.Action.Tap");

                // Handle ShowCard
                if (selectAction is AdaptiveShowCardAction showCardAction)
                {
                    var  actionsConfig = context.Config.Actions;
                    bool isInline      = (actionsConfig.ShowCard.ActionMode == ShowCardActionMode.Inline);
                    if (isInline && context.CardDepth == 1)
                    {
                        FrameworkElement uiShowCardContainer = showCardAction.CreateShowCard(context, actionsConfig);

                        // Add to the list of show cards in context
                        context.ActionShowCards.Add(new Tuple <FrameworkElement, Button>(uiShowCardContainer, uiButton));
                    }
                }

                return(uiButton);
            }

            return(uiElement);
        }
 /// <summary>
 /// 為AdaptiveActionSet加入一個AdaptiveAction
 /// </summary>
 /// <param name="set">要被加入AdaptiveAction的AdaptiveActionSet</param>
 /// <param name="action">要加入的AdaptiveAction</param>
 /// <returns>加入一個AdaptiveAction後的AdaptiveActionSet</returns>
 public static AdaptiveActionSet AddActionToSet(this AdaptiveActionSet set, AdaptiveAction action)
 {
     set.Actions.Add(action);
     return(set);
 }
        public static FrameworkElement RenderSelectAction(this AdaptiveRenderContext context, AdaptiveAction selectAction, FrameworkElement uiElement)
        {
            if (context.Config.SupportsInteractivity)
            {
                var uiButton = (Button)context.Render(selectAction);
                uiButton.HorizontalAlignment = HorizontalAlignment.Left;
                uiButton.Background          = new SolidColorBrush(Colors.Transparent);
                uiButton.BorderThickness     = new Thickness(0);
                uiButton.Content             = uiElement;
                uiButton.Style = context.GetStyle("Adaptive.Action.Tap");
                return(uiButton);
            }

            return(uiElement);
        }
 public AdaptiveActionEventArgs(AdaptiveAction action)
 {
     Action = action;
 }
示例#21
0
        protected static HtmlTag AdaptiveActionRender(AdaptiveAction action, AdaptiveRenderContext context)
        {
            if (context.Config.SupportsInteractivity)
            {
                var actionsConfig = context.Config.Actions;
                var buttonElement = new HtmlTag("button", false)
                                    .Attr("type", "button")
                                    .Style("overflow", "hidden")
                                    .Style("white-space", "nowrap")
                                    .Style("text-overflow", "ellipsis")
                                    .Style("flex",
                                           actionsConfig.ActionAlignment == AdaptiveHorizontalAlignment.Stretch ? "0 1 100%" : "0 1 auto")
                                    .Style("display", "flex")
                                    .Style("align-items", "center")
                                    .Style("justify-content", "center")
                                    .AddClass("ac-pushButton");

                var hasTitle = !string.IsNullOrEmpty(action.Title);

                if (action.IconUrl != null)
                {
                    // Append the icon to the button
                    // NOTE: always using icon size since it's difficult
                    // to match icon's height with text's height
                    var iconElement = new HtmlTag("image", false)
                                      .Attr("src", action.IconUrl)
                                      .Style("max-height", $"{actionsConfig.IconSize}px");

                    if (actionsConfig.IconPlacement == IconPlacement.LeftOfTitle)
                    {
                        buttonElement.Style("flex-direction", "row");

                        if (hasTitle)
                        {
                            iconElement.Style("margin-right", "4px");
                        }
                    }
                    else
                    {
                        buttonElement.Style("flex-direction", "column");

                        if (hasTitle)
                        {
                            iconElement.Style("margin-bottom", "4px");
                        }
                    }

                    buttonElement.Append(iconElement);
                }

                var titleElement = new HtmlTag("div", false)
                {
                    Text = action.Title
                };
                buttonElement.Append(titleElement);

                AddActionAttributes(action, buttonElement, context);
                return(buttonElement);
            }

            return(null);
        }
 public void MissingInput(AdaptiveAction sender, MissingInputEventArgs args)
 {
     OnMissingInput?.Invoke(sender, args);
 }
示例#23
0
        public static FrameworkElement RenderSelectAction(this AdaptiveRenderContext context, AdaptiveAction selectAction, FrameworkElement uiElement)
        {
            if (context.Config.SupportsInteractivity)
            {
                // SelectAction doesn't allow showCard actions
                if (selectAction is AdaptiveShowCardAction)
                {
                    context.Warnings.Add(new AdaptiveWarning(-1, "Inline ShowCard not supported for SelectAction"));
                    return(uiElement);
                }

                context.IsRenderingSelectAction = true;
                var uiButton = (Button)context.Render(selectAction);
                context.IsRenderingSelectAction = false;

                // Stretch both the button and button's content to avoid empty spaces
                uiButton.HorizontalAlignment        = HorizontalAlignment.Stretch;
                uiButton.HorizontalContentAlignment = HorizontalAlignment.Stretch;

                // Adopt the child element's background to parent and set the child's background
                // to be transparent in order for button's on mouse hover color to work properly
                if (uiElement is Panel p)
                {
                    uiButton.Background = p.Background;
                    p.Background        = new SolidColorBrush(Colors.Transparent);
                }
                else
                {
                    uiButton.Background = new SolidColorBrush(Colors.Transparent);
                }
                uiButton.BorderThickness = new Thickness(0);
                uiButton.Content         = uiElement;
                uiButton.Style           = context.GetStyle("Adaptive.Action.Tap");

                return(uiButton);
            }

            return(uiElement);
        }