在WPF主窗口中调用方法 [英] CALLING a method in WPF main window

查看:110
本文介绍了在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.


这篇关于在WPF主窗口中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