示例#1
0
        public static FrameworkElement Render(AdaptiveRichTextBlock richTB, AdaptiveRenderContext context)
        {
            var uiRichTB = CreateControl(richTB, context);

            foreach (var inlineElement in richTB.Inlines)
            {
                AdaptiveTextRun textRun = inlineElement as AdaptiveTextRun;
                AddInlineTextRun(uiRichTB, textRun, context);
            }

            return(uiRichTB);
        }
示例#2
0
        public void RichTextBlock()
        {
            var card = new AdaptiveCard("1.2");

            var richTB = new AdaptiveRichTextBlock();

            richTB.HorizontalAlignment = AdaptiveHorizontalAlignment.Center;

            // Build text runs
            var textRun1 = new AdaptiveTextRun("Start the rich text block ");

            richTB.Inlines.Add(textRun1);

            var textRun2 = new AdaptiveTextRun("with some cool looking stuff");

            textRun2.Color     = AdaptiveTextColor.Accent;
            textRun2.FontStyle = AdaptiveFontStyle.Monospace;
            textRun2.IsSubtle  = true;
            textRun2.Size      = AdaptiveTextSize.Large;
            textRun2.Weight    = AdaptiveTextWeight.Bolder;
            richTB.Inlines.Add(textRun2);

            card.Body.Add(richTB);

            // Indentation needs to be kept as-is to match the result of card.ToJson
            var expected = @"{
  ""type"": ""AdaptiveCard"",
  ""version"": ""1.2"",
  ""body"": [
    {
      ""type"": ""RichTextBlock"",
      ""horizontalAlignment"": ""center"",
      ""inlines"": [
        {
          ""type"": ""TextRun"",
          ""text"": ""Start the rich text block ""
        },
        {
          ""type"": ""TextRun"",
          ""size"": ""large"",
          ""weight"": ""bolder"",
          ""color"": ""accent"",
          ""isSubtle"": true,
          ""text"": ""with some cool looking stuff"",
          ""fontStyle"": ""monospace""
        }
      ]
    }
  ]
}";

            Assert.AreEqual(expected, card.ToJson());
        }
        public static FrameworkElement Render(AdaptiveRichTextBlock richTB, AdaptiveRenderContext context)
        {
            var uiRichTB = CreateControl(richTB, context);

            foreach (var inlineElement in richTB.Inlines)
            {
                AdaptiveTextRun textRun      = inlineElement as AdaptiveTextRun;
                Inline          uiInlineText = FormatInlineTextRun(textRun, context);
                uiRichTB.Inlines.Add(uiInlineText);
            }
            uiRichTB.Inlines.Add(new LineBreak());

            return(uiRichTB);
        }
        private static TextBlock CreateControl(AdaptiveRichTextBlock richTB, AdaptiveRenderContext context)
        {
            TextBlock uiTextBlock = new TextBlock();

            uiTextBlock.Style        = context.GetStyle($"Adaptive.{richTB.Type}");
            uiTextBlock.TextWrapping = richTB.Wrap ? TextWrapping.Wrap : TextWrapping.NoWrap;
            uiTextBlock.TextTrimming = TextTrimming.CharacterEllipsis;

            if (richTB.HorizontalAlignment != AdaptiveHorizontalAlignment.Left)
            {
                System.Windows.TextAlignment alignment;
                if (Enum.TryParse <System.Windows.TextAlignment>(richTB.HorizontalAlignment.ToString(), out alignment))
                {
                    uiTextBlock.TextAlignment = alignment;
                }
            }

            return(uiTextBlock);
        }
