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(); } } } 
1

2 Answers 2

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"); } }); 
Sign up to request clarification or add additional context in comments.

Comments

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> 

1 Comment

Thank you for you response, but the binding context makes the application shutdown itself and I dont understand why

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.