xamarin - 如何在 Xamarin 中单击按钮时显示结果
问题描述
考虑我是一个非常初学者的应用程序开发人员,到目前为止还没有开发过任何东西:) 我喜欢在单击“计算”按钮后将“数字”x2(乘以 2)显示在“结果”前面。另外,如何在另一个页面/屏幕上显示结果?这是应用程序的 图片 在此处输入图像描述
这是我的代码:MainPage.xaml
<StackLayout>
<Label
Text="Give me a number"
Margin="0,50,0,0" Padding="13,0,0,0"/>
<Entry Placeholder="number"
x:Name="numb"
Keyboard="Numeric"
Margin="130,-30,40,0"
Opacity="1" Rotation="0"
TranslationX="16" />
<Button Text=" Calculate "
BackgroundColor="#F6DEDE"
FontSize="Large"
FontAttributes="Bold"
/>
<Label
Text=" Result "
FontSize="30"
Margin="0, 20, 0, 0"
Padding="10,0,0,0"/>
</StackLayout>
这是 MainPage.xaml.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Test
{
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
void CalculateClicked(object sender, EventArgs args)
{
float numb = float.Parse(this.numb.Text);
float B = 100 * numb * 2;
// show B in front of result or on another page
}
}
}
解决方案
首先,为您的标签命名
<Label x:Name="result"
Text=" Result "
FontSize="30"
Margin="0, 20, 0, 0"
Padding="10,0,0,0"/>
现在,您必须在标签中设置结果
void CalculateClicked(object sender, EventArgs args)
{
float numb = float.Parse(this.numb.Text);
float B = 100 * numb * 2;
result.Text = $": {B}";
}
推荐阅读
- git - 如何找到包含我的提交/树/blob/delta 的包文件?
- graphics - FilledCurve 中不需要的线(Wolfram Mathematica)
- google-cloud-run - 将默认的 302 HTTP->HTTPS 重定向更改为 301
- bash - MacBook Pro ZSH 终端提示
- javascript - 如何正确使用 Laravel 和 Vue?
- java - 在 Recyclerview 中更改 Edittext 的 IME 选项
- mysql - Airflow MySql 连接问题
- python - 来自 Python 和 Selenium 中的函数的 send_keys
- php - 加密波斯消息
- mysql - 如何在 SQL 中为相同的 AgentID 选择下一个日期时间