在编程Philips Hue时获取编译器错误[CS0103]错误



我正在尝试编程飞利浦色调灯泡,但我什至无法将命令发送到灯光上。我正在用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。

请参阅Code

中的注释
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,如静态函数。可能是您需要在"客户端"实例上调用此课程:

public partial class Form1 : Form
{
     private ILocalHueClient client
     ....
    private void button1_Click(object sender, EventArgs e)
    {
        client.SendCommandAsync(command);
    }

和" sendCommand async "提示它是一种异步方法。因此,您可能需要等待它:

    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,例如查看您的命令是否成功。

相关内容

最新更新