Xamarin.Forms:HTML 数据的文本转语音功能问题

Xamarin.Forms: Issue with text to speech feature for HTML data

我正在使用 Label 在 UI 和 TextType="Html" 属性 上显示 HTML 数据。该功能运行良好,在 UI HTML 上内容被转换为普通文本。

我还使用 Xamarin Essentials 实现了文本到语音功能。当 TTS 功能启动时,我使用 .

突出显示相应的文本

当 TTS 功能启动时,普通文本将转换为 HTML 数据。我该如何解决这个问题?

截图:

我已经上传了一个示例项目here以供参考。

这将是预期的效果,因为字符串的内容是 html 格式。作为解决方法,您可以使用 Regex[= 获取 html 的内容20=].

public static string GetHtmlText(string html)
{
   html = System.Text.RegularExpressions.Regex.Replace(html, @"<\/*[^<>]*>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
   html = html.Replace("\r\n", "").Replace("\r", "").Replace("&nbsp;", "").Replace(" ", "").Replace("\n\n\n", "\n");
    return html;
}

因此您可以像下面这样改进代码:

public partial class MainPage : ContentPage
{
    string[] strList;
    public List<ChapterDetails> chapterDetails { get; set; }
    public MainPage()
    {
        InitializeComponent();
        //normal text
        //string content = "Each platform supports different locales,\n to speak back text in different languages and accents.\n Platforms have different codes and ways of specifying the locale, \n  which is why Xamarin provides a cross-platform Locale class and a way to query them with GetLocalesAsync.\n ";

        //html text from epub file
        chapterDetails = new List<ChapterDetails>();
        string fileName = "Alices-Adventures-in-wonderland.epub";
        var assembly = typeof(MainPage).GetTypeInfo().Assembly;
        Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{fileName}");
        EpubBook epubBook = EpubReader.ReadBook(stream);
        foreach (EpubChapter chapter in epubBook.Chapters)
        {
            chapterDetails.Add(new ChapterDetails() { title = chapter.Title, htmlData = chapter.HtmlContent, subChapters = chapter.SubChapters });
        }
        string content = GetHtmlText(chapterDetails[0].htmlData);
        label.Text = content;
        string str = ".";
        char character = char.Parse(str);
        string str2 = ",";
        char character2 = char.Parse(str2);
        string str3 = "\n";
        char character3 = char.Parse(str3);
        strList = content.Split(new char[] { character, character2 , character3});
    }


    public static string GetHtmlText(string html)
    {
        html = System.Text.RegularExpressions.Regex.Replace(html, @"<\/*[^<>]*>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        html = html.Replace("\r\n", "").Replace("\r", "").Replace("&nbsp;", "").Replace(" ", "").Replace("\n\n\n", "\n");
        return html;
    }

    private async void ClickedButton(object Sender, EventArgs args)
    {
        for (int i = 0; i < strList.Length; i++)
        {
            
            if(!string.IsNullOrEmpty(strList[i]))
            {
                string content = strList[i];
                var formattedString = new FormattedString();
                for (int j = 0; j < strList.Length; j++)
                {
                    if (i == j)
                    {
                        formattedString.Spans.Add(new Span { Text = strList[j], ForegroundColor = Color.Black, BackgroundColor = Color.Gray });
                    }
                    else
                    {
                        formattedString.Spans.Add(new Span { Text = strList[j], ForegroundColor = Color.Black, });
                    }
                }

                label.FormattedText = formattedString;
                label.TextType = TextType.Html;
                await TextToSpeech.SpeakAsync(content);
            }
        }
    }
}

注意:这样css的样式就失效了。 span中的样式(字体或颜色)需要自行设置。