微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将对象转移到另一页时出现问题

如何解决将对象转移到另一页时出现问题

我有一个简单的项目。我想将对象从登录页面转移到个人资料页面 登录页面

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

代码隐藏

我检查该对象是否没有问题

 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,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,30,30"></Image>
            <Label Text="Оберіть Ваш функціонал" FontSize="25" FontAttributes="Bold" Padding="20,0" TextColor="Black"></Label>
            <StackLayout Orientation="Horizontal"  Margin="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>

代码隐藏

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

单击登录按钮并想要传输对象时出现错误

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

请不要像您一样结束我的问题。说我我的错

解决方法

您所引用的元素meow尚未初始化。 InitializeComponent负责加载和初始化XAML元素。这就是为什么它是模板中构建者的 FIRST

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

public Profile(Client client)
    {
        InitializeComponent();

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。