How to pass the registered data from Signup Page to Setting Page, .NET MAUI

34 Views Asked by At

As I'm very new for using .NET MAUI, so I'm a bit confused about passing data from a page to another page.I'm trying to pass the data from the Signup Page to the Setting Page but I can't.

This is the code for Signup Page:

<Entry x:Name="fullNameEntry" 
                       Text="{Binding FullName}"
                       Placeholder="Alice Wong Li Li"
                       PlaceholderColor="{StaticResource BlurTextColor}"
                       TextColor="{StaticResource NormalTextColor}"
                       FontFamily="WorkSansRegular"
                       CharacterSpacing="-0.5"
                       FontSize="16"/>
                <Entry x:Name="emailEntry" 
                       Placeholder="[email protected]"
                       Text="{Binding Email}"
                       PlaceholderColor="{StaticResource BlurTextColor}"
                       TextColor="{StaticResource NormalTextColor}"
                       FontFamily="WorkSansRegular"
                       CharacterSpacing="-0.5"
                       FontSize="16"/>
                <Entry x:Name="PasswordEntry" 
                       Text="{Binding Password}"
                       TextColor="{StaticResource NormalTextColor}"
                       FontFamily="WorkSansRegular"
                       CharacterSpacing="-0.5"
                       FontSize="16"/>
                <Entry x:Name="ConfirmPasswordEntry"
                       Text="{Binding ConfirmPassword, Mode=TwoWay}"
                       TextColor="{StaticResource NormalTextColor}"
                       FontFamily="WorkSansRegular"
                       CharacterSpacing="-0.5"
                       FontSize="16">
                </Entry>
                <editors:SfMaskedEntry x:Name="phoneNumberEntry" 
                                       Placeholder="0123456789"
                                       MaskType="RegEx"
                                       Mask=""
                                       PlaceholderColor="{StaticResource BlurTextColor}"
                                       TextColor="{StaticResource NormalTextColor}"
                                       FontFamily="WorkSansRegular"
                                       FontSize="16"/>
                <Entry x:Name="homeAddressEntry"
                       Text="{Binding HomeAddress}"
                       Placeholder=""
                       PlaceholderColor="{StaticResource BlurTextColor}"
                       TextColor="{StaticResource NormalTextColor}"
                       FontFamily="WorkSansRegular"
                       CharacterSpacing="-0.5"
                       FontSize="16"/>

And this is the code for SettingPage:

<listView:SfListView x:Name="PersonalInformationListView"
                     Grid.Row="1"
                     Orientation="Vertical"
                     VerticalOptions="Start"
                     ScrollBarVisibility="Always"
                     BackgroundColor="Azure"
                     ItemSize="{OnPlatform 350}"
                     ItemSpacing="{OnPlatform '0,0,0,0'}"
                     ItemsSource="{Binding PersonalInfo}">
    <listView:SfListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="{OnPlatform Auto}"/>
                    <RowDefinition Height="{OnPlatform 1}"/>
                    <RowDefinition Height="{OnPlatform Auto}"/>
                    <RowDefinition Height="{OnPlatform 1}"/>
                    <RowDefinition Height="{OnPlatform Auto}"/>
                    <RowDefinition Height="{OnPlatform 1}"/>
                    <RowDefinition Height="{OnPlatform Auto}"/>
                    <RowDefinition Height="{OnPlatform 1}"/>
                </Grid.RowDefinitions>
                <Frame Grid.Row="0" Padding="{OnPlatform 0}" CornerRadius="0" BorderColor="Transparent" BackgroundColor="{StaticResource FrameBackgroundColor}" HeightRequest="{OnPlatform 80}">
                    <Label Text="{Binding FullName}" Grid.Row="1" TextColor="{StaticResource ActiveButtonTextColor}" VerticalOptions="Center" LineBreakMode="WordWrap" FontFamily="WorkSansMedium" FontSize="{OnPlatform 18}" Padding="{OnPlatform 15}"/>
                </Frame>
                <BoxView Grid.Row="1" Color="{StaticResource Gray400}" HeightRequest="{OnPlatform 1}"/>
                <Frame Grid.Row="2" Padding="{OnPlatform 0}" CornerRadius="0" BorderColor="Transparent" BackgroundColor="{StaticResource FrameBackgroundColor}" HeightRequest="{OnPlatform 80}">
                    <Label Text="{Binding Email}" Grid.Row="1" TextColor="{StaticResource ActiveButtonTextColor}" VerticalOptions="Center" LineBreakMode="WordWrap" FontFamily="WorkSansMedium" FontSize="{OnPlatform 18}" Padding="{OnPlatform 15}"/>
                </Frame>
                <BoxView Grid.Row="3" Color="{StaticResource Gray400}" HeightRequest="{OnPlatform 1}"/>
                <Frame Grid.Row="4" Padding="{OnPlatform 0}" CornerRadius="0" BorderColor="Transparent" BackgroundColor="{StaticResource FrameBackgroundColor}" HeightRequest="{OnPlatform 80}">
                    <Label Text="{Binding PhoneNumber}" Grid.Row="1" TextColor="{StaticResource ActiveButtonTextColor}" VerticalOptions="Center" LineBreakMode="WordWrap" FontFamily="WorkSansMedium" FontSize="{OnPlatform 18}" Padding="{OnPlatform 15}"/>
                </Frame>
                <BoxView Grid.Row="5" Color="{StaticResource Gray400}" HeightRequest="{OnPlatform 1}"/>
                <Frame Grid.Row="6" Padding="{OnPlatform 0}" CornerRadius="0" BorderColor="Transparent" BackgroundColor="{StaticResource FrameBackgroundColor}" HeightRequest="{OnPlatform 80}">
                    <Label Text="{Binding HomeAddress}" Grid.Row="1" TextColor="{StaticResource ActiveButtonTextColor}" VerticalOptions="Center" LineBreakMode="WordWrap" FontFamily="WorkSansMedium" FontSize="{OnPlatform 18}" Padding="{OnPlatform 15}"/>
                </Frame>
                <BoxView Grid.Row="7" Color="{StaticResource Gray400}" HeightRequest="{OnPlatform 1}"/>
            </Grid>
        </DataTemplate>
    </listView:SfListView.ItemTemplate>
