首页 > 解决方案 > 如何在 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

        }
    }
}

标签: xamarinxamarin.formsxamarin.ios

解决方案


首先,为您的标签命名

<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}";
    }

推荐阅读