Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# API扩展中的对象链接与方法_C#_Oop_Mindstorms - Fatal编程技术网

C# API扩展中的对象链接与方法

C# API扩展中的对象链接与方法,c#,oop,mindstorms,C#,Oop,Mindstorms,因此,我希望根据自己的需要扩展/调整API。我说的是Lego Mindstorms C#API。我正在围绕它构建自己的API(基于适配器模式),这样我就可以用更好的OO方式编程机器人 以下是有关API工作原理的链接: 但现在我陷入了C#API处理砖块命令的一种非常奇怪的方式 绝对不是OO的方式 示例:要向程序块发送命令,您需要一个程序块实例来发送命令。但是DirectCommand实例与砖块无关 await brick.DirectCommand.TurnMotorAtPowerAsync(Ou

因此,我希望根据自己的需要扩展/调整API。我说的是Lego Mindstorms C#API。我正在围绕它构建自己的API(基于适配器模式),这样我就可以用更好的OO方式编程机器人

以下是有关API工作原理的链接:

但现在我陷入了C#API处理砖块命令的一种非常奇怪的方式

绝对不是OO的方式

示例:要向程序块发送命令,您需要一个程序块实例来发送命令。但是DirectCommand实例与砖块无关

await brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A, 50, 5000);
因此,我想做的是使brick和DirectCommand松散耦合

下面是另一个示例:执行一批命令。您必须写出所有命令,然后执行某个方法。在当前的API中,无法循环通过数组并向它们添加堆栈元素,以便在以后执行它们

brick.BatchCommand.TurnMotorAtSpeedForTime(OutputPort.A, 50, 1000, false);
brick.BatchCommand.TurnMotorAtPowerForTime(OutputPort.C, 50, 1000, false);
brick.BatchCommand.PlayTone(50, 1000, 500);
await brick.BatchCommand.SendCommandAsync();
所以我想做的事情是:

创建一个类似PlayTone(..)的命令,将其添加到命令的arrayList中,然后在其中循环

List<Command> toBeExecuted = new List<Command>;
toBeExecuted.Add(DirectCommand.PlayTone(50, 1000, 500));
brick.DirectCommand(toBeExecuted[0]);
List toBeExecuted=新列表;
toBeExecuted.Add(DirectCommand.PlayTone(501000500));
DirectCommand(toBeExecuted[0]);

所以如果有人能帮忙。。。我会非常高兴:)

它们的设计目的并不完全相同,但您能将它们作为任务列表进行排队,并将其传递给其他人吗

像这样:

static void Main(string[] args)
{
    //---- queue commands into a "batch"
    List<Task> toBeExecuted  = new List<Task>();
    toBeExecuted.Add(Task.Run(() => dothing()));
    toBeExecuted.Add(Task.Run(() => dothing()));
    toBeExecuted.Add(Task.Run(() => dothing()));
    toBeExecuted.Add(Task.Run(() => dothing()));
    toBeExecuted.Add(Task.Run(() => dothing()));


    //---- elsewhere
    Task.WaitAll(toBeExecuted.ToArray()); //fire off the batch
    await brick.BatchCommand.SendCommandAsync(); //send to brick
}
static void Main(字符串[]args)
{
//----将命令排入“批处理”队列
List toBeExecuted=新列表();
toBeExecuted.Add(Task.Run(()=>dothing());
toBeExecuted.Add(Task.Run(()=>dothing());
toBeExecuted.Add(Task.Run(()=>dothing());
toBeExecuted.Add(Task.Run(()=>dothing());
toBeExecuted.Add(Task.Run(()=>dothing());
//----别处
Task.WaitAll(toBeExecuted.ToArray());//触发批处理
等待brick.BatchCommand.SendCommandAsync();//发送到brick
}
将dothing()替换为要排队的batchcommand:


.Add(Task.Run(()=>brick.BatchCommand…())

是的,这是个好主意。这个实现的唯一问题是,我不能将Command类的实例与brick对象分开使用。这是我想做的主要事情之一,因为我希望能够独立于稍后将执行的命令块来实现命令。