示例#5
0
        public void RichTextBlock()
        {
            AdaptiveTextRun textRun1 = new AdaptiveTextRun
            {
                Color         = ForegroundColor.Accent,
                FontType      = FontType.Monospace,
                Highlight     = true,
                IsSubtle      = true,
                Italic        = true,
                Language      = "en",
                Size          = TextSize.Large,
                Strikethrough = true,
                Text          = "This is text run number 1",
                Weight        = TextWeight.Bolder,
                Underline     = true,
            };

            Assert.AreEqual(ForegroundColor.Accent, textRun1.Color);
            Assert.AreEqual(FontType.Monospace, textRun1.FontType);
            Assert.IsTrue(textRun1.Highlight);
            Assert.IsTrue(textRun1.IsSubtle);
            Assert.IsTrue(textRun1.Italic);
            Assert.IsTrue(textRun1.Strikethrough);
            Assert.AreEqual("en", textRun1.Language);
            Assert.AreEqual(TextSize.Large, textRun1.Size);
            Assert.AreEqual("This is text run number 1", textRun1.Text);
            Assert.AreEqual(TextWeight.Bolder, textRun1.Weight);
            Assert.IsTrue(textRun1.Underline);

            textRun1.SelectAction = new AdaptiveSubmitAction
            {
                Title = "Select Action"
            };
            Assert.IsNotNull(textRun1.SelectAction);
            Assert.AreEqual("Select Action", textRun1.SelectAction.Title);

            AdaptiveTextRun textRun2 = new AdaptiveTextRun {
                Text = "This is text run number 2"
            };
            AdaptiveTextRun textRun3 = new AdaptiveTextRun {
                Text = "This is text run number 3"
            };

            AdaptiveRichTextBlock richTextBlock = new AdaptiveRichTextBlock
            {
                Height = HeightType.Stretch,
                HorizontalAlignment = HAlignment.Center,
                Id        = "RichTextBlockId",
                IsVisible = false,
                Separator = true,
                Spacing   = Spacing.Large,
            };

            ValidateBaseElementProperties(richTextBlock, "RichTextBlockId", false, true, Spacing.Large, HeightType.Stretch);

            Assert.AreEqual(HAlignment.Center, richTextBlock.HorizontalAlignment);

            richTextBlock.Inlines.Add(textRun1);
            richTextBlock.Inlines.Add(textRun2);
            richTextBlock.Inlines.Add(textRun3);

            Assert.AreEqual("This is text run number 1", (richTextBlock.Inlines[0] as AdaptiveTextRun).Text);
            Assert.AreEqual("This is text run number 2", (richTextBlock.Inlines[1] as AdaptiveTextRun).Text);
            Assert.AreEqual("This is text run number 3", (richTextBlock.Inlines[2] as AdaptiveTextRun).Text);

            var jsonString = richTextBlock.ToJson().ToString();

            Assert.AreEqual("{\"height\":\"Stretch\",\"horizontalAlignment\":\"center\",\"id\":\"RichTextBlockId\",\"inlines\":[{\"color\":\"Accent\",\"fontType\":\"Monospace\",\"highlight\":true,\"isSubtle\":true,\"italic\":true,\"selectAction\":{\"title\":\"Select Action\",\"type\":\"Action.Submit\"},\"size\":\"Large\",\"strikethrough\":true,\"text\":\"This is text run number 1\",\"type\":\"TextRun\",\"underline\":true,\"weight\":\"Bolder\"},{\"text\":\"This is text run number 2\",\"type\":\"TextRun\"},{\"text\":\"This is text run number 3\",\"type\":\"TextRun\"}],\"isVisible\":false,\"separator\":true,\"spacing\":\"large\",\"type\":\"RichTextBlock\"}", jsonString);
        }
        public static FrameworkElement Render(AdaptiveRichTextBlock richTB, AdaptiveRenderContext context)
        {
            // used to define MaxLines
            StringBuilder totalText     = new StringBuilder();
            FontWeight    maxTextWeight = FontWeight.FromOpenTypeWeight(context.Config.GetFontWeight(AdaptiveFontStyle.Default, AdaptiveTextWeight.Lighter));
            double        maxTextSize   = context.Config.GetFontSize(AdaptiveFontStyle.Default, AdaptiveTextSize.Small);

            var uiRichTB = CreateControl(richTB, context);

            foreach (var paragraph in richTB.Paragraphs)
            {
                foreach (var inlineElement in paragraph.Inlines)
                {
                    Inline uiInlineText = FormatInlineTextRun(inlineElement, context);
                    uiRichTB.Inlines.Add(uiInlineText);

                    // Extract data for MaxLines
                    totalText.Append(inlineElement.Text);
                    maxTextWeight = SelectMaxTextWeight(maxTextWeight, uiInlineText.FontWeight);
                    maxTextSize   = Math.Max(maxTextSize, uiInlineText.FontSize);
                }
                uiRichTB.Inlines.Add(new LineBreak());
            }

            if (richTB.MaxLines > 0)
            {
                var uiGrid = new Grid();
                uiGrid.RowDefinitions.Add(new RowDefinition()
                {
                    Height = GridLength.Auto
                });

                // create hidden textBlock with appropriate linebreaks that we can use to measure the ActualHeight
                // using same style, fontWeight settings as original textblock
                var measureBlock = new TextBlock()
                {
                    Style               = uiRichTB.Style,
                    FontWeight          = maxTextWeight,
                    FontSize            = maxTextSize,
                    Visibility          = Visibility.Hidden,
                    TextWrapping        = TextWrapping.NoWrap,
                    HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                    VerticalAlignment   = VerticalAlignment.Top,
                    DataContext         = richTB.MaxLines
                };

                measureBlock.Inlines.Add(totalText.ToString());

                // bind the real textBlock's Height => MeasureBlock.ActualHeight
                uiRichTB.SetBinding(FrameworkElement.MaxHeightProperty, new Binding()
                {
                    Path      = new PropertyPath("ActualHeight"),
                    Source    = measureBlock,
                    Mode      = BindingMode.OneWay,
                    Converter = new MultiplyConverter(richTB.MaxLines)
                });

                // Add both to a grid so they go as a unit
                uiGrid.Children.Add(measureBlock);

                uiGrid.Children.Add(uiRichTB);
                return(uiGrid);
            }

            if (!richTB.IsVisible)
            {
                uiRichTB.Visibility = Visibility.Collapsed;
            }

            return(uiRichTB);
        }
        public void RichTextBlock()
        {
            var card = new AdaptiveCard("1.2");

            var richTB = new AdaptiveRichTextBlock();

            richTB.HorizontalAlignment = AdaptiveHorizontalAlignment.Center;

            // Build text runs
            var textRun1 = new AdaptiveTextRun("Start the rich text block ");

            richTB.Inlines.Add(textRun1);

            var textRun2 = new AdaptiveTextRun("with some cool looking stuff. ");

            textRun2.Color         = AdaptiveTextColor.Accent;
            textRun2.FontStyle     = AdaptiveFontStyle.Monospace;
            textRun2.IsSubtle      = true;
            textRun2.Italic        = true;
            textRun2.Strikethrough = true;
            textRun2.Highlight     = true;
            textRun2.Size          = AdaptiveTextSize.Large;
            textRun2.Weight        = AdaptiveTextWeight.Bolder;
            richTB.Inlines.Add(textRun2);

            var textRun3 = new AdaptiveTextRun("This run has a link!");

            textRun3.SelectAction = new AdaptiveOpenUrlAction()
            {
                Title     = "Open URL",
                UrlString = "http://adaptivecards.io/"
            };
            richTB.Inlines.Add(textRun3);

            card.Body.Add(richTB);

            // Indentation needs to be kept as-is to match the result of card.ToJson
            var expected = @"{
  ""type"": ""AdaptiveCard"",
  ""version"": ""1.2"",
  ""body"": [
    {
      ""type"": ""RichTextBlock"",
      ""horizontalAlignment"": ""center"",
      ""inlines"": [
        {
          ""type"": ""TextRun"",
          ""text"": ""Start the rich text block ""
        },
        {
          ""type"": ""TextRun"",
          ""size"": ""large"",
          ""weight"": ""bolder"",
          ""color"": ""accent"",
          ""isSubtle"": true,
          ""italic"": true,
          ""strikethrough"": true,
          ""highlight"": true,
          ""text"": ""with some cool looking stuff. "",
          ""fontStyle"": ""monospace""
        },
        {
          ""type"": ""TextRun"",
          ""text"": ""This run has a link!"",
          ""selectAction"": {
            ""type"": ""Action.OpenUrl"",
            ""url"": ""http://adaptivecards.io/"",
            ""title"": ""Open URL""
          }
        }
      ]
    }
  ]
}";

            Assert.AreEqual(expected, card.ToJson());
        }
