0

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

Aren Hovsepyan
  • 1,947
  • 2
  • 17
  • 45

2 Answers2

1

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

TheInnerLight
  • 12,034
  • 1
  • 29
  • 52
  • Hi;thanks for answer;so in MVVM I need not use xaml.c files;i just need use ViewModels – Aren Hovsepyan Nov 07 '15 at 23:59
  • Generally you don't need to edit the .xaml.cs file in MVVM. You should aim to design the layout and presentation in the xaml file while the interaction between your view and your "business logic" is controlled in the ViewModel. – TheInnerLight Nov 08 '15 at 00:04
  • Also ViewModel (best in separate assembly/project) should not reference any Type from UI (WPF, Presentation.dll, System.Windows.Form.* etc). With separate assembly you'll notice if you break MVVM when you get a "Presentation.dll reference missing" or similar message – Tseng Nov 09 '15 at 11:16
0

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;
    }
Aren Hovsepyan
  • 1,947
  • 2
  • 17
  • 45
  • While this might work, it isn't an MVVM solution. MVVM has significant advantages in terms of testing because the interaction logic between the view and the business layer can be tested independently of the UI as well as several other benefits. See http://stackoverflow.com/questions/1644453/why-mvvm-and-what-are-its-core-benefits – TheInnerLight Nov 09 '15 at 11:14