我是一名 C++ 开发人员,最近开始研究 WPF。我正在研究一系列组合框和一个按钮。这是代码:
XAML:
<GroupBox Header="EEPROM Version Strings" Grid.Column="1" Height="Auto" HorizontalAlignment="Stretch" Margin="10,0,10,0" Name="groupBox1" VerticalAlignment="Stretch" Width="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Button Content="Write EEPROM" Command="{Binding WriteEEPROMCommand}" Grid.Row="4" Height="23" HorizontalAlignment="Center" Margin="0,0,0,0" Name="WriteEEPROMVersionStrings" VerticalAlignment="Center" Width="116" />
<ComboBox Height="23" ItemsSource="{Binding I2CAddressList}" SelectedItem="{Binding SelectedI2CAddressList, Mode=TwoWay}" SelectedIndex="0" Grid.Row="0" HorizontalAlignment="Center" Margin="0,0,0,0" Name="comboBox1" VerticalAlignment="Center" Width="200" />
<ComboBox Height="23" ItemsSource="{Binding BoardBoxList}" SelectedItem="{Binding SelectedBoardBoxList, Mode=TwoWay}" SelectedIndex="0" Grid.Row="1" HorizontalAlignment="Center" Margin="0,0,0,0" Name="comboBox2" VerticalAlignment="Center" Width="200" />
<ComboBox Height="23" ItemsSource="{Binding VersionBoxList}" SelectedItem="{Binding SelectedVersionBoxList, Mode=TwoWay}" SelectedIndex="0" Grid.Row="2" HorizontalAlignment="Center" Margin="0,0,0,0" Name="comboBox3" VerticalAlignment="Center" Width="200" />
<ComboBox Height="23" ItemsSource="{Binding SerialBoxList}" SelectedItem="{Binding SelectedSerialBoxList, Mode=TwoWay}" SelectedIndex="0" Grid.Row="3" HorizontalAlignment="Center" Margin="0,0,0,0" Name="comboBox4" VerticalAlignment="Center" Width="200" />
</Grid>
</GroupBox>
由于我使用的是 MVVM,这是我的 ViewModel 类:
Byte[] sendBuf = new Byte[256];
Byte[] readBuf = new Byte[256];
public ObservableCollection<string> _I2CAddressList;
public ObservableCollection<string> _BoardBoxList;
public ObservableCollection<string> _VersionBoxList;
public ObservableCollection<string> _SerialBoxList;
在构造函数中:
var myboard = new[]
{
"",
"S1010012", // redhook
"S1010018", // bavaria
"S1010020" // flying dog
};
var myVariant = new[]
{
"",
"001A",
"001B",
"001C",
"002A",
"002B",
"002C",
"003A",
"003B",
"003C",
};
_I2CAddressList = new ObservableCollection<string>();
_BoardBoxList = new ObservableCollection<string>(myboard);
_VersionBoxList = new ObservableCollection<string>(myVariant);
_SerialBoxList = new ObservableCollection<string>();
for (int i = 1; i < 200; i++)
{
_SerialBoxList.Add(i.ToString());
}
//List of I2C Address
_I2CAddressList.Add("0x50");
_I2CAddressList.Add("0x53");
this.SelectedI2CAddressList = this._I2CAddressList[1];
this.SelectedBoardBoxList = this.BoardBoxList[2];
this.SelectedVersionBoxList = this.VersionBoxList[2];
this.SelectedSerialBoxList = this.SerialBoxList[0];
这是组合框和按钮属性:
public ObservableCollection<string> I2CAddressList
{
get { return _I2CAddressList; }
set
{
_I2CAddressList = value;
OnPropertyChanged("I2CAddressList");
}
}
private string _SelectedI2CAddressList;
public string SelectedI2CAddressList
{
get { return _SelectedI2CAddressList; }
set
{
_SelectedI2CAddressList = value;
OnPropertyChanged("SelectedI2CAddressList");
}
}
// Similar property for other comboboxes
private ICommand mWriteEEPROMCommand;
public ICommand WriteEEPROMCommand
{
get
{
if (mWriteEEPROMCommand == null)
mWriteEEPROMCommand = new DelegateCommand(new Action(mWriteEEPROMCommandExecuted), new Func<bool>(mWriteEEPROMCommandCanExecute));
return mWriteEEPROMCommand;
}
set
{
mWriteEEPROMCommand = value;
}
}
public bool mWriteEEPROMCommandCanExecute()
{
return true;
}
char[] version =
{
'A', 'U', 'D', 'I', 'E', 'N', 'C', 'E', // name
'0', '0', '0', '0', '0', '0', '0', '0' , // reserved, firmware size
'0', '0', '0', '0', '0', '0', '0', '0' , // board number
'0', '0', '0', '0', '0', '0', '0', '0' , // variant, version, serial
'0', '0', '0', '0', '0', '0', '0', '0' // date code, reserved
};
public void mWriteEEPROMCommandExecuted()
{
int temp, memloc = 0;
int cmd = 0x0A53;
// fill in the I2C address
if ((I2CAddressList.IndexOf(_SelectedI2CAddressList) + 1) == 1)
{
cmd = 0x0A50;
}
// fill in the address to write to -- 0
sendBuf[memloc++] = 0;
sendBuf[memloc++] = 0;
// fill in the audience header
for (int i = 0; i < 8; i++)
{
sendBuf[i + memloc] = Convert.ToByte(version[i]); // the first 8 bytes
}
memloc += 16; // the 8 copied, plus 8 reserved bytes
temp = (BoardBoxList.IndexOf(_SelectedBoardBoxList) + 1);
if (temp >= 1 && temp <= 3)
{
// How to perform the memory operation here
}
memloc += 8; // move regardless if this was set
}
我无法弄清楚如何在myboard
. 虽然我能够做到这一点version
。这就是我在 C++ 代码中所做的
sendBuf[memloc++] = 0;
sendBuf[memloc++] = 0;
// fill in the audience header
memcpy(sendBuf+memloc, version, 8); // the first 8 bytes
memloc += 16;
temp = m_boardBox->getSelectedId();
if(temp >= 1 && temp <= 3) // a valid board selection
memcpy(sendBuf+memloc, boards[temp], 8);
memloc += 8;
板子是这样的:
static const signed char boards[][9] = {
{}, // left blank to indicate no selection
{ 'S', '1', '0', '1', '0', '0', '1', '2', 0 }, // redhook
{ 'S', '1', '0', '1', '0', '0', '1', '8', 0 }, // bavaria
{ 'S', '1', '0', '1', '0', '0', '2', '0', 0 }, // flying dog
};
因此,当我单击 WRITEEEPROM 按钮时,会执行一系列代码。我在某种程度上是成功的,但是当我遇到如何为字符串数组复制内存时,我无法解决它。我已经展示了上面的 C++ 示例 :)
我怎样才能实现它???