【发布时间】:2011-01-21 21:53:57
【问题描述】:
WPF/XAML 新手,希望为我的应用程序构建命令库。我的应用程序的当前版本是“平面”的,这意味着后面的所有代码都驻留在我的 MainWindow.cs 文件中。我已决定将内容分离到用户控件中,并且到目前为止所有 UI 部分都运行良好,但是....
现在我已经完成了将我的命令绑定到事物的问题。为了简单起见,我创建了一个新的 WPF 项目,添加了我的菜单控件 (Controls\Menu.xaml) 并在我的 MainWindow.xaml 中引用了它。现在,我继续添加了一个 CommandLibrary 类,但似乎没有任何效果。这是我的文件 -> 新命令代码:
public static class MyAppCommands
{
private static RoutedUICommand _NewItem;
static MyAppCommands()
{
_NewItem = new RoutedUICommand("Create a new project", "NewItem", typeof(MyAppCommands));
}
public static RoutedUICommand NewItem
{
get { return _NewItem; }
}
private string filePath = null;
private bool dataChanged = false;
public static void NewItem_Executed(object sender, ExecutedRoutedEventArgs e)
{
if (dataChanged)
{
string sf = SaveFirst();
if (sf != "Cancel")
{
ClearState();
}
}
else
{
ClearState();
}
}
public static void NewItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
public static void BindCommandsToWindow(Window window)
{
window.CommandBindings.Add(new CommandBinding(NewItem, NewItem_Executed, NewItem_CanExecute));
}
我应该将以下内容放在课堂上的什么位置,因为我的很多命令都将使用它们...?
private string filePath = null;
private bool dataChanged = false;
非常感谢!
【问题讨论】: