1
我已經使用MVVM創建了登錄身份驗證,並且它工作正常。這是我做了什麼:WPF中模型視圖和視圖模型之間的關係
MainWinodw.xaml
<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
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
}
這是工作的罰款。但正如我所說我是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爲空。那麼任何人都可以幫助我如何在視圖模型中獲取模型屬性,以及如何更新模型屬性。請幫忙。 謝謝
我不認爲thatr你不能讓你的邏輯都在一起。在ViewModel中使用方法不成問題。您可以繼續使用您的方法 – Apoorv
,但我在許多網站上閱讀,模型和視圖模型必須是分開的。 –
確定做一件事..在您的UserViewModel.cs中替換UserModel obj = new UserModel()與ObservableCollection coll = new ObservableCollection ();然後嘗試訪問這些值並查看您是否仍然收到空值? –
Apoorv