简体   繁体   English

使用Xamarin形式的数据绑定在类和页面之间传递值

[英]Passing values between classes and pages using data binding in xamarin forms

I want to pass the values from ViewModel class to model and then i want to bind the values of model class with page which is detail page of Master detail page.. I want to show User name and Email after login success.Every thing work fine but User name and email does not display in Home Page . 我想将ViewModel类的值传递给模型,然后将模型类的值与主详细信息页的详细信息页绑定。.我想在登录成功后显示用户名和电子邮件。一切正常但是用户名和电子邮件不会显示在“主页”中。 This is my model class 这是我的模特班

 public class User
    {
    private string name;
    private string email;
    public  string Email

            {
                get { return email; }
                set { email = value; }
            }
    public  string UserName

            {
                get { return name; }
                set { name = value; }
            }
    }

here is my viewmodel 这是我的视图模型

      public class LoginVM
        {
User user;
         public async void Login()
                {
//the _user store Email,UserName and Password after calling GetEmployeePassword()
                     var _user = await azureServices.GetEmployeePassword(Employees.Email,Employees. Password);
                    if (_user != null)
                    {
                        if (_user.Email == Employees.Email && _user.Password == Employees.Password)
                        {
                             await App.Current.MainPage.DisplayAlert("success", "Login success", "OK");

        //passing Email and User Name to User Model
    //Here is my Problem
                            user = new User()
                            {
                                Email = _user.Email,
                            UserName=_user.UserName
                            };


                            var masterDetailPage = new MasterDetailPage1();

                            masterDetailPage.Master = new MasterDetailPage1Master();
                             masterDetailPage.Detail = new NavigationPage(new Page1());
                            Application.Current.MainPage = masterDetailPage;
                        }
                        else
                            await App.Current.MainPage.DisplayAlert("Error", "Login fail", "OK");
                    }

        }

this is page1(Home Page) Xml 这是page1(首页)Xml

<?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="MyProject.Views.Profile.Page1"
 Title="Home" >

                <StackLayout Margin="5,30,5,5">
        <Entry x:Name="UsernameEntery" Text="{Binding Username, Mode=TwoWay}" Placeholder="UserName"/>
        <Entry x:Name="EmailEntry" Text="{Binding Email, Mode=TwoWay}" Placeholder="Email"/>
    </StackLayout>

</ContentPage>

Page1 CS 第1页CS

public partial class Page1:ContentPage
    {
        User user;
        public Page1 ()
        {
            InitializeComponent ();
            BindingContext = user= new User();
        }
    }

Please help me if anybody understand what i want.i will give more info if anybody need 如果有人了解我想要什么,请帮助我。如果有人需要,我会提供更多信息

first, you need to pass in the User object when you create your page 首先,您需要在创建页面时传递User对象

masterDetailPage.Detail = new NavigationPage(new Page1(user));

then in your page, set the BindingContext 然后在您的页面中,设置BindingContext

public partial class Page1:ContentPage
{
    User _user;
    public Page1 (User user)
    {
        InitializeComponent ();
        BindingContext = _user = user;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM