Reputation: 2047
I am creating login functionality and I need to send my information of user from my xaml.cs to my ViewModels SO when I am clicking on the button I to need show other window with my data
How can I do that
My Click function from LoginPage.xaml.cs
private void LoginButton_OnClick(object sender, RoutedEventArgs e)
{
LoginPageViewModels ViewModel = new LoginPageViewModels();
SqlHelper Sql = new SqlHelper();
string ConnectionState = Sql.SqlConnectionState();
// Check connection;
if (ConnectionState == "Connected")
{
List<User> User = new List<User>();
string username = UsernameInput.Text;
string password = PasswordInput.Password;
User = ViewModel.Auth(username, password);
if (User.Count >= 1)
{
MainPage mainPage = new MainPage();
mainPage.Show();
this.Close();
}
else
{
AuthResultMessage.Text = "User not found";
}
}
else
{
AuthResultMessage.Text = ConnectionState;
}
}
I need to send List<User> User
to my new MainPage mainPage
and then I need bind User to mainPage
Upvotes: 0
Views: 1680
Reputation: 2047
I found solution
I can add User UserData {get;set;}
property in MainPageViewModels.cs
and in xaml.cs
private void LoginButton_OnClick(object sender, RoutedEventArgs e)
{
LoginPageViewModels ViewModel = new LoginPageViewModels();
SqlHelper Sql = new SqlHelper();
string ConnectionState = Sql.SqlConnectionState();
// Check connection;
if (ConnectionState == "Connected")
{
User userData= new User;
string username = UsernameInput.Text;
string password = PasswordInput.Password;
userData= ViewModel.Auth(username, password);
if (userData.Username != null)
{
MainPage mainPage = new MainPage();
mainPage.UserData=userData;
mainPage.Show();
this.Close();
}
else
{
AuthResultMessage.Text = "User not found";
}
}
else
{
AuthResultMessage.Text = ConnectionState;
}
Upvotes: 0
Reputation: 12184
If you're doing MVVM, you shouldn't generally be writing button click events. You should use data binding to bind a UI element to a property in your view model.
<TextBlock Text="{Binding Path=ViewModelTextProperty}">
where ViewModelTextProperty
is the property in your view model you wish to bind to.
Buttons should be bound commands, properties which have a return type that implements the ICommand
interface.
<Button Command="{Binding Path=ViewModelButtonCommand}">
where ViewModelButtonCommand
is a property in your view model of type ICommand.
Your view model then has ownership of the strings and can perform the login logic using them once your the login button is clicked.
Your view model class will need to trigger PropertyChanged events when each of the bound properties are updated in order to trigger updates of the view. See this MSDN article on implementing the MVVM pattern: https://msdn.microsoft.com/en-us/library/gg405484%28v=PandP.40%29.aspx
Upvotes: 1