2

I'd like to call a method that is located in a class LoginViewModel but it is inacessible. How can I render it accessible from the Login.xaml.cs code behind so that i can call the Connexion() method ?

Login.xaml

<Button 
            StyleId="btn_connexion"
            Text="Connexion"
                Clicked="Connexion_click" />

Login.xaml.cs

private void Connexion_click(object sender, EventArgs e)
        {
           //here is where i'd like to call the connexion method 
        }

LoginViewModel.cs

public async Task Connexion()
        {

            List<Visiteur> listeTest = CreerListeVisiteurDur();
            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                foreach (Visiteur unVisiteur in listeTest)
                {
                    string login = unVisiteur.login;
                    string pass = unVisiteur.mdp;

                    if (login == username && pass == password)
                    {
                        App.Current.MainPage = new CreerVisite();
                    }

                }
            }
steveax
  • 17,527
  • 6
  • 44
  • 59
En_Tech_Siast
  • 83
  • 1
  • 12
  • [Access ViewModel from Code Behind](https://stackoverflow.com/questions/44996349/prism-vm-binding-with-view-within-page-code-behind/46451346#46451346) Refer this answer – K K Oct 26 '17 at 11:28

2 Answers2

0

I think you should use "Binding" with Command, not Clicked

something like

<Button 
            StyleId="btn_connexion"
            Text="Connexion"
                Command="{Binding OpenPageCommand}" />

and in your ViewModel something like

    this.OpenPageCommand = new Command(async () => {
        try { 
                        List<Visiteur> listeTest = CreerListeVisiteurDur();
    if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
    {
        foreach (Visiteur unVisiteur in listeTest)
        {
            string login = unVisiteur.login;
            string pass = unVisiteur.mdp;

            if (login == username && pass == password)
            {
                App.Current.MainPage = new CreerVisite();
            }

        }
    }

        }
        catch (Exception ex) {
            await Application.Current.MainPage.DisplayAlert("Attention", ex.Message, "Ok");
        }
    });
Alessandro Caliaro
  • 5,623
  • 7
  • 27
  • 52
0

I agree with Alessandro Caliaro on your chosen design pattern; however, you can use the code below to achieve what you're asking for.

BtnClicker.xaml.cs

 namespace BountyApp.Pages
{

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class BtnClicker : ContentPage
    {
        BtnClickerViewModel model = new BtnClickerViewModel();
        public BtnClicker()
        {
            InitializeComponent();
            BindingContext = model;

        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            Device.BeginInvokeOnMainThread(async () =>
            {
                await model.Connexion();
            });

        }
    }

    class BtnClickerViewModel : INotifyPropertyChanged
    {
        public async Task Connexion()
        {
            List<Visiteur> listeTest = CreerListeVisiteurDur();
            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                foreach (Visiteur unVisiteur in listeTest)
                {
                    string login = unVisiteur.login;
                    string pass = unVisiteur.mdp;

                    if (login == username && pass == password)
                    {
                        App.Current.MainPage = new CreerVisite();
                    }

                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        void OnPropertyChanged([CallerMemberName]string propertyName = "") =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    }
}

BtnClicker.xaml
This button will be huge, so you'll want to adjust it appropriately.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="BountyApp.Pages.BtnClicker"
             Title="BtnClicker">
    <Button StyleId="btn_connexion" Text="Connexion" Clicked="Button_Clicked"></Button>
</ContentPage>
Community
  • 1
  • 1
Joshua Poling
  • 561
  • 8
  • 29