Search code examples
c#xamlxamarinxamarin.formsxamarin.android

problem transferring the object to another page


I have a simple project. I want to transfer an object from the login page to profile page LOGIN PAGE

 <StackLayout Orientation="Vertical" Padding="30" Spacing="40">
        <BoxView HeightRequest="10"/>
        <Image HorizontalOptions="Center" WidthRequest="300" Source="maco.jpg"/>
        <Frame BackgroundColor="#2D78FD" HasShadow="False">
            <StackLayout Orientation="Vertical" Spacing="10">
                <StackLayout Orientation="Horizontal">
                    <Label Text="Phone:" FontSize="Large"></Label>
                <Entry x:Name="PhoneName" Placeholder="+380**********"   
                       PlaceholderColor="White" HeightRequest="40"   MaxLength="13"
                       Keyboard="Telephone"  
                       TextColor="White"/>
                </StackLayout>
                <StackLayout Orientation="Horizontal">
                    <Label Text="Password:" FontSize="Large"></Label>
                    <Entry x:Name="PasswordName"  Placeholder="************"   
                       PlaceholderColor="White" HeightRequest="40"   MaxLength="10"
                       IsPassword="True"  
                       TextColor="White"/>
                </StackLayout>
            </StackLayout>
        </Frame>
        <Button Clicked="LoginNext" Text="Login" TextColor="White"  
                FontAttributes="Bold" FontSize="Large" HorizontalOptions="FillAndExpand"  
                BackgroundColor="#2D78FD" />
    </StackLayout>

CODE BEHIND

I check that this object does not have problems

 public partial class LoginPage : ContentPage
    {
        public Client MeClient { get; set; }
        public LoginPage()
        {
            Device.SetFlags(new string[] { "AppTheme_Experimental" });
            MeClient = new Client
            {
                Name = "M",
                FirstName = "K",
                Phone = "+380996471253",
                Card = "5375 4141 0000 0000",
                PasswordCard = 1234,
                PasswordApp = 4321,
                Money = 5000,
                State = true
            };

            InitializeComponent();
        }
        private async void LoginNext(object sender, EventArgs e)
        {
         if(PhoneName.Text== MeClient.Phone && PasswordName.Text=="4321")
            {
                  await Navigation.PushModalAsync(new Profile(MeClient));
            }
         else
            {
                _ = DisplayAlert("Помилка", "Не вірний номер або пароль", "ОK");
            }
        }
    }

PROFILE XAML

<ScrollView>
        <StackLayout BackgroundColor="#fbfaff">
            <Image Grid.Row="0" Source="me" VerticalOptions="Start" HeightRequest="300" Aspect="AspectFill" />
            <Label Grid.Row="0" VerticalOptions="End" Padding="20,5,20,0">
                <Label.FormattedText>
                    <FormattedString>
                        <Span x:Name="meow" Text="{Binding MeClient2.Name} " FontSize="25" FontAttributes="Bold" ForegroundColor="Black" />
                        <Span Text="ID 1234567" FontSize="17" ForegroundColor="Black" />
                    </FormattedString>
                </Label.FormattedText>
            </Label>
            <Label Grid.Row="0" VerticalOptions="End" Padding="20,5,20,0">
                <Label.FormattedText>
                    <FormattedString>
                        <Span Text="Баланс на карті &#10;" FontSize="17" FontAttributes="Bold" ForegroundColor="Black" />
                        <Span Text="5000 ₴" FontSize="25" ForegroundColor="Black" />
                    </FormattedString>
                </Label.FormattedText>
            </Label>

            <Image Source="card"  HeightRequest="200" Margin="30,0,30,30"></Image>
            <Label Text="Оберіть Ваш функціонал" FontSize="25" FontAttributes="Bold" Padding="20,5,20,0" TextColor="Black"></Label>
            <StackLayout Orientation="Horizontal"  Margin="30,0,30,30">
                <Label Text="Анулювати картку" HorizontalOptions="Start" VerticalOptions="Center" FontSize="17" TextColor="Black"></Label>
                <Button ImageSource="arrow_right" BackgroundColor="LightBlue" VerticalOptions="Center" HorizontalOptions="EndAndExpand"></Button>
            </StackLayout>
            
        </StackLayout>
        
    </ScrollView>

CODE BEHIND

public partial class Profile : ContentPage
    {
        public Client MeClient2 { get; set; }
        public Profile(Client client)
        {
            Device.SetFlags(new string[] { "AppTheme_Experimental" });
            MeClient2 = client;
            meow.Text = MeClient2.Name;
           
            InitializeComponent();
        }
    }

I have an error when I click on the login button and want to transfer an object

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Please do not close my question as you did. Say me my mistake


Solution

  • you are referencing an element meow that has not been initialized yet. InitializeComponent is responsible for loading and initializing XAML elements. That is why it is the FIRST line of the consructor in the template

    public Profile(Client client)
        {
            Device.SetFlags(new string[] { "AppTheme_Experimental" });
            MeClient2 = client;
            meow.Text = MeClient2.Name;
           
            // this line should be BEFORE referencing a XAML element like "meow"
            InitializeComponent();
        }
    

    ie,

    public Profile(Client client)
        {
            InitializeComponent();
    
            Device.SetFlags(new string[] { "AppTheme_Experimental" });
            MeClient2 = client;
            meow.Text = MeClient2.Name;
        }