我是C++開發人員,最近我開始使用WPF。我正在研究一系列組合框和一個按鈕。這裏是代碼:未能在我的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++示例:)
我該如何實現它?
您應該嘗試提供[sscce](http://sscce.org/)。這是很多需要經歷的代碼,我無法看到每件事情都與你的問題有關。 – Default
@默認嗯,我只是想給我一個清晰的圖片,我的組合框,我正在添加的項目,與它相關的屬性以及我需要在按鈕上單擊執行的操作:) –