How can receive a value from my Login.cs to MasterPageDetail?

When i logged in to app i need to receive the code and username from a dataset, but when i sent those values from my LoginPage.cs to my MasterPage.cs it is received NULL. What im doing wrong?

I have a Class called "Login" which receive the value, but doesnt send them.

Here is my code

/*LoginPage.cs*/
    var VService = DependencyService.Get<IntValidateLogin>();
    string resultC = VService.UserCode(txtUser.Text, txtPwd.Text);
    string resultN = VService.UserName(txtUser.Text, txtPwd.Text);
        if (resultC == "FALSE" || resultN == "FALSE")
        {
            await DisplayAlert("Alerta", "Usted no tiene permisos para ingresar a la Aplicación Smart." +
            "Favor verifique sus credenciales", "Aceptar");
        }
        else
        {
            Login lc = new Login
            {
                LoginUserCode = resultC,
                LoginUserName = resultN
            };
            await Navigation.PushModalAsync(new Views.MenuPage(lc), false);
        }

/*Login.cs*/
public class Login
    {
        public string LoginUserCode { get; set; }
        public string LoginUserName { get; set; }
    }

/--------------------------------------------------------------------------------------------------------------------------------/

/*MainPage.xaml.cs*/
public partial class MenuPage : MasterDetailPage
    {
        public string code { get; set; }
        public string name { get; set; }
        public MenuPage()
        {
            InitializeComponent();
            MasterPage.ListView.ItemSelected += ListView_ItemSelected;
        }
        public MenuPage(Login login)
        {
            code = login.LoginUserCode;
            name = login.LoginUserName;
        }

/*MainPage.xaml*/
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SmartAPPVB.Views.MenuPage"
             xmlns:pages="clr-namespace:SmartAPPVB.Views">
    <MasterDetailPage.Master>
        <pages:MenuPageMaster x:Name="MasterPage" />
    </MasterDetailPage.Master>
    <MasterDetailPage.Detail>
        <NavigationPage>
            <x:Arguments>
                <pages:MenuPageDetail>

                </pages:MenuPageDetail>
            </x:Arguments>
        </NavigationPage>
    </MasterDetailPage.Detail>
</MasterDetailPage>

/-------------------------------------------------------------------------------------------------------------------------/
/MainPageDetal.xaml.cs/

 public MenuPageDetail()
        {
            InitializeComponent();
            lblName.FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label));
            lbluser_.FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label));
            lblwelc.FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label));
        }

/*MainPageDetal.xaml*/

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SmartAPPVB.Views.MenuPageDetail"
             Title="[email protected] Mobile">
    <ContentPage.Content>
        <ScrollView VerticalOptions="FillAndExpand"
                HorizontalOptions="Fill">
            <StackLayout Padding="30"
                     Spacing="20">
                <Image x:Name="UserLogo"
                   Source="@drawable/User"/>
                <Label x:Name="lblwelc"
                       Text="Bienvenido al Sistema de negocios [email protected]" 
                   HorizontalTextAlignment="Center"/>
                <Label x:Name="lbluser_" Text="Nombre del Usuario" 
                   HorizontalTextAlignment="Center"/>
                <Label x:Name="lblName"
                   Text="NOMBRE"
                   HorizontalTextAlignment="Center"/>
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

How can pass the values to de DetailPage or, which is the best way to do it ?. Can i do it on my .CS or on my .XAML doing a binding ?
Hope you all can help me and understand my question.
Regards.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Firstly, you need to add InitializeComponent() in your custom constructor to load the xaml interface:

    public MenuPage(Login login)
    {
        InitializeComponent();
        code = login.LoginUserCode;
        name = login.LoginUserName;
    
        // Register the master list's event
        MasterPage.ListView.ItemSelected += ListView_ItemSelected;
    }
    

    Then name your detail page in XAML so that you can access it in the code behind:

    <x:Arguments>
        <pages:MenuPageDetail x:Name="DetailPage"/>
    </x:Arguments>
    

    Secondly, define a method to access data in your MenuPageDetail page:

    public string code { get; set; }
    public string name { get; set; }
    public void SetLoginInfo(Login login)
    {
        code = login.LoginUserCode;
        name = login.LoginUserName;
    }
    

    At last pass the data like:

    public MenuPage(Login login)
    {
        InitializeComponent();
        code = login.LoginUserCode;
        name = login.LoginUserName;
    
        DetailPage.SetLoginInfo(login);
    }
    
Sign In or Register to comment.