C#如何用字段而不是类型创建if / else语句

时间:2017-05-16 21:40:14

标签: c#

我需要帮助,我需要为我的屏幕录像机制作一个音频按钮。我需要用字段

创建一个if / else语句
private void Audiotext_Click(object sender, EventArgs e)
{
    Audiotext.Text = "Audio: ON";
    EncoderDevice AudioDevices()
    {
        EncoderDevice foundDevice = null;
        Collection<EncoderDevice> audioDevices = EncoderDevices.FindDevices(EncoderDeviceType.Audio);
        try
        {
            foundDevice = audioDevices.First();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Cannot Find Perfered Audio Device Using " + audioDevices[0].Name + ex.Message);
        }
        return foundDevice;
    }
    ----->  if (Audiotext.Text == "Audio: ON")  <-----
    {

    }

它说错误是'Armored_Screen_Recorder.Form1.Audiotext'是一个字段但是像一个类型一样使用。你能帮助我吗?

1 个答案:

答案 0 :(得分:1)

  

您似乎设法将方法复制/粘贴到另一个方法中。一世   认为这就是你想要的:

private void Audiotext_Click(object sender, EventArgs e)
{
    Audiotext.Text = "Audio: ON";

    if (Audiotext.Text == "Audio: ON")
    {
    }
}

EncoderDevice AudioDevices()
{
    EncoderDevice foundDevice = null;
    Collection<EncoderDevice> audioDevices = EncoderDevices.FindDevices(EncoderDeviceType.Audio);
    try
    {
        foundDevice = audioDevices.First();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Cannot Find Perfered Audio Device Using " + audioDevices[0].Name + ex.Message);
    }
    return foundDevice;
}

但是,我不确定你想从哪里调用AudioDevices。把AudioDevices()放在那里