2012-10-30 15 views
1

我是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++示例:)

我該如何實現它?

+1

您應該嘗試提供[sscce](http://sscce.org/)。這是很多需要經歷的代碼,我無法看到每件事情都與你的問題有關。 – Default

+0

@默認嗯,我只是想給我一個清晰的圖片,我的組合框,我正在添加的項目,與它相關的屬性以及我需要在按鈕上單擊執行的操作:) –

回答

1

我不完全相信,如果這是你在找什麼,但我會想試試。

字符串使用System.Text.Encoding類編碼。此類提供方法,使用指定的編碼到一個字符串轉換爲字節數組轉換:

Byte[] encodedBytes = System.Text.Encoding.ASCII.GetBytes("string"); 

幾個編碼由框架本身,例如提供ASCII,UTF7,UTF8,Unicode等。如果你需要一個特殊的編碼,你必須自己使用Encoding作爲你的基類來實現它。

生成的字節數組可以使用System.Buffer複製到您的發送緩衝區中。

+0

那是對的。如果你看看'temp = m_boardBox-> getSelectedId();如果(temp> = 1 && temp <= 3) memcpy(sendBuf + memloc,boards [temp],8);'你會發現我訪問'boards'裏面的字符串。假設'temp = 2',它會從中選擇'S1010018'並執行'memcpy'。牢記這一點,我如何從字符串數組中選擇一個項目然後進行編碼? :)謝謝,如果你可以用代碼演示它:) –

+0

@StevenWilson要從字符串數組中選擇一個項目,你可以使用'string s = arrayOfString [itemID];' – PVitt

+0

如果你注意到我的myboard是在構造函數中。因爲我試圖在我的方法中訪問它,例如字符串s = myboard [2],它說'myboard'不存在於此上下文中。 –

1

在.NET中,System.Buffer包含一堆用於複製緩衝區的方法。

http://msdn.microsoft.com/en-us/library/system.buffer.aspx

+0

看看我爲「版本「,然後看看'myboard'。我無法弄清楚如何使用執行類似的操作(如果您可以用示例代碼演示此問題,我將不勝感激:) –

相關問題