C# 通过ICollection进行迭代
我想遍历设备上安装的所有语音 在TextSoSpeech元数据中,我看到C# 通过ICollection进行迭代,c#,android,xamarin,icollection,C#,Android,Xamarin,Icollection,我想遍历设备上安装的所有语音 在TextSoSpeech元数据中,我看到 namespace Android.Speech.Tts { public class TextToSpeech : Java.Lang.Object { [Obsolete("deprecated")] public virtual ICollection<Voice> Voices { get; } 名称空间Android.Speech.Tts { 公共类T
namespace Android.Speech.Tts
{
public class TextToSpeech : Java.Lang.Object
{
[Obsolete("deprecated")]
public virtual ICollection<Voice> Voices { get; }
名称空间Android.Speech.Tts
{
公共类TextToSpeech:Java.Lang.Object
{
[过时(“弃用”)]
公共虚拟ICollection语音{get;}
即使它已经过时,我还是希望使用“公共虚拟ICollection Voices{get;}”
我不知道如何用Xamarin安装语音
然而,我从未迭代过ICollection
那怎么办
我试着从
ICollection<Voice>nVoices = Android.Speech.Tts.TextToSpeech.
ICollectionnVoices=Android.Speech.Tts.TextToSpeech。
但是“.Voices”不是该名称空间的一部分。
Voices
不是静态属性,这就是为什么您需要一个TextToSpeech
类的实例来迭代它的原因。不过,要获得一个实例,您需要实现该接口:
public class Speaker : Java.Lang.Object, TextToSpeech.IOnInitListener
{
private readonly TextToSpeech speaker;
public Speaker(Context context)
{
speaker = new TextToSpeech(context, this);
// Don't use speaker.Voices here because it hasn't
// been initialized. Wait for OnInit to be called.
}
public void OnInit(OperationResult status)
{
if (status.Equals(OperationResult.Success))
{
// Iterating the collection with a foreach
// is perfectly fine.
foreach (var voice in speaker.Voices)
{
// Do whatever with the voice
}
}
}
}
然后从您的活动中,您可以像这样使用它:
var speaker = new Speaker(this);
您只需使用
foreach(集合中的var x)