我从主方法调用:
public MainPage()
{
Text_to_Speech.changetospeech("Welcome to Nepal!", newmedia).Wait();
mytxtblck.Text="Hello from Nepal!"
}
我真正想做的是等到“欢迎来到尼泊尔”被说出来,然后在mytextblck
中写入“Hello”。
我已经去了几个线程并努力工作,但没有任何东西能够让它工作。
public async static Task changetospeech(string text, MediaElement mediaa)
{
var synth = new SpeechSynthesizer();
var voices = SpeechSynthesizer.AllVoices;
synth.Voice = voices.First(x => x.Gender == VoiceGender.Female );
SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync(text);
MediaElement media = mediaa;
media.SetSource(stream,stream.ContentType);
media.Play();
}
Task.FromResult
比TaskCompletionSource
更好吗? - Mrinal KambojTask.FromResult
是TaskCompletionSource
的替代品,但现在看来只适用于模拟和单元测试。 - Mrinal KambojTask.OfResult
,如果需要计算结果,则可以调用实际的异步方法。 - Jon Skeet