2011-01-21 47 views
0

WPF/XAML的新手和正在爲我的應用程序構建命令庫。我的應用程序的當前版本是「扁平的」,意味着所有的代碼都駐留在我的MainWindow.cs文件中。我已決定將事情分離到用戶控件中,目前爲止,所有UI部分都可以很好地工作,但是...WPF創建命令庫

我現在遇到了將我的命令綁定到現在已完成此操作的問題。爲了簡單起見,我創建了一個新的WPF項目,添加了我的Menu控件(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; 

很多謝謝!

回答

0

我想你可以從設置一些設計模式,如MVC或MVVM中受益。

查閱這些框架:

您也可以按照MVC或MVVM只是用了幾個小助手類,看看周圍的ariticles網頁,就像這個MVVM

檢查是否需要輕型框架:MVVM Foundation