我是Xamarin.Forms的新手。我尝试制作该应用程序,但尚未成功。现在的代码由2个输入和一个“开始”按钮组成。我无法将其绑定到C#代码并返回答案。
namespace FirstApp
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void Calculate
{
Double Num1;
Double Num2;
Answer = (Num1 + Num2).ToString();
}
}
<FlexLayout>
<Entry Text="{Binding Num1}"></Entry>
<Entry Text="{Binding Num2}"></Entry>
<Button Text="Go" Clicked="Calculate"></Button>
<Label Text="{Binding Answer}"></Label>
</FlexLayout>
答案 0 :(得分:-2)
在xaml文件中尝试一下:
<Window x:Class="AddTwoNumbersCSharp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AddTwoNumbersCSharp"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<TextBox Name="TextBox1" AcceptsReturn="False" AcceptsTab="False" Grid.Column="0" Grid.Row="0" FontSize="20"/>
<TextBox Name="TextBox2" AcceptsReturn="False" AcceptsTab="False" Grid.Column="1" Grid.Row="0" FontSize="20"/>
<Button Name="GoButton" Click="GoButton_Click" Grid.Column="2" Content="Go" Grid.Row="0" FontSize="20"/>
<TextBlock Name="ResultText" Grid.Row="1" FontSize="20"/>
</Grid>
</Window>
然后在C#文件中输入以下内容:
using System;
using System.Windows;
namespace AddTwoNumbersCSharp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void GoButton_Click(object sender, RoutedEventArgs e)
{
Decimal num1;
Decimal num2;
bool success = Decimal.TryParse(TextBox1.Text, out num1);
bool success2 = Decimal.TryParse(TextBox2.Text, out num2);
if (!success || !success2)
{
ResultText.Text = "Not a valid number!";
}
else
{
ResultText.Text = (num1 + num2).ToString();
}
}
}
}