在 WPF 中,你可以使用 System.Speech.Synthesis 命名空间中的 SpeechSynthesizer 类来朗读文本。下面是一个简单的示例代码,演示了如何使用 SpeechSynthesizer 类来朗读文本:
using System;
using System.Windows;
using System.Speech.Synthesis;
namespace YourNamespace
{
public partial class YourWindow : Window
{
private SpeechSynthesizer synthesizer;
public YourWindow()
{
InitializeComponent();
synthesizer = new SpeechSynthesizer();
}
private void Speak(string text)
{
synthesizer.SpeakAsync(text);
}
}
}
在这个示例中,我们首先创建了一个 SpeechSynthesizer 对象。然后,在 Speak 方法中,我们调用 synthesizer.SpeakAsync() 方法并传入要朗读的文本。
你可以在需要的时候调用 Speak 方法,并传入你想要朗读的文本。例如:
Speak("Hello, welcome to WPF speech synthesis!");
当调用 Speak 方法时,系统将会朗读传入的文本。