【发布时间】: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