在Windows Phone 8中具有文本到语音的System.UnauthorizedAccessException

时间:2014-03-07 14:53:09

标签: windows-phone-8 text-to-speech

我在Windows Phone 8中使用了以下代码来使用文本到语音功能。我正在使用带书签的ssml。但是当更改名为function的Bookmark事件中的任何UI元素时,会引发Unauthorized Exception。

private void Initialise_synthesizer()
        {
            this.synthesizer = new SpeechSynthesizer();

            synthesizer.BookmarkReached += new TypedEventHandler<SpeechSynthesizer, SpeechBookmarkReachedEventArgs>
                (BookmarkReached);
        }

void BookmarkReached(object sender, SpeechBookmarkReachedEventArgs e)
        {
            Debugger.Log(1, "Info", e.Bookmark + " mark reached\n");

            switch (e.Bookmark)
            {
                case "START":
                    cur = start;
                    break;
                case "LINE_BREAK":
                    cur++;
                    break;
                }
**error here**  t1.Text = cur.ToString();
            }

但在运行时会出现以下错误

A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll
An exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary
Invalid cross-thread access.

任何想法如何解决此错误,或任何解决方法。

3 个答案:

答案 0 :(得分:2)

得到了答案。

由于synthesizer.SpeakSsmlAsync()是一个异步函数,要执行UI操作,必须使用Dispatcher,如下所示:

Dispatcher.BeginInvoke(() =>
                t1.Text = cur.ToString());

答案 1 :(得分:1)

它几乎与语音识别无关。它似乎与访问来自不同线程的UI线程上的元素有关。

试试这个:

Dispatcher.BeginInvoke(() => 
    {
        t1.Text = cur.ToString();
    }
);

答案 2 :(得分:0)

从AppManifest.xml打开功能ID_CAP_SPEECH_RECOGNITION。