如何解决Xamarin 公共类数据如何正确访问
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# 的其他部分获得这个公共整数。从哪里获取传递的数据
我可以绑定,但 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/ 是关于 xaml 级别的绑定属性和上下文。我喜欢从其他页面获取数据,但只能与 x:names Ages 绑定,但在页面加载/InitializeComponent 时它没有年龄名称。
解决方法
您将 Sdeduction 实例的 BindingContext 设置为 check 实例,然后导航到 Sdeduction。这是导航时传递数据的正确方式。
但是当您在 Sdeduction() 方法中检查 Age 作为页面加载时,它总是在 Sdeduction 页面设置 BindingContext 之前获得 0。您需要在 OnAppearing() 方法中加载。
主页:
xml:
<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);
}
}
演绎:
XML:
<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;
}
}
请检查截图:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。