我是一名 C++ 开发人员,最近转向 WPF C# 的世界。我正在开发一个应用程序,我必须使用 fileopendialog 加载文本文件,选择文件并将其保存在组合框中,然后单击 writebutton 我应该使用文本文件中存在的数据执行一些操作。
我在 C++ 中完成了如下操作:
if(button == m_writeButton)
{
// get the data from the file
File m_binFile = m_fileChoice->getCurrentFile();
MemoryBlock m_data;
m_binFile.loadFileAsData(m_data);
size_t blockSize = m_data.getSize();
unsigned char *f_buffer;
f_buffer = (unsigned char *)m_data.getData();
unsigned cnt = 0;
// Some code
}
我在 C# 中这样做如下:
<ComboBox Name="WriteFileCombo" >
<ComboBoxItem Content="Firmware To Download" />
<ComboBoxItem Content="{Binding FirmwarePath}" />
</ComboBox>
<Button Content="..." Command="{Binding Path=WriteFilePathCommand}" Name="FileDialogBtn"/>
<Button Content="Write File" Command="{Binding Path=WriteFileCommand}" Name="WriteFileBtn" />
查看模型类:
private string _selectedFirmware;
public string FirmwarePath
{
get { return _selectedFirmware; }
set
{
_selectedFirmware = value;
OnPropertyChanged("FirmwarePath");
}
}
// Gets called when Browse Button (...) is clicked
private void ExecuteWriteFileDialog()
{
var dialog = new OpenFileDialog { InitialDirectory = _defaultPath };
dialog.DefaultExt = ".txt";
dialog.Filter = "TXT Files (*.txt)|*.txt";
dialog.ShowDialog();
FirmwarePath = dialog.FileName;
WriteFileCommandExecuted();
}
// Gets called when Write Button is clicked
public void WriteFileCommandExecuted()
{
// same logic as in c++
}
如何在我的WriteFileCommandExecuted()
方法中执行在 C++ 代码中完成的相同操作?
请帮忙 :)