c# - 如何从 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" 中保持价值观。
解决方案
您需要将它们转换为整数。输入文本始终是字符串,但您可能会将输入限制为整数。
但是,您可以轻松地尝试使用以下方法转换值:
var parseOk = int.TryParse(Phone.Text, out int result);
然后,您可以在尝试将其分配给模型之前测试该值是否为 int:
if (parseOk)
Kullanicilarr.Telefonno = result;
推荐阅读
- python - 为什么我的随机整数变量不刷新?
- kubernetes - Kubernetes 滚动更新仅通过更改 env 变量
- ios - 使用已弃用的 kSecAttrAccessibleAlwaysThisDeviceOnly 会发生什么?
- javascript - 如何制作实用程序类或功能组件以公开少数常用实用程序方法
- css - 鼠标离开时悬停效果跳跃
- php - MYSQL 查询以排除结果中具有空值的列
- python - 如何根据文件夹结构自动化和调整 Python 代码
- c# - void 数组扩展方法什么都不做
- powershell - ForEach 和 $_ 空结果
- html - 覆盖而不使用相对父级和填充子级