我正在开发一个WPF应用程序,我刚刚遇到了加载脚本文件(.xml)操作,我应该从系统加载它,从组合框中获取文件,并调用load方法。
Xaml:
<ComboBox Name="ScriptCombo" SelectedIndex="0" >
<ComboBoxItem Content="Select Aardvark Script" />
<ComboBoxItem Content="{Binding ScriptPath}" />
</ComboBox>
<Button Content="..." Command="{Binding Path=ScriptPathCommand}" Name="ScriptFileDialog" />ViewModel:
private string _ScriptPath;
public string ScriptPath
{
get { return _ScriptPath; }
set
{
_ScriptPath = value;
NotifyPropertyChanged("ScriptPath");
}
}
// Method gets called when ... Button is clicked
private void ExecuteScriptFileDialog()
{
var dialog = new OpenFileDialog { InitialDirectory = _defaultPath };
dialog.DefaultExt = ".xml";
dialog.Filter = "XML Files (*.xml)|*.xml";
dialog.ShowDialog();
ScriptPath = dialog.FileName; //Stores the FileName in ScriptPath
}这将打开一个文件对话框,让我选择一个.xml文件。在这里,当对话框打开时,它不会显示CurrentWorkingDirectory。如何才能做到这一点?因此,在选择when I click Open并将断点放在ScriptPath statemnt附近之后,它会在我的组合框中显示文件的路径。
另外,我希望获得这个文件并将其存储为文件类型,从而调用LoadFile方法。我在C++中是这样做的:
File file = m_selectScript->getCurrentFile(); //m_selectScript is combobox name
if(file.exists())
{
LoadAardvarkScript(file);
}
void LoadAardvarkScript(File file)
{
}在WPf中,我确实喜欢:
ScriptPath = dialog.FileName;
if (File.Exists(ScriptPath))
{
LoadAardvarkScript(ScriptPath);
}
}
public void LoadAardvarkScript(string ScriptPath)
{
MessageBox.Show("Start Reading File");
}我在C++代码中传递文件作为参数,在这里我传递一个字符串。在读取xml文件时是否会产生任何问题?
发布于 2012-10-10 20:00:49
我不完全理解您的问题所在,但是OpenFileDialog最初显示的目录是由它的InitialDirectory属性设置的,因此它就是您放入defaultPath变量中的内容。例如,这可以是System.Environment.CurrentDirectory属性的值。
对于问题的第二部分,在.Net中有一个File类。
https://stackoverflow.com/questions/12817909
复制相似问题