How to declare and pass a decimal variable from XAML to C#?

Asked

Viewed 220 times

0

    <StackLayout>
        <Label Text="Digite um Valor:" 
               Margin="10"
               HorizontalOptions="Start"
               VerticalOptions="Start"/>

        <Entry Keyboard="Numeric"/>
 <Button Text="Calcular"
                TextColor="White"
                FontSize="Medium"
                FontAttributes="Bold"
                BackgroundColor="Accent"
                HorizontalOptions="FillAndExpand"
                VerticalOptions="EndAndExpand"
                Clicked="Handle_Clicked"/>
</StackLayout>

namespace App4
{
    public partial class MainPage : ContentPage
    { 
        public MainPage()
        {
            InitializeComponent();
        }
        private async void Handle_Clicked (object sender,  System.EventArgs e)
        {
           await Navigation.PushAsync(new Page1());
        }
    }
}
  • You want to access the Entry value in the code, that’s it?

1 answer

4


To access the value of your Entry, you can do the following:

<Entry 
    x:Name="txtValue"
    Keyboard="Numeric"/>

Note that I used the property x:Name to set a name for your Entry. By doing this, you can now access the element in your code.

namespace App4
{
    public partial class MainPage : ContentPage
    { 
        public MainPage()
        {
            InitializeComponent();
        }

        private async void Handle_Clicked (object sender,  System.EventArgs e)
        {
           // Valor do Entry
           var value = txtValue.Text;

           await Navigation.PushAsync(new Page1());
        }
    }
}

To convert the value to double, you can do it:

double newValue = Double.Parse(value);

To convert the value to decimal, you can do it:

decimal newValue = Decimal.Parse(value);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.