字典查找-特定字符串
本文关键字:字符串 查找 字典 | 更新日期: 2023-09-27 17:53:07
我想在512个dmx通道中控制一种颜色(在这个例子中是"红色")。我从xml文件中读取红色并将其放入Dictionary中:
<Channel Id="Lamp1Red" Key="2"/>
<Channel Id="Lamp1Green" Key="3"/>
<Channel Id="Lamp1Blue" Key="4"/>
<Channel Id="Lamp2Red" Key="5"/>
<Channel Id="Lamp2Green" Key="6"/>
<Channel Id="Lamp2Blue" Key="7"/>
<Channel Id="Lamp3Red" Key="8"/>
etc. ... up to 512 keys/channels.
我有以下Dictionary
,其中包含id(字符串)和通道(键+值)
public Dictionary<string, Tuple<byte, byte>> DmxChannelsDictionary
{
get;
set;
}
我想查找包含ID"LampXRed"的所有字符串(红色),并获得每个字符串的键(2,5,8),并在以下方法SetColor中使用它们:
SetColor(red, green, blue);
public void SetColor(Tuple<byte, byte> redTuple, Tuple<byte, byte> greenTuple, Tuple<byte, byte> blueTuple)
SetColor将元组传递给DmxDataMessage()
public static byte[] DmxDataMessage(Tuple<byte, byte> redTuple, Tuple<byte, byte> greenTuple, Tuple<byte, byte> blueTuple)
{
//Initialize DMX-buffer: Must be full buffer (512Bytes)
var dmxBuffer = new byte[512];
dmxBuffer.Initialize();
// Fill DATA Buffer: Item1 (Key/Channel) = Item2 (Value)
// Channel1
dmxBuffer[redTuple.Item1] = redTuple.Item2;
dmxBuffer[greenTuple.Item1] = greenTuple.Item2;
dmxBuffer[blueTuple.Item1] = blueTuple.Item2;
// Here I need a foreach or something else to set the the value for each channel (up to 512)
....
我如何在字典中进行智能搜索/交互并保存所有红色id +键以供SetColor()
使用??
对于一个"红色"通道,我是这样做的:
var red = DmxChannelsDictionary["Lamp1Red"];
red = Tuple.Create(red.Item1, _redValue); // _redValue = 0-255
我希望这有意义。非常感谢你的帮助!
为什么不这样呢:
class LampInfo
{
int Red{get;set;}
int Green{get;set;}
int Blue{get;set;}
}
,然后将灯名(例如Lamp1)映射到它:
Dictionary<string,LampInfo> dmxChannelsDictionary;
要填充,您可以这样做:
那么你可以这样做:
Lamp lamp=new LampInfo(){Red=2, Green=3, Blue=4}'
dmxChannelsDictionary.Add("Lamp1",lamp);
然后你只需要输入:
var lamp=dmxChannelsDictionary["Lamp1"];
int red=lamp.Red;
下面修复了我的问题-非常感谢您的帮助。
public static byte[] DmxDataMessage(Tuple<byte, byte> redTuple, Tuple<byte, byte> greenTuple, Tuple<byte, byte> blueTuple, EnttecDmxController _enttecDmxControllerInterface)
{
foreach (KeyValuePair<string, Tuple<byte, byte>> entry in _enttecDmxControllerInterface.DmxChannelsDictionary)
{
if (entry.Key.Contains("Red"))
{
dmxBuffer[entry.Value.Item1] = redTuple.Item2;
}
}
.......
}