private static Inline FormatInlineTextRun(AdaptiveRichTextBlock.AdaptiveParagraph.AdaptiveTextRun textRun, AdaptiveRenderContext context)
        {
            Marked marked = new Marked();

            marked.Options.Renderer = new AdaptiveXamlMarkdownRenderer();
            marked.Options.Mangle   = false;
            marked.Options.Sanitize = true;

            // Handle Date/Time parsing
            string text = RendererUtilities.ApplyTextFunctions(textRun.Text, context.Lang);

            // Handle markdown
            string       xaml         = $"<Span  xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"  xml:space=\"preserve\">{marked.Parse(text)}</Span>";
            StringReader stringReader = new StringReader(xaml);
            XmlReader    xmlReader    = XmlReader.Create(stringReader);

            Span uiInlineElement = XamlReader.Load(xmlReader) as Span;

            uiInlineElement.Style = context.GetStyle($"Adaptive.{textRun.Type}");

            uiInlineElement.FontFamily = new FontFamily(context.Config.GetFontFamily(textRun.FontStyle));
            uiInlineElement.FontWeight = FontWeight.FromOpenTypeWeight(context.Config.GetFontWeight(textRun.FontStyle, textRun.Weight));
            uiInlineElement.FontSize   = context.Config.GetFontSize(textRun.FontStyle, textRun.Size);

            uiInlineElement.SetColor(textRun.Color, textRun.IsSubtle, context);

            return(uiInlineElement);
        }
Пример #2
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 AdaptiveRichTextBlock.AdaptiveParagraph();

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

            paragraph1.Inlines.Add(textRun1);

            var textRun2 = new AdaptiveRichTextBlock.AdaptiveParagraph.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 AdaptiveRichTextBlock.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());
        }