【问题标题】:Xamarin Public Class data how to acess it properlyXamarin Public Class 数据如何正确访问
【发布时间】:2021-04-05 15:23:52
【问题描述】:

我从另一个页面获取数据

var check = new check
            {
                Age = (int)Convert.ToInt64(Ageg)
            };
            
            var secondPage = new Sdeduction ();
            secondPage.BindingContext = check;
            await Navigation.PushAsync(secondPage);

并收到

public class check
    {
        public int Age { get; set;  }
    }
public Sdeduction()
            {    
    InitializeComponent();    
        
    }

但我无法直接访问 Age 进行某些计算。 lile tta.Text = Age.ToString();

所以我使用绑定。哪个效果很好。

 <Label x:Name="Ages" Text="{Binding Age}"></Label>

但我想检查页面之间传递了哪些数据的年龄,但我无法弄清楚我尝试了

public Sdeduction()
        {    
InitializeComponent();    
    int aged = (int)Convert.ToInt64(Ages.Text);
        if (aged == 0)
           {
             TTA.IsVisible = true;
           }
        if(aged > 0)
           {
             TTB.IsVisible = true;
             //Ds.IsVisible = true;
            }
}

但它是 Ages.Text 为空。

主要问题是如何从公共类检查中获取数据

  public class check
    {
        public int Age { get; set;  }
    }

如何在 c# 的其他部分获得这个公共 int。从哪里获取传递的数据

我可以绑定,但https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/ 是关于 xaml 级别的绑定属性和上下文的全部内容。我喜欢从其他页面获取数据,但只能与 x:names Ages 绑定,但在页面加载/InitializeComponent 时它没有年龄名称。

【问题讨论】:

  • 请阅读任何有关绑定的教程。您不应该尝试访问Ages.text。您应该访问“绑定”变量Age2。 (我给它起了这个名字,以避免与您已经在其他地方拥有的Age 混淆。)Age2 应该在您的页面的BindingContext 中,Deduction 需要设置它。假设您用作 BindingContext 的类名为 MyViewModel。然后您可以在页面加载期间以((MyViewModel)BindingContext).Age2 的身份访问。
  • 好的,我实际上能够绑定,并且可能在页面呈现后可以运行一些有帮助的功能。但我的主要问题是获取通过这种方式传递的数据。 github.com/xamarin/xamarin-forms-samples/blob/master/… 在新语言中停留了一周以上。如果有帮助,我非常感谢

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

您将 Sdeduction 实例的 BindingContext 设置为检查实例,然后导航到 Sdeduction。这是导航时传递数据的正确方式。

但是当你在 Sdeduction() 方法中检查 Age as page load 时,它总是在 Sdeduction 页面设置 BindingContext 之前得到 0。您需要在 OnAppearing() 方法中加载。

主页:

xaml:

  <StackLayout>
    <Label x:Name="label" Text="10" />
    <Button Clicked="Button_Clicked" Text="Navigate" />
</StackLayout>

后面的代码:

 public partial class MainPage : ContentPage
{
    string Ageg;
    public MainPage()
    {
        InitializeComponent();
        Ageg = label.Text;
    }

    async void Button_Clicked(object sender, EventArgs e)
    {
        var check = new check
        {
            Age = (int)Convert.ToInt64(Ageg)
        };

        var secondPage = new Sdeduction();
        secondPage.BindingContext = check;
        await Navigation.PushAsync(secondPage);
    }

}   

推理:

Xaml:

<StackLayout>
        <Label
            HorizontalOptions="CenterAndExpand"
            Text="Welcome to Sdeduction!"
            VerticalOptions="CenterAndExpand" />

        <Label x:Name="Ages" Text="{Binding Age}" />

        <StackLayout Orientation="Horizontal">
            <Button
                x:Name="TTA"
                IsVisible="false"
                Text="TTA" />
            <Button
                x:Name="TTB"
                IsVisible="false"
                Text="TTB" />
        </StackLayout>
    </StackLayout>

后面的代码:

   protected override void OnAppearing()
    {
        base.OnAppearing();

        int aged = (int)Convert.ToInt64(Ages.Text);
        if (aged == 0)
        {
            TTA.IsVisible = true;
        }
        if (aged > 0)
        {
            TTB.IsVisible = true;
            //Ds.IsVisible = true;
        }

    }

请查看截图:

https://imgur.com/vx6GZFw

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-09-24
  • 2019-11-28
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 2012-03-29
相关资源
最近更新 更多