如何在异步函数c#UWP结束时启动函数

时间:2017-09-27 16:16:03

标签: c# uwp text-to-speech

我在c#UWP中有这个简单的代码,其中使用Windows.Media.SpeechSynthesis类,应用程序合成第一个字符串,我的问题是在第一个合成完成后合成第二个字符串。我知道有可能通过创建一个包含str1 + str2的唯一字符串,但运行此代码的场景更复杂,这是不可能的。(对不起我的英语水平低)

data <- [!(data$DES6=="F001"),]

2 个答案:

答案 0 :(得分:2)

与往常一样,让async方法返回void代替TaskTask<T>是个坏主意。
当您返回Task时,您只需添加ContinueWith的续集:

public MainPage()
{
    this.InitializeComponent();
    string str1 = "weather data"; 
    Task talkingTask = talk(Textmeteo);
    string str2 = "hello world";
    talkingTask = talkingTask.ContinueWith(completedTask => talk(str2));
}

public async Task talk(string text)
{
    // await...
}

答案 1 :(得分:2)

让方法talk返回Task而不是void

public MainPage()
{
    this.InitializeComponent();
    MakeTalk();
}

private async void MakeTalk()
{
    // Surround by a try catch as we have async void.
    string str1 = "weather data"; 
    await talk(Textmeteo);
    string str2 = "hello world";
    await talk(str2);
}

public async Task talk(string text)
{
   // [...]
}
相关问题