错误:mscorlib.ni.dll中发生'System.UnauthorizedAccessException'

时间:2016-12-10 03:36:42

标签: c# uwp voice-recognition

我正在为我的Raspberry Pi编写一个UWP C#应用程序。我想创建一个连续的识别会话,但每次我的程序命中await recognizer.ContinuousRecognitionSession.StartAsync();时它都会因为这个错误而失败。

我尝试在我的清单文件中添加行,例如<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />,但无济于事。

我该如何调用此方法?我已经设置了一些先决条件,比如用于调用它的grammar.xml文件,但我仍然找不到问题:

const string SRGS_FILE = "Grammar\\grammar.xml";
    SpeechRecognizer recognizer;

    public MainPage()
    {
        this.InitializeComponent();
        Unloaded += MainPage_Unloaded;
        initializeSpeechRecognizer();
    }

    private async void initializeSpeechRecognizer()
    {
        // Initialize recognizer
        recognizer = new SpeechRecognizer();

        // Set event handlers
        recognizer.StateChanged += RecognizerStateChanged;
        recognizer.ContinuousRecognitionSession.ResultGenerated += RecognizerResultGenerated;
        txbSpeech.Text += "Event handlers set\n";

        // Load Grammer file constraint
        string fileName = String.Format(SRGS_FILE);
        StorageFile grammarContentFile = await Package.Current.InstalledLocation.GetFileAsync(fileName);
        txbSpeech.Text += "Grammar file constraint loaded\n";

        SpeechRecognitionGrammarFileConstraint grammarConstraint = new SpeechRecognitionGrammarFileConstraint(grammarContentFile);

        // Add to grammer constraint
        recognizer.Constraints.Add(grammarConstraint);
        txbSpeech.Text += "Grammar constraint added\n";

        // Compile grammer
        SpeechRecognitionCompilationResult compilationResult = await recognizer.CompileConstraintsAsync();
        txbSpeech.Text += "Grammar compiled\n";

        txbSpeech.Text += ("Status: " + compilationResult.Status.ToString() + "\n");

        // If successful, display the recognition result.
        if (compilationResult.Status == SpeechRecognitionResultStatus.Success)
        {
            txbSpeech.Text += ("Result: " + compilationResult.ToString()) + "\n";

            // The line below fails
            await recognizer.ContinuousRecognitionSession.StartAsync();
        }
        else
        {
            txbSpeech.Text += ("Status: " + compilationResult.Status + "\n");
        }
    }

    private void RecognizerResultGenerated(SpeechContinuousRecognitionSession session, SpeechContinuousRecognitionResultGeneratedEventArgs args)
    {
        // Output debug strings
        txbSpeech.Text += (args.Result.Status.ToString() + "\n");
        txbSpeech.Text += (args.Result.Text + "\n");
    }

    // Recognizer state changed
    private void RecognizerStateChanged(SpeechRecognizer sender, SpeechRecognizerStateChangedEventArgs args)
    {
        //txbSpeech.Text += ("Speech recognizer state: " + args.State.ToString() + "\n");
    }

0 个答案:

没有答案