2016-07-22 10 views
1

MVVMを使用してログイン認証を作成しても問題ありません。これは私がやっていることです:WPFのモデルビューとビューモデルの関係

MainWinodw.xamlこれが正常に動作している

public class LoginViewModel : INotifyPropertyChanged 
{ 
    private bool _isAuthenticated; 
    public bool isAuthenticated 
    { 
     get { return _isAuthenticated; } 
     set 
     { 
      if (value != _isAuthenticated) 
      { 
       _isAuthenticated = value; 
       OnPropertyChanged("isAuthenticated"); 
      } 
     } 
    } 

    private bool _loginFail; 
    public bool LoginFail 
    { 
     get { return _loginFail; } 
     set 
     { 
      if (value != _loginFail) 
      { 
       _loginFail = value; 
       OnPropertyChanged("LoginFail"); 
      } 
     } 
    } 

    private string _username; 
    public string UserName 
    { 
     get { return _username; } 
     set 
     { 
      _username = value; 
      OnPropertyChanged("UserName"); 
     } 
    } 

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

    public ICommand LoginCommand 
    { 
     get { return new RelayCommand(param => this.Login()); } 
    } 

    public void Login() 
    { 
     //TODO check username and password vs database here. 
     //If using membershipprovider then just call Membership.ValidateUser(UserName, Password) 
     //if (!String.IsNullOrEmpty(UserName) && !String.IsNullOrEmpty(Password)) 
     // isAuthenticated = true; 
     isAuthenticated = LoginDataLayer.AuthenticateUser(UserName, Password); 
     if(isAuthenticated == true) 
     { 
      LoginFail = false; 
     } 
     else 
     { 
      LoginFail = true; 
     } 

    } 

    #region INotifyPropertyChanged Methods 

    public void OnPropertyChanged(string propertyName) 
    { 
     this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); 
    } 

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, args); 
     } 
    } 


    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 
} 

<Window.Resources> 
    <BooleanToVisibilityConverter x:Key="BoolToVis" /> 
</Window.Resources> 
<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <TextBlock Grid.Row="0" Grid.Column="0">UserName:</TextBlock> 
    <TextBlock Grid.Row="1" Grid.Column="0">Password:</TextBlock> 
    <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
    <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
    <Label Grid.Row="3" Grid.ColumnSpan="2" Visibility="{Binding isAuthenticated, Converter={StaticResource BoolToVis}}"> 
     User has been authenticated 
    </Label> 
    <Label Grid.Row="3" Grid.ColumnSpan="2" Visibility="{Binding LoginFail, Converter={StaticResource BoolToVis}}"> 
     Please enter valid UserName and Password 
    </Label> 

    <Button Grid.Row="2" Grid.Column="1" Content="Authenticate" Command="{Binding LoginCommand}" Margin="3" Width="100" HorizontalAlignment="Right" /> 
</Grid> 

UserViewModel.cs。しかし、私が言ったように私はMVVMには新しいです。私はモデルモデルをviewmodelにも書きます。私のミスを実現した後、私はこのような独立したモデルとビューモデルコードを試してみました:

UserModel.cs

public class UserModel : INotifyPropertyChanged 
{ 
    private bool _isAuthenticated; 
    public bool isAuthenticated 
    { 
     get { return _isAuthenticated; } 
     set 
     { 
      if (value != _isAuthenticated) 
      { 
       _isAuthenticated = value; 
       OnPropertyChanged("isAuthenticated"); 
      } 
     } 
    } 

    private bool _loginFail; 
    public bool LoginFail 
    { 
     get { return _loginFail; } 
     set 
     { 
      if (value != _loginFail) 
      { 
       _loginFail = value; 
       OnPropertyChanged("LoginFail"); 
      } 
     } 
    } 

    private string _username; 
    public string UserName 
    { 
     get { return _username; } 
     set 
     { 
      _username = value; 
      OnPropertyChanged("UserName"); 
     } 
    } 

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

    #region INotifyPropertyChanged Methods 

    public void OnPropertyChanged(string propertyName) 
    { 
     this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); 
    } 

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, args); 
     } 
    } 


    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 
} 

UserViewModel.cs

public void Login() 
    { 
     UserModel obj = new UserModel(); 


     obj.isAuthenticated = LoginDataLayer.AuthenticateUser(obj.UserName,obj. Password); 
     if(obj.isAuthenticated == true) 
     { 
      obj.LoginFail = false; 
     } 
     else 
     { 
      obj.LoginFail = true; 
     } 

    } 

しかし、私はobj.usernameがnullで取得しています。どのように私はビューモデルでモデルプロパティを取得し、どのように更新することができます誰も助けてください。助けてください。 ありがとう

+0

私はあなたが一緒にあなたの両方のロジックを保つカントthatrと思ういけません。 ViewModelのメソッドを持つことは問題ではありません。あなたはあなたのアプローチを先に進めることができます – Apoorv

+0

しかし、私はモデルとビューのモデルが別々でなければならない多くのサイトで読む。 –

+0

OK何かをUserViewModel.csに置き換えます。UserModel obj = ObservableCollectionを使った新しいUserModel()を置き換えます。 coll = new ObservableCollection ();値にアクセスして、まだヌルになっているかどうかを確認してください。 – Apoorv

答えて

1

私が見るとおり、あなたのビューdatacontextはviewModelですが、プロパティはモデル上にあります。このように、ビューモデルからあなたUserModelがづけしてください:

のViewModel:

private UserModel _userModel; 
public UserModel userModel 
{ 
    get { return _userModel; } 
    set 
    { 
     _userModel = value; 
     OnPropertyChanged("userModel"); 
    } 
} 

ビュー:

<TextBox Text="{Binding userModel.UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
関連する問題