私は 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++ サンプルを示しました :)
どうすれば達成できますか?