C#非静态场、方法或属性-语音合成需要对象引用
我尝试使用此代码构建一个测试应用程序 我定义了一个公共类,如下所示:C#非静态场、方法或属性-语音合成需要对象引用,c#,methods,reference,uwp,C#,Methods,Reference,Uwp,我尝试使用此代码构建一个测试应用程序 我定义了一个公共类,如下所示: public class iSpeech { // Performs synthesis public async Task<IRandomAccessStream> SynthesizeTextToSpeechAsync(string text) { IRandomAccessStream stream = null; using (SpeechSynt
public class iSpeech
{
// Performs synthesis
public async Task<IRandomAccessStream> SynthesizeTextToSpeechAsync(string text)
{
IRandomAccessStream stream = null;
using (SpeechSynthesizer synthesizer = new SpeechSynthesizer())
{
VoiceInformation voiceInfo =
(
from voice in SpeechSynthesizer.AllVoices
where voice.Gender == VoiceGender.Male
select voice
).FirstOrDefault() ?? SpeechSynthesizer.DefaultVoice;
synthesizer.Voice = voiceInfo;
stream = await synthesizer.SynthesizeTextToStreamAsync(text);
}
return (stream);
}
// Build audio stream
public async Task SpeakTextAsync(string text, MediaElement mediaElement)
{
IRandomAccessStream stream = await this.SynthesizeTextToSpeechAsync(text);
await mediaElement.PlayStreamAsync(stream, true);
}
}
我一直在变老
“非静态字段、方法或属性需要对象引用…”错误
有人能告诉我我做错了什么吗?方法SpeakTextAsync中缺少“static”关键字
public static async Task SpeakTextAsync(string text, MediaElement mediaElement)
{
IRandomAccessStream stream = await this.SynthesizeTextToSpeechAsync(text);
await mediaElement.PlayStreamAsync(stream, true);
}
SpeakTextAsync方法中缺少“static”关键字
public static async Task SpeakTextAsync(string text, MediaElement mediaElement)
{
IRandomAccessStream stream = await this.SynthesizeTextToSpeechAsync(text);
await mediaElement.PlayStreamAsync(stream, true);
}
iSpeech
是一个类,但您需要该类的实例才能使用非静态方法
把它想象成列表
。你不能打电话
List<string>.Add("Hello");
然后,mySpeechThing
将是一个变量,表示iSpeech
的一个实例,然后您可以执行以下操作
await mySpeechThing.SpeakTextAsync("test speech", this.uiMediaElement);
有时,一个类有一些方法可以在不修改对象状态的情况下调用(比如在列表上调用Add
,通过向其添加字符串来更改其状态)。我们将这些方法声明为静态
方法。它们属于类,而不是类的实例
要做到这一点,您可以将关键字static
放在方法声明中,如下所示:
public static async Task SpeakTextAsync(string text, MediaElement mediaElement)
然后你就可以用你想用的方法了
static
方法无法访问非静态类属性或方法。尽管有些人可能不同意,但通常最好不要使用静态方法。它们不是邪恶的,但在你更熟悉之前,我会向另一个方向倾斜 iSpeech
是一个类,但您需要该类的实例才能使用非静态方法
把它想象成列表
。你不能打电话
List<string>.Add("Hello");
然后,mySpeechThing
将是一个变量,表示iSpeech
的一个实例,然后您可以执行以下操作
await mySpeechThing.SpeakTextAsync("test speech", this.uiMediaElement);
有时,一个类有一些方法可以在不修改对象状态的情况下调用(比如在列表上调用Add
,通过向其添加字符串来更改其状态)。我们将这些方法声明为静态
方法。它们属于类,而不是类的实例
要做到这一点,您可以将关键字static
放在方法声明中,如下所示:
public static async Task SpeakTextAsync(string text, MediaElement mediaElement)
然后你就可以用你想用的方法了
static
方法无法访问非静态类属性或方法。尽管有些人可能不同意,但通常最好不要使用静态方法。它们不是邪恶的,但在你更熟悉之前,我会向另一个方向倾斜 谢谢你的回复,艾伦。不幸的是,this.synthezettextospeechasync不能是静态的并返回错误。下一个回复解决了这个问题。谢谢你的回复,艾伦。不幸的是,this.synthezettextospeechasync不能是静态的并返回错误。下一个回复解决了这个问题。非常感谢斯科特,这正是我所需要的。var mySpeechThing=new iSpeech();等待mySpeechThing.SpeakTextAsync(“testspeech”,this.uiMediaElement);不客气!令人毛骨悚然的细节:为了使代码更加一致可读,建议让类以大写字母开头。除非它是一个以“I”开头的单词,否则在类型名称前面加上“I”通常用于接口。(但显然没有规定。)非常感谢斯科特,这正是我需要的。var mySpeechThing=new iSpeech();等待mySpeechThing.SpeakTextAsync(“testspeech”,this.uiMediaElement);不客气!令人毛骨悚然的细节:为了使代码更加一致可读,建议让类以大写字母开头。除非它是一个以“I”开头的单词,否则在类型名称前面加上“I”通常用于接口。(但显然没有规则。)