使用数据类和页面xamarin形式结合之间传递值

问题描述 投票:0回答:1

我想通过从ViewModel类的值来建模,然后我想结合这是主详细页面的详细信息页面。我想展示后登录success.Every事情做工精细的用户名和电子邮件与网页模型类的值但用户名和电子邮件地址没有在主页显示。这是我的模型类

 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; }
            }
    }

这里是我的视图模型

      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");
                    }

        }

这是第1页(首页)的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>

第1页CS

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

请帮我,如果有人明白我want.i将给予更多的信息,如果有谁需要

c# mvvm data-binding xamarin.forms parameter-passing
1个回答
0
投票

首先,你需要在User对象传递,当你创建你的页面

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

然后在你的页面上,设置BindingContext

public partial class Page1:ContentPage
{
    User _user;
    public Page1 (User user)
    {
        InitializeComponent ();
        BindingContext = _user = user;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.