示例#8
0
        public void RichTextBlock()
        {
            var card = new AdaptiveCard("1.2");

            var richTB = new AdaptiveRichTextBlock();

            richTB.Wrap                = true;
            richTB.MaxLines            = 3;
            richTB.HorizontalAlignment = AdaptiveHorizontalAlignment.Center;

            // Build First Paragraph
            var paragraph1 = new AdaptiveParagraph();

            var textRun1 = new AdaptiveTextRun("Start the first paragraph ");

            paragraph1.Inlines.Add(textRun1);

            var textRun2 = new AdaptiveTextRun("with some cool looking stuff");

            textRun2.Color     = AdaptiveTextColor.Accent;
            textRun2.FontStyle = AdaptiveFontStyle.Monospace;
            textRun2.IsSubtle  = true;
            textRun2.Size      = AdaptiveTextSize.Large;
            textRun2.Weight    = AdaptiveTextWeight.Bolder;
            paragraph1.Inlines.Add(textRun2);

            richTB.Paragraphs.Add(paragraph1);

            // Build Second Paragraph (Empty inlines)
            var paragraph2 = new AdaptiveParagraph();

            richTB.Paragraphs.Add(paragraph2);

            card.Body.Add(richTB);

            var expected = @"{
  ""type"": ""AdaptiveCard"",
  ""version"": ""1.2"",
  ""body"": [
    {
      ""type"": ""RichTextBlock"",
      ""horizontalAlignment"": ""center"",
      ""wrap"": true,
      ""maxLines"": 3,
      ""paragraphs"": [
        {
          ""inlines"": [
            {
              ""type"": ""TextRun"",
              ""text"": ""Start the first paragraph ""
            },
            {
              ""type"": ""TextRun"",
              ""size"": ""large"",
              ""weight"": ""bolder"",
              ""color"": ""accent"",
              ""isSubtle"": true,
              ""text"": ""with some cool looking stuff"",
              ""fontStyle"": ""monospace""
            }
          ]
        },
        {
          ""inlines"": []
        }
      ]
    }
  ]
}";

            Assert.AreEqual(expected, card.ToJson());
        }
