如何在Xamarin Forms的文本字段中显示数据库中的注册数据进行更新

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

我想在我的Xamarin Forms应用程序中Update/Editt玩家详细信息。一旦玩家登录到应用程序,点击个人资料图片时应导航到玩家详细信息(Register.xaml)屏幕,其中player details是从数据库填充的。如何在文本字段中显示数据?

// Register.xaml:

<ContentPage.Content>
        <StackLayout Spacing="20" Padding="20">           
            <Label Text="Player Details" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" FontSize="25"></Label>
            <Entry x:Name="fullNameEntry" Placeholder="Full Name" Text="{Binding FullName}"></Entry>
            <Entry x:Name="mobileEntry" Placeholder="Mobile" Text="{Binding Mobile}"></Entry>
            <Entry x:Name="soccerpostionEntry" Placeholder="Soccer Position" Text="{Binding SoccerPosition}"></Entry>            
            <Button Text="Register"  Clicked="RegisterSave_OnClicked" TextColor="White" BackgroundColor="ForestGreen"></Button>
        </StackLayout>
    </ContentPage.Content>

下面OnProfilePicClicked将从数据库中获取登录用户

private async void OnProfilePicClicked(object sender, EventArgs e)
        {
            //Navigate to Register screen with player data loaded:
            var emailText = emailEntry.Text;            
            await Navigation.PushAsync(new Register(){});
            List<PlayerDetails> details = (from x in conn.Table<PlayerDetails>() where x.Email == emailText select x).ToList();
            if (details!= null)
            {
                // found the record
                PlayerDetails playerDetails = new PlayerDetails();
                playerDetails.FullName = details[0].FullName;
                playerDetails.Mobile = details[0].Mobile;
                playerDetails.SoccerPosition = details[0].SoccerPosition;
            }

        }

PlayerDetails模型类:

string fullname;
        string mobile;
        string soccerposition;

        public PlayerDetails()
        {

        }

        public string FullName
        {
            set
            {
                if (fullname != value)
                {
                    fullname = value;

                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("FullName"));
                    }
                }
            }
            get
            {
                return fullname;
            }
        }

        public string Mobile
        {
            set
            {
                if (mobile != value)
                {
                    mobile = value;

                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Mobile"));
                    }
                }
            }
            get
            {
                return mobile;
            }

        }

        public string SoccerPosition
        {
            set
            {
                if (soccerposition != value)
                {
                    soccerposition = value;

                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("SoccerPosition"));
                    }
                }
            }
            get
            {
                return soccerposition;
            }
        }
xamarin xamarin.forms
1个回答
1
投票

解:

当你推到注册页面时,你应该pass the model

在Register页面中,在构造函数中添加一个PlayerDetails参数,并将BindingContext设置为模型:

public partial class Register : ContentPage
{
    PlayerDetails myDetails;

    public Register(PlayerDetails playD)
    {
        InitializeComponent ();

        myDetails = playD;
        BindingContext = myDetails;
    }
}

当你推,传递模型:

private async void OnProfilePicClicked(object sender, EventArgs e)
{
    //Navigate to Register screen with player data loaded:
    var emailText = emailEntry.Text;
    List<PlayerDetails> details = (from x in conn.Table<PlayerDetails>() where x.Email == emailText select x).ToList();
    if (details != null)
    {
        // found the record
        PlayerDetails playerDetails = new PlayerDetails();
        playerDetails.FullName = details[0].FullName;
        playerDetails.Mobile = details[0].Mobile;
        playerDetails.SoccerPosition = details[0].SoccerPosition;


        await Navigation.PushAsync(new Register(playerDetails) { });
    }
    else {

        Console.WriteLine("Can't find the playerDetails");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.