我正在尝试对飞利浦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。
答案 0 :(得分:1)
查看代码中的评论
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,例如看看你的命令是否成功。