문제

I have the following piece of code for using text to speech feature in Windows Phone 8. I am using ssml, with bookmarks. But when changing any UI element in the Bookmark event called function, raises 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();
            }

But on running it gives the following error

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.

Any idea how to fix this error, or any work around.

도움이 되었습니까?

해결책

Just got the answer.

Since the synthesizer.SpeakSsmlAsync() is an async function, to perform UI operations Dispatcher has to be used, something like this -

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

다른 팁

It's pretty much unrelated to the speech recognition. It seems that it's related to accessing elements which are on the UI thread from a different thread.

Try this:

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

From AppManifest.xml turn on capability ID_CAP_SPEECH_RECOGNITION.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top