</listView:SfListView>

This is the code for SignupPageViewModel:

namespace fitnessStudioMobileApp.ViewModels
{
    public partial class SignupPageViewModel : ObservableObject, INotifyPropertyChanged
    {
        [ObservableProperty]
        private string fullName;

        [ObservableProperty]
        private string homeAddress;

        [ObservableProperty]
        private string phoneNumber;

        private string userName;

        private string userPassword;

        private string email;

        private string password;

        public string UserName
        {
            get => userName;
            set
            {
                userName = value;
                RaisePropertyChanged("UserName");
            }
        }
        public string UserPassword
        {
            get => userPassword;
            set
            {
                userPassword = value;
                RaisePropertyChanged("UserPassword");
            }
        }

        public string Email
        {
            get => email;
            set
            {
                email = value;
                RaisePropertyChanged("Email");
            }
        }

        public string Password
        {
            get => password;
            set
            {
                password = value;
                RaisePropertyChanged("Password");
            }
        }

        private PersonalInfo _personalInfo;
        public PersonalInfo PersonalInfo
        {
            get => _personalInfo;
            set => SetProperty(ref _personalInfo, value);
        }

        private void RaisePropertyChanged(string v)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v));
        }

        public SignupPageViewModel()
        {
            LoadUserProfile();
        }

        public void LoadUserProfile()
        {
            var personalInfoJson = Preferences.Get("PersonalInfo", string.Empty);
            if (!string.IsNullOrEmpty(personalInfoJson))
            {
                Debug.WriteLine("Loading user profile...");

                PersonalInfo = JsonConvert.DeserializeObject<PersonalInfo>(personalInfoJson);
                // Update ViewModel properties with loaded data
                FullName = PersonalInfo.FullName;
                Email = PersonalInfo.Email;
                PhoneNumber = PersonalInfo.PhoneNumber;
                HomeAddress = PersonalInfo.HomeAddress;
            }
        }

        private async void RegisterUserCommandTappedAsync()
        {
            try
            {
                var authProvider = new FirebaseAuthProvider(new FirebaseConfig(webApiKey));
                var auth = await authProvider.CreateUserWithEmailAndPasswordAsync(Email, Password);
                string token = auth.FirebaseToken;
                var personalInfo = new PersonalInfo
                {
                    FullName = this.FullName,
                    Email = this.Email,
                    PhoneNumber = this.PhoneNumber,
                    HomeAddress = this.HomeAddress
                };
                if (token != null)
                {
                    var personalInfoJson = JsonConvert.SerializeObject(personalInfo);
                    Preferences.Set("PersonalInfo", personalInfoJson);
                    await App.Current.MainPage.DisplayAlert("Congratulation!", "User Registered successfully", "OK");
                    await Shell.Current.GoToAsync("LoginPage");

                }

            }
            catch (Exception ex)
            {
                await App.Current.MainPage.DisplayAlert("Alert", ex.Message, "OK");
            }
        }
    }
}

And this is the code for PersonalInfo.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using System.ComponentModel;

namespace fitnessStudioMobileApp.Model
{
    public class PersonalInfo : INotifyPropertyChanged
    {
        #region Fields

        private string? fullName;
        private string? email;
        private string? phoneNumber;
        private string? homeAddress;

        #endregion

        #region Constructor

        public PersonalInfo()
        {

        }

        #endregion

        #region Properties

        public string? FullName
        {
            get { return fullName; }
            set
            {
                fullName = value;
                OnPropertyChanged("FullName");
            }
        }

        public string? Email
        {
            get { return email; }
            set
            {
                email = value;
                OnPropertyChanged("Email");
            }
        }

        public string? PhoneNumber
        {
            get { return phoneNumber; }
            set
            {
                phoneNumber = value;
                OnPropertyChanged("PhoneNumber");
            }
        }

        public string? HomeAddress
        {
            get { return homeAddress; }
            set
            {
                homeAddress = value;
                OnPropertyChanged("HomeAddress");
            }
        }

        #endregion

        #region Interface Member

        public event PropertyChangedEventHandler? PropertyChanged;

        public void OnPropertyChanged(string name)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        #endregion
    }
}

I'm trying to pass the data (what the user write their information in SignupPage) to the Setting Page.

0

There are 0 best solutions below