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

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());
        }
    }
}
    
asked by anonymous 11.06.2018 / 15:41

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 x:Name property to set a name for its 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 this:

double newValue = Double.Parse(value);

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

decimal newValue = Decimal.Parse(value);
    
11.06.2018 / 16:30