我正在用C#编程,在使用xaml.cs代码隐藏时我遇到了一个非常奇怪的错误。我找到了一个解决方法,但这是一个巨大的黑客攻击:
这不起作用 - 无声地失败。当我输入断点时,当我尝试观察fileList时出现此错误:
“当前上下文中不存在fileList。”
void item_Selected(object sender, RoutedEventArgs e) {
// Does not work even if on two separate lines.
IList<string> fileList = new List<string>();
TreeViewItem _item = (TreeViewItem)sender;
FileName = _item.Tag as string;
fileList = Directory.GetFiles(FileName, "*.extension").ToList();
// Other stuff to be done with fileList here, which has not been coded yet.
OnDirectoryChanged(FileName, EventArgs.Empty); // Breakpoint here
}
这是有效的黑客:
IList<string> fileList;
void item_Selected(object sender, RoutedEventArgs e) {
fileList = new List<string>();
TreeViewItem _item = (TreeViewItem)sender;
FileName = _item.Tag as string;
fileList = Directory.GetFiles(FileName, "*.xls?").ToList();
// Other stuff to be done with fileList here, which has not been coded yet.
OnDirectoryChanged(FileName, EventArgs.Empty); // Breakpoint here
}
我想知道的是:为什么?