对 Philips HUE 进行编程时出现 Getting Compiler Error [CS0103] 错误

Getting Compiler Error [CS0103] error when programming the Philips HUE

我正在尝试对飞利浦 HUE 灯泡进行编程,但我什至无法向灯发送命令。我正在使用 Q42.HueApi.

在 C# 中编程

如果我在我的 WinForms 应用程序中按下一个按钮,我就是这样尝试打开灯的:

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            IBridgeLocator locator = new HttpBridgeLocator();
            ILocalHueClient client = new LocalHueClient("10.1.1.150");

            string AppKey = "myappkey";
            client.Initialize(AppKey);
        }

        void commandCreation(object sender, EventArgs e)
        {
        var command = new LightCommand();
        command.On = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ILocalHueClient.SendCommandAsync(command);
        }
    }
}

但是在最后一行我得到了编译器错误 CS0103

查看代码中的注释

void commandCreation(object sender, EventArgs e)
{
    var command = new LightCommand(); //  <== because you declare it HERE
    command.On = true;
}

private void button1_Click(object sender, EventArgs e)
{
        ILocalHueClient.SendCommandAsync(command); // ^^ command is out of scope HERE.
}

此外,您似乎像调用静态函数一样调用 SendCommandAsync。 可能是您需要在 'client' 实例上调用它,您应该创建一个 class 字段:

public partial class Form1 : Form
{

     private ILocalHueClient client
     ....

    private void button1_Click(object sender, EventArgs e)
    {
        client.SendCommandAsync(command);
    }

并且 "SendCommandAsync" 暗示它是一个异步方法。所以你可能想要等待它:

    private async void button1_Click(object sender, EventArgs e)
    {
        // assuming command is a field ...
        await client.SendCommandAsync(command);
    }

编辑:

实际上是

public Task<HueResults> SendCommandAsync(
              LightCommand command, 
              IEnumerable<string> lightList = null)

因此您甚至可以探索 HueResults,例如查看您的命令是否成功。