如何在Xamarin中单击按钮时显示结果

问题描述 投票:0回答:2

考虑我是一个非常初级的应用程序开发人员,到目前为止他还没有开发任何东西:)我喜欢将“结果” x2(乘以2)显示在“结果” 单击后“计算”按钮的前面。另外,如何在其他页面/屏幕上显示结果?这是应用程序的图片enter image description here

这是我的代码: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

        }
    }
}
xamarin xamarin.forms xamarin.ios
2个回答
0
投票

首先,给您的Label名称

<Label x:Name="lblResult"
    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;
        // show B in front of result or on another page

        lblResult.Text += ": " + B.ToString();
    }

0
投票

首先,给标签起个名字

<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}";
    }
© www.soinside.com 2019 - 2024. All rights reserved.