首页 > 解决方案 > 如何从 xamarin 中的条目中读取 int 值?

问题描述

我正在尝试从条目中获取 int 值。.Text 获取字符串,但我不知道如何保存 int 值。Kullanicilarr.Telefonno 和 Kullanicilarr.GrupID 被限制为获取 long 和 int 值,因为我不能使用 Phone.Text 或 GrupID.Text。是否有像“.Text”这样的参数名称可以用来获取 int 值?

<StackLayout Spacing="20" Padding="15">
            <Label Text="Name" FontSize="Medium" />
            <Entry Text="Enter Name" FontSize="Small" x:Name="Name" />
            <Label Text="Phone" FontSize="Medium" />
            <Entry Text="Enter Phone Number" FontSize="Small" x:Name="Phone"/>
            <Label Text="Group Id" FontSize="Medium" />
            <Entry Text="Enter Group id" FontSize="Small" x:Name="Group"/>
            <Button x:Name="btn" Text="Add" Clicked="btn_Clicked"/>
        </StackLayout>
async void btn_Clicked(object sender, EventArgs e)
        {
            Kullanicilarr.Kullaniciadi = Name.Text;
            Kullanicilarr.Telefonno = Phone.Text;
            Kullanicilarr.GrupID = Group.Text;

            await Navigation.PopModalAsync();
        }

我希望在那个 Kullanicilar."something" with Group."something" 中保持价值观。

标签: c#androidxamlxamarin

解决方案


您需要将它们转换为整数。输入文本始终是字符串,但您可能会将输入限制为整数。

但是,您可以轻松地尝试使用以下方法转换值:

var parseOk = int.TryParse(Phone.Text, out int result);

然后,您可以在尝试将其分配给模型之前测试该值是否为 int:

if (parseOk)
    Kullanicilarr.Telefonno = result;

推荐阅读