如何从 Xamarin PCL 项目中的视图调用模型的方法



>我想调用一个位于class LoginViewModel中但无法使用的方法。如何从 Login.xaml.cs 代码隐藏中使其可访问,以便我可以调用 Connexion() 方法?

登录.xaml

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

登录.xaml.cs

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

登录视图模型.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();
                    }
                }
            }

我认为您应该将"绑定"与命令一起使用,而不是单击">

类似的东西

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

在你的视图模型中,类似的东西

    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(选择的设计模式;但是,您可以使用下面的代码来实现您的要求。

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
这个按钮会很大,所以你需要适当地调整它。

<?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>

最新更新