Xamarin.forms Forms:HTML数据的文本到语音特性问题
我使用Xamarin.forms Forms:HTML数据的文本到语音特性问题,xamarin.forms,text-to-speech,xamarin.essentials,Xamarin.forms,Text To Speech,Xamarin.essentials,我使用标签在用户界面上用TextType=“HTML”属性显示HTML数据。该功能运行良好,在UI上HTML内容可以转换为普通文本 我还使用Xamarin Essentials实现了文本到语音功能。当TTS功能启动时,我使用高亮显示相应的文本 当TTS函数启动时,普通文本将转换为HTML数据。如何解决此问题? 屏幕截图: 我上传了一个示例项目供参考。这将是一个预期效果,因为字符串的内容是html格式。作为一种解决方法,您可以使用Regex获取html的内容 public static stri
标签
在用户界面上用TextType=“HTML”
属性显示HTML数据。该功能运行良好,在UI上HTML内容可以转换为普通文本
我还使用Xamarin Essentials
实现了文本到语音功能。当TTS功能启动时,我使用高亮显示相应的文本
当TTS函数启动时,普通文本将转换为HTML数据。如何解决此问题?
屏幕截图:
我上传了一个示例项目供参考。这将是一个预期效果,因为字符串的内容是html格式。作为一种解决方法,您可以使用Regex获取html的内容
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(" ", "").Replace(" ", "").Replace("\n\n\n", "\n");
return html;
}
公共静态字符串GetHtmlText(字符串html)
{
html=System.Text.RegularExpressions.Regex.Replace(html,@“”,System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html=html.Replace(“\r\n”,”).Replace(“\r”,”).Replace(“,”).Replace(“,”).Replace(“\n\n”,“\n”);
返回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(" ", "").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);
}
}
}
}
public分部类主页面:ContentPage
{
字符串[]strList;
公共列表章节详细信息{get;set;}
公共主页()
{
初始化组件();
//普通文本
//string content=“每个平台都支持不同的区域设置,\n以不同的语言和口音回传文本。\n平台具有不同的代码和指定区域设置的方式,\n这就是Xamarin提供跨平台区域设置类以及使用GetLocalesAsync查询这些类的原因。\n”;
//来自epub文件的html文本
chapterDetails=新列表();
string fileName=“Alices Adventures in wonderland.epub”;
var assembly=typeof(主页).GetTypeInfo().assembly;
Stream=assembly.GetManifestResourceStream($“{assembly.GetName().Name}.{fileName}”);
EpubBook-EpubBook=EpubReader.ReadBook(流);
foreach(epubBook.chaptes中的EpubChapter章节)
{
添加(新chapterDetails(){title=chapter.title,htmlData=chapter.HtmlContent,subChapters=chapter.subChapters});
}
字符串内容=GetHtmlText(chapterDetails[0].htmlData);
label.Text=内容;
字符串str=“.”;
char character=char.Parse(str);
字符串str2=“,”;
character2=char.Parse(str2);
字符串str3=“\n”;
character3=char.Parse(str3);
strList=content.Split(新字符[]{character,character2,character3});
}
公共静态字符串GetHtmlText(字符串html)
{
html=System.Text.RegularExpressions.Regex.Replace(html,@“”,System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html=html.Replace(“\r\n”,”).Replace(“\r”,”).Replace(“,”).Replace(“,”).Replace(“\n\n”,“\n”);
返回html;
}
private async void Clicked按钮(对象发送方、事件args args)
{
for(int i=0;i
注意:这样,css的样式将不再有效。您需要自己在span中设置样式(字体或颜色)。在android上运行良好。在ios和windows平台上,当TTS启动时,UI将为空to
label.TextType=TextType.Text
.UI空白问题在添加TextType.Text后得到解决;但在windows中,文本高亮显示不起作用。对于android和ios,单词之间的空格没有正确添加。如果我的回答对你有帮助,你可以接受,我会努力找到更好的解决办法