2011-10-05 34 views
1

兩個視圖(Windows)中我開發了具有2種模式切換在MVVM WPF

  1. 配置模式
  2. 執行模式

Boths模式將在新窗口中打開的應用程序。我已經創建了配置窗口。

我需要通過按Key F12或類似的東西來切換2模式....我也必須提示用戶輸入密碼時,從執行切換到配置模式(只需一次durnig會話)我已經輸入密碼屏幕並在配置中實施。

我也很擔心,因爲我已經使用Messenger(Meaditor模式),因此關閉並打開2個不同的窗口將再次註冊代表再次登錄代表...並且我正在啓動Modal windows

我們還需要保持兩個視圖活着或我可以殺死其中一個切換。

完全糊塗了有關實現的...

我的App.xaml代碼

/// <summary> 
    /// Application_Startup 
    /// </summary> 
    /// <param name = "sender"></param> 
    /// <param name = "e"></param> 
    private void Application_Startup(object sender, StartupEventArgs e) 
    { 
     log.Debug("Application_Startup " + Utility.FUNCTION_ENTERED_LOG); 
     try 
     { 

       if (e.Args.Length == 0) 
       { 
        AboutDialog.SpashScreen(Utility.TOOL_NAME, 
              Utility.TOOL_VERSION); 
        MainView mainForm = new MainView(); 
        mainForm.ShowDialog(); 
       } 
       else 
       { 
        string key = null; 
        foreach (string arg in e.Args) 
        { 
         if (arg.StartsWith("-")) 
         { 
          //this is a key   
          key = arg; 
          if (key.Equals("-config")) 
          { 
           CommandLineArgs.Add(key, "config"); 
           break; 
          } 
          if (key.Equals("-start")) 
          { 
           CommandLineArgs.Add(key, "start"); 
          } 
         } 
         else 
         { 
          //should be a value   
          if (key == null) 
          { 
           throw new Exception(
            "The command line arguments are malformed."); 
          } 
          CommandLineArgs.Add(key, arg); 
          key = null; 
         } 
        } 
        string config = string.Empty; 
        foreach (object k in App.CommandLineArgs.Keys) 
        { 
         config += CommandLineArgs[k].ToString(); 
        } 

        switch (config) 
        { 
         case "config": 
          AboutDialog.SpashScreen(
           Utility.TOOL_NAME, 
           Utility.TOOL_VERSION); 
          MainView mainForm = new MainView(); 
          mainForm.ShowDialog(); 
          break; 
         case "start" : 
           ExecutionForm execuForm= new ExecutionForm(); 
           execuForm.ShowDialog(); 
           break; 
         default: 
          MessageBox.Show("Incorrect Parameters", 
              Utility.TOOL_NAME); 
          Application.Current.Shutdown(); 
          break; 
        } 

      } 
      log.Debug("Application_Startup" + Utility.FUNCTION_EXIT_LOG); 
     } 
     catch (Exception ex) 
     {    
      log.Error("Application_Startup" + ex.Message, ex); 
     } 
    } 
+1

感謝您的投票...但至少有禮貌告訴我爲什麼... – Ankesh

+0

這裏沒有真正的問題,除了一些代碼... –

回答

4

你實現does not看起來完全錯誤的我。

如果我給出的要求由按鍵上的模態窗口兩種觀點之間,然後等維護我會去的MVVM方式...

  1. 使用單模式是situationally託管多個內容窗口。
  2. 在窗口上有一個ContentControl。將Content綁定到DataContext

    <DockPanel LastChildFill="true"> 
        <Menu DockPanel.Dock="Top" ...> 
        </Menu> 
        <ContentControl Content="{Binding}" ... /> 
    </DockPanel> 
    
  3. 創建兩個數據模板作爲資源。一個數據模板對應於「配置」的觀點和其它數據模板是「執行」視圖。

    <Window.Resources> 
        <DataTemplate x:Key="ConfigView" ... /> 
        <DataTemplate x:Key="ExecutionView" ... /> 
    </Window.Resources> 
    

你也可以在不同的資源文件中的這些數據模板和合並的資源字典的窗口資源。

  1. 與可寫Mode屬性創建一個單一的視圖模型。有INotifyPropertyChanged的實施。

    public class ViewModel : INotifPropertyChanged 
    { 
        private string mode; 
    
        public string Mode 
        { 
         get { return mode; } 
         set { mode = value; NotifPropertyChanged("Mode"); } 
        } 
    
        private IComamnd changeModeCommand; 
    
        public ICommand ChangeModeCommand 
        { 
         get 
         { 
          if (changeModeCommand == null) 
           changeModeCommand 
            = new DelegateCommand(
             OnModeChange, 
             CanModeChange); 
          return changeModeCommand; 
         } 
        } 
    
        //// Other properties and functions go here ... 
    } 
    

在上面的代碼DelegateCommand不在內部找到.Net API。從互聯網上獲得他們的實施。

  1. 註冊一個KeyBindingF12在窗口上。 F12 KeyBindingCommand來自ViewModel

    <Window.InputBindings> 
        <KeyBinding Command="{Binding ChangeModeCommand}" 
           Key="F12" /> 
    </Window.InputBindings> 
    

在到.NET 4.0之前的版本中,Command屬性不綁定。爲此使用CommandReference

  1. OnModeChange()爲F12鍵,切換從ViewModelMode和提高性能改變的通知。

  2. ContentControl上的數據觸發器在Window上。在數據觸發器中檢查Mode如果它是「配置」,並將ContentTemplate更改爲「配置」數據模板,則更改爲「執行」數據模板。

    <ContentControl Content="{Binding}"> 
        <ContentControl.Style> 
         <Style TargetType="{x:Type ContentControl}"> 
          <Style.Triggers> 
           <DataTrigger Binding="{Binding Mode}" Value="Config"> 
            <Setter Property="ContentTemplate" 
              Value="{StaticResource ConfigView}"/> 
           </DataTrigger> 
           <DataTrigger Binding="{Binding Mode}" Value="Execution"> 
            <Setter Property="ContentTemplate" 
              Value="{StaticResource ExecutionView}"/> 
           </DataTrigger> 
          </Style.Triggers> 
         </Style> 
        </ContentControl.Style> 
        </ContentControl> 
    

我希望我的方法可以幫助你在某種意義。

+0

謝謝,我會盡力而爲和回到你...... :) – Ankesh

+0

當然。不要忘記將'ViewModel'的實例設置爲'DataContext'到'Window'。 :-) –

+0

我試過你的方式,但兩個視圖'MenuBar'是不同的?如何處理....每個視圖的不同MenuBar或......還有其他一些方法 – Ankesh