コード例 #1
0
        public static FrameworkElement Render(AdaptiveTextInput input, AdaptiveRenderContext context)
        {
            if (context.Config.SupportsInteractivity)
            {
                var textBox = new WatermarkTextBox()
                {
                    Text = input.Value
                };
                if (input.IsMultiline == true)
                {
                    textBox.AcceptsReturn = true;
                    textBox.TextWrapping  = TextWrapping.Wrap;
                    textBox.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
                }
                if (input.MaxLength > 0)
                {
                    textBox.MaxLength = input.MaxLength;
                }

                textBox.Watermark   = input.Placeholder;
                textBox.Style       = context.GetStyle($"Adaptive.Input.Text.{input.Style}");
                textBox.DataContext = input;
                context.InputBindings.Add(input.Id, () => textBox.Text);
                if (input.InlineAction != null)
                {
                    if (context.Config.Actions.ShowCard.ActionMode == ShowCardActionMode.Inline &&
                        input.InlineAction.GetType() == typeof(AdaptiveShowCardAction))
                    {
                        context.Warnings.Add(new AdaptiveWarning(-1, "Inline ShowCard not supported for InlineAction"));
                    }
                    else
                    {
                        if (context.Config.SupportsInteractivity && context.ActionHandlers.IsSupported(input.InlineAction.GetType()))
                        {
                            // Set up a parent view that holds textbox, separator and button
                            var parentView = new Grid();

                            // grid config for textbox
                            parentView.ColumnDefinitions.Add(new ColumnDefinition()
                            {
                                Width = new GridLength(1, GridUnitType.Star)
                            });
                            Grid.SetColumn(textBox, 0);
                            parentView.Children.Add(textBox);

                            // grid config for spacing
                            int spacing = context.Config.GetSpacing(AdaptiveSpacing.Default);
                            var uiSep   = new Grid
                            {
                                Style             = context.GetStyle($"Adaptive.Input.Text.InlineAction.Separator"),
                                VerticalAlignment = VerticalAlignment.Stretch,
                                Width             = spacing,
                            };
                            parentView.ColumnDefinitions.Add(new ColumnDefinition()
                            {
                                Width = new GridLength(spacing, GridUnitType.Pixel)
                            });
                            Grid.SetColumn(uiSep, 1);

                            // adding button
                            var   uiButton = new Button();
                            Style style    = context.GetStyle($"Adaptive.Input.Text.InlineAction.Button");
                            if (style != null)
                            {
                                uiButton.Style = style;
                            }

                            // this textblock becomes tooltip if icon url exists else becomes the tile for the button
                            var uiTitle = new TextBlock
                            {
                                Text = input.InlineAction.Title,
                            };

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

                                var image = new AdaptiveImage(input.InlineAction.IconUrl)
                                {
                                    HorizontalAlignment = AdaptiveHorizontalAlignment.Center,
                                    Type = "Adaptive.Input.Text.InlineAction.Image",
                                };

                                FrameworkElement uiIcon = null;
                                uiIcon           = AdaptiveImageRenderer.Render(image, context);
                                uiButton.Content = uiIcon;

                                // adjust height
                                textBox.Loaded += (sender, e) =>
                                {
                                    uiIcon.Height = textBox.ActualHeight;
                                };

                                uiButton.ToolTip = uiTitle;
                            }
                            else
                            {
                                uiTitle.FontSize = context.Config.GetFontSize(AdaptiveFontStyle.Default, AdaptiveTextSize.Default);
                                uiTitle.Style    = context.GetStyle($"Adaptive.Input.Text.InlineAction.Title");
                                uiButton.Content = uiTitle;
                            }

                            uiButton.Click += (sender, e) =>
                            {
                                context.InvokeAction(uiButton, new AdaptiveActionEventArgs(input.InlineAction));

                                // Prevent nested events from triggering
                                e.Handled = true;
                            };

                            parentView.ColumnDefinitions.Add(new ColumnDefinition()
                            {
                                Width = GridLength.Auto
                            });
                            Grid.SetColumn(uiButton, 2);
                            parentView.Children.Add(uiButton);
                            uiButton.VerticalAlignment = VerticalAlignment.Bottom;

                            textBox.KeyDown += (sender, e) =>
                            {
                                if (e.Key == System.Windows.Input.Key.Enter)
                                {
                                    context.InvokeAction(uiButton, new AdaptiveActionEventArgs(input.InlineAction));
                                    e.Handled = true;
                                }
                            };
                            return(parentView);
                        }
                    }
                }
                return(textBox);
            }
            else
            {
                var textBlock = AdaptiveTypedElementConverter.CreateElement <AdaptiveTextBlock>();
                textBlock.Text = XamlUtilities.GetFallbackText(input) ?? input.Placeholder;
                return(context.Render(textBlock));
            }
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        public static FrameworkElement RenderInlineAction(AdaptiveTextInput input, AdaptiveRenderContext context, FrameworkElement textBox)
        {
            // Set up a parent view that holds textbox, separator and button
            var parentView = new Grid();

            // grid config for textbox
            parentView.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = new GridLength(1, GridUnitType.Star)
            });
            Grid.SetColumn(textBox, 0);
            parentView.Children.Add(textBox);

            // grid config for spacing
            int spacing = context.Config.GetSpacing(AdaptiveSpacing.Default);
            var uiSep   = new Grid
            {
                Style             = context.GetStyle($"Adaptive.Input.Text.InlineAction.Separator"),
                VerticalAlignment = VerticalAlignment.Stretch,
                Width             = spacing,
            };

            parentView.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = new GridLength(spacing, GridUnitType.Pixel)
            });
            Grid.SetColumn(uiSep, 1);

            // adding button
            var   uiButton = new Button();
            Style style    = context.GetStyle($"Adaptive.Input.Text.InlineAction.Button");

            if (style != null)
            {
                uiButton.Style = style;
            }

            // this textblock becomes tooltip if icon url exists else becomes the tile for the button
            var uiTitle = new TextBlock
            {
                Text = input.InlineAction.Title,
            };

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

                var image = new AdaptiveImage(input.InlineAction.IconUrl)
                {
                    HorizontalAlignment = AdaptiveHorizontalAlignment.Center,
                    Type = "Adaptive.Input.Text.InlineAction.Image",
                };

                FrameworkElement uiIcon = null;
                uiIcon           = AdaptiveImageRenderer.Render(image, context);
                uiButton.Content = uiIcon;

                // adjust height
                textBox.Loaded += (sender, e) =>
                {
                    uiIcon.Height = textBox.ActualHeight;
                };

                uiButton.ToolTip = uiTitle;
            }
            else
            {
                uiTitle.FontSize = context.Config.GetFontSize(AdaptiveFontType.Default, AdaptiveTextSize.Default);
                uiTitle.Style    = context.GetStyle($"Adaptive.Input.Text.InlineAction.Title");
                uiButton.Content = uiTitle;
            }

            uiButton.Click += (sender, e) =>
            {
                context.InvokeAction(uiButton, new AdaptiveActionEventArgs(input.InlineAction));

                // Prevent nested events from triggering
                e.Handled = true;
            };

            parentView.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = GridLength.Auto
            });
            Grid.SetColumn(uiButton, 2);
            parentView.Children.Add(uiButton);
            uiButton.VerticalAlignment = VerticalAlignment.Bottom;

            textBox.KeyDown += (sender, e) =>
            {
                if (e.Key == System.Windows.Input.Key.Enter)
                {
                    context.InvokeAction(uiButton, new AdaptiveActionEventArgs(input.InlineAction));
                    e.Handled = true;
                }
            };
            return(parentView);
        }