示例#9
0
        public void RichTextBlock()
        {
            AdaptiveTextRun textRun1 = new AdaptiveTextRun
            {
                Color     = ForegroundColor.Accent,
                FontStyle = FontStyle.Monospace,
                IsSubtle  = true,
                Language  = "en",
                Size      = TextSize.Large,
                Text      = "This is text run number 1",
                Weight    = TextWeight.Bolder,
            };

            Assert.AreEqual(ForegroundColor.Accent, textRun1.Color);
            Assert.AreEqual(FontStyle.Monospace, textRun1.FontStyle);
            Assert.AreEqual(true, textRun1.IsSubtle);
            Assert.AreEqual("en", textRun1.Language);
            Assert.AreEqual(TextSize.Large, textRun1.Size);
            Assert.AreEqual("This is text run number 1", textRun1.Text);
            Assert.AreEqual(TextWeight.Bolder, textRun1.Weight);

            textRun1.SelectAction = new AdaptiveSubmitAction
            {
                Title = "Select Action"
            };
            Assert.IsNotNull(textRun1.SelectAction);
            Assert.AreEqual("Select Action", textRun1.SelectAction.Title);

            AdaptiveTextRun textRun2 = new AdaptiveTextRun {
                Text = "This is text run number 2"
            };
            AdaptiveTextRun textRun3 = new AdaptiveTextRun {
                Text = "This is text run number 3"
            };

            AdaptiveParagraph paragraph1 = new AdaptiveParagraph {
            };

            paragraph1.Inlines.Add(textRun1);
            paragraph1.Inlines.Add(textRun2);

            AdaptiveParagraph paragraph2 = new AdaptiveParagraph {
            };

            paragraph2.Inlines.Add(textRun3);

            AdaptiveRichTextBlock richTextBlock = new AdaptiveRichTextBlock
            {
                Height = HeightType.Stretch,
                HorizontalAlignment = HAlignment.Center,
                Id        = "RichTextBlockId",
                IsVisible = false,
                MaxLines  = 3,
                Separator = true,
                Spacing   = Spacing.Large,
                Wrap      = true
            };

            ValidateBaseElementProperties(richTextBlock, "RichTextBlockId", false, true, Spacing.Large, HeightType.Stretch);

            Assert.AreEqual(HAlignment.Center, richTextBlock.HorizontalAlignment);
            Assert.AreEqual <uint>(3, richTextBlock.MaxLines);
            Assert.AreEqual(true, richTextBlock.Wrap);

            richTextBlock.Paragraphs.Add(paragraph1);
            richTextBlock.Paragraphs.Add(paragraph2);

            Assert.AreEqual("This is text run number 1", (richTextBlock.Paragraphs[0].Inlines[0] as AdaptiveTextRun).Text);
            Assert.AreEqual("This is text run number 2", (richTextBlock.Paragraphs[0].Inlines[1] as AdaptiveTextRun).Text);
            Assert.AreEqual("This is text run number 3", (richTextBlock.Paragraphs[1].Inlines[0] as AdaptiveTextRun).Text);

            var jsonString = richTextBlock.ToJson().ToString();

            Assert.AreEqual("{\"height\":\"Stretch\",\"horizontalAlignment\":\"center\",\"id\":\"RichTextBlockId\",\"isVisible\":false,\"maxLines\":3,\"paragraphs\":[{\"inlines\":[{\"color\":\"Accent\",\"fontStyle\":\"Monospace\",\"isSubtle\":true,\"selectAction\":{\"title\":\"Select Action\",\"type\":\"Action.Submit\"},\"size\":\"Large\",\"text\":\"This is text run number 1\",\"type\":\"TextRun\",\"weight\":\"Bolder\"},{\"text\":\"This is text run number 2\",\"type\":\"TextRun\"}]},{\"inlines\":[{\"text\":\"This is text run number 3\",\"type\":\"TextRun\"}]}],\"separator\":true,\"spacing\":\"large\",\"type\":\"RichTextBlock\",\"wrap\":true}", jsonString);
        }