Sylas Coker
Sylas Coker

Reputation: 35

How do I access a variable from another class in Xamarian?

I'm calculating the percentage of Males and Females from the total user gender by allowing the user to input the number of each gender from the source class. I also want to "carry" the variables from the source file "POF" and "POM" into the target class, how would I go about doing this? As you can see I've attempted to bind the values into the target labels but nothing...

The source .XAML Form I want to get the varibles from

    <ContentPage.Content>
        <StackLayout>
            <Label Text="Change Amount of Male Users"/>
            <Entry x:Name="MaleUsersEntry"></Entry>

            <Label Text="Change Amount of Female Users"/>
            <Entry x:Name="FemaleUsersEntry"></Entry>

            <Button Text="Calculate" Clicked="Calculate_Button"></Button>

        </StackLayout>
    </ContentPage.Content>

The source .cs Class

public partial class HomePage : ContentPage
    {
        public double POF { get; set; }
        public double POM { get; set; }

        public HomePage()
        {
            InitializeComponent();
        }

        private void Calculate_Button(object sender, EventArgs e)
        {
            string maleUsersString = MaleUsersEntry.Text;
            string femaleUsersString = FemaleUsersEntry.Text;

            double maleUsersDouble = 65.0D;
            double femaleUsersDouble = 35.0D;

            try
            {
                double.TryParse(maleUsersString, out maleUsersDouble);
                double.TryParse(femaleUsersString, out femaleUsersDouble);
            }
            catch (FormatException)
            {
                Console.WriteLine("Please Input Digits." + maleUsersString + "&" + femaleUsersString + " are not numbers.");
            }

            double totalUsers = maleUsersDouble + femaleUsersDouble;

            double PercentageOfFemales = (femaleUsersDouble / totalUsers) * 100;
            double PercentageOfMales = (maleUsersDouble / totalUsers) * 100;

            PercentageOfFemales = Math.Round(PercentageOfFemales, 2);
            PercentageOfMales = Math.Round(PercentageOfMales, 2);

            POF = PercentageOfFemales;
            POM = PercentageOfMales;

        }
    }

The Target .XAML Form I want to bind the varible to

    <ContentPage.Content>
        <StackLayout>
            <Label Text="The percentage of the User being Male is: " FontSize="Small"/>
            <Label Text="{Binding POM}" FontSize="Large"/>

            <Label Text="The percentage of the User being Female is: " FontSize="Small"/>
            <Label Text="{Binding POF}" FontSize="Large"/>

            <Button Text="Change Metrics" Clicked="UserChange_OnClicked"/>
        </StackLayout>
    </ContentPage.Content>

The Target .cs Class

    public partial class OutputPageExt : ContentPage
    {
        double POM;
        double POF;
        public OutputPageExt()
        {
            InitializeComponent();
            HomePage viewModel = new HomePage();
            this.POM = viewModel.POM;
            this.POF = viewModel.POF;
        }
    }

Upvotes: 0

Views: 483

Answers (3)

Zubair Munir
Zubair Munir

Reputation: 478

Method:1 You can access the variable with two methods make a generic static class and assign value to this static variable and then access anywhere ..where you want.

Method:2 And here is the way to pass value when you navigate

Navigation.PushAsync(new HomePage (val));

Make page code behind parametrized constructor Like

public partial class HomePage : ContentPage
    {
        public double POF { get; set; }
        public double POM { get; set; }
       public double ReceivedValue { get; set; }


        public HomePage( Double val) //Here is to Receive the value
        {
            InitializeComponent();
            ReceivedValue =Val;
        }

        private void Calculate_Button(object sender, EventArgs e)
        {
            string maleUsersString = MaleUsersEntry.Text;
            string femaleUsersString = FemaleUsersEntry.Text;

            double maleUsersDouble = 65.0D;
            double femaleUsersDouble = 35.0D;

            try
            {
                double.TryParse(maleUsersString, out maleUsersDouble);
                double.TryParse(femaleUsersString, out femaleUsersDouble);
            }
            catch (FormatException)
            {
                Console.WriteLine("Please Input Digits." + maleUsersString + "&" + femaleUsersString + " are not numbers.");
            }

            double totalUsers = maleUsersDouble + femaleUsersDouble;

            double PercentageOfFemales = (femaleUsersDouble / totalUsers) * 100;
            double PercentageOfMales = (maleUsersDouble / totalUsers) * 100;

            PercentageOfFemales = Math.Round(PercentageOfFemales, 2);
            PercentageOfMales = Math.Round(PercentageOfMales, 2);

            POF = PercentageOfFemales;
            POM = PercentageOfMales;

        }
    }

Upvotes: 1

Alan
Alan

Reputation: 362

You could create a new public class called Variables.cs, for example, and define the variables there.

Then both your HomePage and OutputPage classes can access them as Variables.POM

Upvotes: 1

Lucas Zhang
Lucas Zhang

Reputation: 18861

If you navigate from HomePage to OutputPageExt , you could pass the current HomePage as a parameter .

public OutputPageExt(HomePage page)
{
   InitializeComponent();
   
   this.POM = page.POM;

   BindingContext = this;

}

And in HoemPage

 this.Navigation.PushAsync(new OutputPageExt(this));

Upvotes: 1

Related Questions