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

如何从 Xamarian 中的另一个类访问变量?

如何解决如何从 Xamarian 中的另一个类访问变量?

我通过允许用户输入源类中每个性别的数量来计算男性和女性占用户总性别的百分比。我还想将源文件“POF”和“POM”中的变量“携带”到目标类中,我该怎么做?正如你所看到的,我试图将值绑定到目标标签中,但没有......

我想从中获取变量的源 .XAML 表单

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

源 .cs 类

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;

        }
    }

我想将变量绑定到的目标 .XAML 表单

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

目标 .cs 类

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

解决方法

如果您从 HomePage 导航到 OutputPageExt,您可以将当前的 HomePage 作为参数传递。

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

   BindingContext = this;

}

在 HoemPage 中

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

例如,您可以创建一个名为 Variables.cs 的新公共类,并在其中定义变量。

然后您的 HomePage 和 OutputPage 类都可以作为 Variables.POM 访问它们

,

方法:1 您可以使用两种方法访问该变量,创建一个通用静态类并将值分配给该静态变量,然后访问任何您想要的地方。

方法:2 这是导航时传递值的方法

Navigation.PushAsync(new HomePage (val));

在参数化构造函数后面制作页面代码如

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;

        }
    }

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