2013-05-28 35 views
1

對WPF和MVVM的新增功能。 我試圖創建使用MVVM的登錄窗口,我成功創建。
這裏是Login.xmal代碼。在WPF中使用MVVM進行視圖導航

<Button x:Name="btnLogin" Content="Login" HorizontalAlignment="Left" Margin="51,0,0,10" 
      VerticalAlignment="Bottom" Width="124" Height="57" Grid.Column="1" 
      CommandParameter="{Binding ElementName=txtPassword}" 
      Command="{Binding LoginCommand}" 
      >   
    </Button> 

    <Button x:Name="btnClose" Content="Close" HorizontalAlignment="Left" Margin="180,0,0,10" 
     VerticalAlignment="Bottom" Width="124" Height="57" Grid.Column="1" Command="{Binding ExitCommand}"> 

    </Button> 

    <Label Content="User Name" Margin="10,74,0,0" VerticalAlignment="Top" Height="49" 
      VerticalContentAlignment="Center" Grid.Column="1" HorizontalAlignment="Left" Width="130"/> 

    <TextBox x:Name="txtUserName" HorizontalAlignment="Right" Height="49" Margin="0,74,10,0" 

      TextWrapping="Wrap" VerticalAlignment="Top" Width="185" 
      VerticalContentAlignment="Center" Grid.Column="1" FontSize="18"> 
     <TextBox.Text> 
      <Binding Path="Username" Mode="OneWayToSource"> 
       <Binding.ValidationRules> 
        <ExceptionValidationRule></ExceptionValidationRule> 
       </Binding.ValidationRules> 
      </Binding> 
     </TextBox.Text> 
    </TextBox> 

    <Label Content="Password" Margin="10,128,0,0" VerticalAlignment="Top" Height="49" 
      VerticalContentAlignment="Center" Grid.Column="1" HorizontalAlignment="Left" Width="130"/> 
    <PasswordBox x:Name="txtPassword" HorizontalAlignment="Right" 
      Height="49" Margin="0,128,10,0" 
      VerticalAlignment="Top" Width="185" 
     VerticalContentAlignment="Center" Grid.Column="1" FontSize="18"> 

    </PasswordBox> 

在這之後我已經創建了我實現INotifyPropertyChanged的的viewModeBase.cs類,這包括在LoginViewModel.cs ... 這裏是LoginViewModel.cs代碼

public class LoginViewModel : ViewModelBase 
{ 
    private string m_username; 
    public string Username 
    { 
     get { return m_username; } 
     set 
     { 
      m_username = value; 
      OnPropertyChanged("Username"); 

     } 
    } 

    private string m_password; 
    public string Password 
    { 
     get { return m_password; } 
     set 
     { 
      m_password = value; 
      OnPropertyChanged("Password"); 
     } 
    } 
    private DelegateCommand exitCommand; 

    public ICommand ExitCommand 
    { 
     get 
     { 
      if (exitCommand == null) 
      { 
       exitCommand =new DelegateCommand(Exit); 
      } 
      return exitCommand; 
     } 
    } 

    private void Exit() 
    { 
     Application.Current.Shutdown(); 
    } 

    public LoginViewModel() 
    { 

    } 

    private DelegateCommand<object> loginCommand; 
    public ICommand LoginCommand 
    { 
     get 
     { 
      if (loginCommand == null) 
      { 
       loginCommand = new DelegateCommand<object>(Login); 
      } 
      return loginCommand; 
     } 
    } 



    public void Login(object pPasswordBox) 
    { 
     try 
     { 
      if (string.IsNullOrEmpty(Username)) 
      { 
       MessageBox.Show("Username cannot be blank.");      
       return; 
      } 

      if (string.IsNullOrEmpty(((PasswordBox)pPasswordBox).Password)) 
      { 
       MessageBox.Show("Password cannot be blank.");     
       return; 
      } 

      dlUsers odlUsers = new dlUsers(); 
      bool lResult = odlUsers.UserAuthentication(clsGymManagment.ConnectionString, Username, 
       ((((PasswordBox)pPasswordBox).Password))); 
      if (lResult) 
      { 
       ///TODO: Need code to Hide Login Window and Open New XAML..... 
      } 
      else 
      { 
       MessageBox.Show("Username/Password is wrong.");     
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    }   
} 

因爲我想隱藏LOGIN.XAML文件並打開UI.XAML文件..(UI.XAML您可以考慮使用任何XAML窗口。)... 如果您可以幫助我導航UserControl之間的用戶界面UI.XAML

回答

2

您需要通過單獨的代碼塊控制登錄窗口,例如App.xaml.cs.設置app.xaml來調用代碼而不是顯示一個窗口。

讓App_Startup創建LoginViewModel,新建一個表單,將表單的數據上下文設置到您的ViewModel並顯示它。

窗體的更新將更新ViewModel,當它關閉時會將控制權返回給您的調用代碼。

Login.xaml.cs

private void btnOk_Click(object sender, RoutedEventArgs e) 
    { 
     if (anything incorrect) 
     { 
      MessageBox.Show("Enter a username and password"); 
     } 
     else 
      DialogResult = true; 
    } 

App.xaml.cs

Login.DataContext = LoginViewModel; 

if (Login.ShowDialog() ?? false) 
{ 
    //Check the LoginViewModel for a correct password. 
} 
+0

我喜歡你只是解釋它的想法,它可以幫助我編寫代碼....但需要確認,這是處理MVVM模式的好方法嗎? –

+0

是的,你看到的唯一一個代碼就是從對話框中返回正確的值。我不知道另一種設置返回值的方法。 – strattonn