抱歉,我的问题可能很愚蠢,但我无法解决:现在我已经编写了一个菜单的UserControl,并且有一个关于打开项目的命令(我使用EventtoCommand将其切换到事件OpenProject),以及此UserControl效果很好,这是MainWindow.xaml中的代码
<uc:MenuControl Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3"
DataContext="{Binding Menu}"
ProjectExist="{Binding ProjectExist}"
Path="{Binding SelectedPath}"
HistorySource="{Binding HistorySource}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="NewProject">
<command:EventToCommand Command="{Binding NewProjectHandle}"></command:EventToCommand>
</i:EventTrigger>
<i:EventTrigger EventName="ProjectConfig">
<command:EventToCommand Command="{Binding ProjectConfigHandle}"></command:EventToCommand>
</i:EventTrigger>
<i:EventTrigger EventName="OpenProject">
<command:EventToCommand Command="{Binding OpenProjectHandle}"></command:EventToCommand>
</i:EventTrigger>
<i:EventTrigger EventName="OpenHistory">
<command:EventToCommand Command="{Binding OpenHistoryHandle}"></command:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</uc:MenuControl>
您看到我已将Event OpenProject绑定到名为OpenProjectHandle的RelayCommand,这是MenuViewModel中的代码:
public MenuViewModel()
{
OpenProjectHandle = new RelayCommand(() =>
{
//OpenProject
});
}
当然我已经定义了OpenProjectHandle,但是当我调试时我可以看到程序提升事件OpenProject,但OpenProjectHandle没有激发,任何人都知道哪里出错了?
=============================================== ============= 这是OpenProjectHandle:
_projectConfig = ProjectConfigModel.Instance();
HistorySource = new ObservableCollection<HistorySection>();
XmlDocument HisFile = new XmlDocument();
HisFile.Load("D:\\history");
XmlNode Projects = HisFile.SelectSingleNode("Project");
XmlNodeList paths = Projects.ChildNodes;
foreach (XmlNode path in paths) {
HistorySource.Add(new HistorySection(path.InnerText));
}
OpenProjectHandle = new RelayCommand(() =>
{
OpenFileDialog dialog = new OpenFileDialog()
{
Title = "请选择工程文件",
Filter = "工程文件(*.project)|*.project"
};
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
_projectConfig.Path = dialog.FileName;
}
else {
return;
}
XmlDocument ProjFile = new XmlDocument();
ProjFile.Load(_projectConfig.Path);
XmlNode root = ProjFile.SelectSingleNode("Project");
XmlNode database = root.SelectSingleNode("DataBase");
XmlNode resource = root.SelectSingleNode("Resource");
_projectConfig.ProjectName = getValue(root, "Name");
_projectConfig.ProjectPath = getValue(root, "ProjectPath");
_projectConfig.DatabaseIP = getValue(database, "DataBaseIP");
_projectConfig.DatabasePort = getValue(database, "DataBasePort");
_projectConfig.DatabaseName = getValue(database, "DataBaseName");
_projectConfig.DatabaseUserName = getValue(database, "DataBaseUserName");
_projectConfig.DatabasePassword = getValue(database, "DataBasePassword");
_projectConfig.MapSourcePath = getValue(resource, "MapSourcePath");
_projectConfig.MapSourceOutputPath = getValue(resource, "MapSourceOutputPath");
_projectConfig.NpcPicturePath = getValue(resource, "NpcPicturePath");
_projectConfig.MonsterPicturePath = getValue(resource, "MonsterPicturePath");
_projectConfig.MapSoundPath = getValue(resource, "MapSoundPath");
HisFile.Load("D:\\history");
Projects = HisFile.SelectSingleNode("Project");
Paths = InProjects.ChildNodes;
bool existed = false;
foreach (XmlNode path in Paths) {
if (path.InnerText == _projectConfig.Path) {
existed = true;
}
}
if (!existed) {
XmlNode xmle = HisFile.CreateElement("Path");
xmle.InnerText = _projectConfig.Path;
Projects.AppendChild(xmle);
}
HisFile.Save("D:\\history");
ProjectExist = true;
ProjectConfigModel.SetCurrentInstance();
ProjectConfigModel.SetNewInstance();
string getValue(XmlNode father, string str) {
return father.SelectSingleNode(str).InnerText;
}
});
所有这些都在MenuViewModel的public MenuViewModel()
构造函数
答案 0 :(得分:0)
这个问题是MVVMLight的一个错误,我们不能使用RelayCommand中未定义的变量(我使用过MenuViewModel定义的XmlDocument)