在WPF主窗口中调用方法
我遇到了问题,我想打开主窗口然后我希望窗口说欢迎使用电子邮件系统。我不知道出了什么问题,它先说出来然后出现了窗口,我希望窗口出现然后说出消息
我的代码
公共部分班级MainWindow:MetroWindow
{
私人SpeechRecognitionEngine sre;
private SpeechSynthesizer synth;
public MainWindow()
{
InitializeComponent();
synth = new SpeechSynthesizer();
sre = new SpeechRecognitionEngine();
speak();
}
public void speak()
{
synth.SetOutputToDefaultAudioDevice();
synth.Speak(欢迎使用电子邮件系统);
我尝试了什么:
在同一类和calle的方法中生成对象d主窗口,没有工作
I am having a problem , I want to open main window and then i want the window to speak "welcome to email system " . I dont know what is wrong , it speaks this first and then the window appears , i want the window to appear and then say the message
my code
public partial class MainWindow : MetroWindow
{
private SpeechRecognitionEngine sre;
private SpeechSynthesizer synth;
public MainWindow()
{
InitializeComponent();
synth = new SpeechSynthesizer();
sre = new SpeechRecognitionEngine();
speak();
}
public void speak()
{
synth.SetOutputToDefaultAudioDevice();
synth.Speak("Welcome to email System");
What I have tried:
made object in the method of the same class and called the main window, didnt work
Speak是一个阻塞操作 - 在方法完成之前它不会返回: SpeechSynthesizer.Speak Method(String)(System.Speech.Synthesis) [ ^ ]
您可以使用异步方法替换它: SpeechSynthesizer.SpeakAsync Method(String)( System.Speech.Synthesis) [ ^ ]这可能有所帮助,但我首先将代码移动到ContentRendered事件而不是将其放在构造函数中,然后将其设为asnyc。
Speak is a blocking operation - it doesn't return until the method completes: SpeechSynthesizer.Speak Method (String) (System.Speech.Synthesis)[^]
You could replace it with the asynchronous method: SpeechSynthesizer.SpeakAsync Method (String) (System.Speech.Synthesis)[^] which may help, but I'd start by moving the code to the ContentRendered event rather than putting it in the constructor and then make it asnyc.