C# 在windows phone 8中取消语音合成

C# 在windows phone 8中取消语音合成,c#,windows-phone-8,text-to-speech,C#,Windows Phone 8,Text To Speech,我将语音合成添加到我的应用程序中。它可以工作,但问题是我不能取消演讲…例如,当我导航到另一个页面时,演讲会继续。。。因此,我调用CancelAll()方法来取消当前的语音,但是发生了异常,我不知道为什么。你知道有什么问题吗 例外 A first chance exception of type 'System.Threading.Tasks.TaskCanceledException' occurred in mscorlib.ni.dll An exception of type 'Syste

我将语音合成添加到我的应用程序中。它可以工作,但问题是我不能取消演讲…例如,当我导航到另一个页面时,演讲会继续。。。因此,我调用CancelAll()方法来取消当前的语音,但是发生了异常,我不知道为什么。你知道有什么问题吗

例外

A first chance exception of type 'System.Threading.Tasks.TaskCanceledException' occurred in mscorlib.ni.dll
An exception of type 'System.Threading.Tasks.TaskCanceledException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
An exception of type 'System.Threading.Tasks.TaskCanceledException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
The program '[2576] TaskHost.exe' has exited with code -1 (0xffffffff).
我的代码:

    private SpeechSynthesizer synth = new SpeechSynthesizer();

    protected override void OnBackKeyPress(CancelEventArgs e)
    {
        //I tried to cancel also here but it's the same exception...
    }

    //method called when I press a button Cancel
    private void ButtonCancelSpeech(object sender, EventArgs eventArgs)
    {
        try
        {
            synth.CancelAll();
        }
        catch (TaskCanceledException)
        {
            //I arrive in this exception
        }
    }

    private async void BtnSpeech_Click(object sender, EventArgs e)
    {
        IEnumerable<VoiceInformation> voices = from voice in InstalledVoices.All
                                                     where voice.Language.Substring(0, 2).Equals(LanguageApp.GetLangage2Characters())
                                                     select voice;
        if (voices.ElementAt(0) != null)
        {
            // Set the voice as identified by the query.
            synth.SetVoice(voices.ElementAt(0));

            await synth.SpeakTextAsync(_place.Description);
        }
    }
private SpeechSynthesizer synth=new SpeechSynthesizer();
BackKeyPress上受保护的覆盖无效(CancelEventArgs e)
{
//我也试着在这里取消,但它是相同的例外。。。
}
//当我按下“取消”按钮时调用的方法
private void ButtonCancelSpeech(对象发送方,EventArgs EventArgs)
{
尝试
{
synth.CancelAll();
}
捕获(TaskCanceledException)
{
//我是特例到达的
}
}
私有异步void BtnSpeech\u单击(对象发送方,事件参数e)
{
IEnumerable voices=来自安装语音中的语音。全部
其中voice.Language.Substring(0,2).Equals(LanguageApp.GetLanguage2Characters())
选择语音;
if(voices.ElementAt(0)!=null)
{
//将语音设置为查询标识的语音。
synth.SetVoice(voices.ElementAt(0));
等待synth.SpeakTextAsync(_place.Description);
}
}

谢谢

因为您想取消异步操作,所以可以使用从
SpeakTextAsync
返回的
iasyncation
而不是使用
wait

private SpeechSynthesizer synth = new SpeechSynthesizer();
private IAsyncAction task;

private void ButtonCancelSpeech(object sender, EventArgs eventArgs)
{
    try
    { 
        //cancel the async task itself
        task.Cancel();
    }
    catch (TaskCanceledException)
    {

    }
    }

private void BtnSpeech_Click(object sender, EventArgs e)
{
    IEnumerable<VoiceInformation> voices = from voice in InstalledVoices.All
                                                     where voice.Language.Substring(0, 2).Equals(LanguageApp.GetLangage2Characters())
                                                     select voice;
    if (voices.ElementAt(0) != null)
    {
        // Set the voice as identified by the query.
        synth.SetVoice(voices.ElementAt(0));

        task = synth.SpeakTextAsync(_place.Description);
    }
}
private SpeechSynthesizer synth=new SpeechSynthesizer();
私用iSyncation任务;
private void ButtonCancelSpeech(对象发送方,EventArgs EventArgs)
{
尝试
{ 
//取消异步任务本身
task.Cancel();
}
捕获(TaskCanceledException)
{
}
}
私有void BtnSpeech\u单击(对象发送者,事件参数e)
{
IEnumerable voices=来自安装语音中的语音。全部
其中voice.Language.Substring(0,2).Equals(LanguageApp.GetLanguage2Characters())
选择语音;
if(voices.ElementAt(0)!=null)
{
//将语音设置为查询标识的语音。
synth.SetVoice(voices.ElementAt(0));
task=synth.SpeakTextAsync(_place.Description);
}
}