我目前正在尝试第一次在Visual Studio中练习使用Xamarin,因此我决定尝试制作一个计算器应用程序。我尝试了各种方法来制作此应用,但发现使用属性可以使其更有效地运行。但是,在尝试从其他来源实现某种格式后,结果总是为零。
XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CalculatorApp.Calculator2">
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="display" TargetType="Label">
<Setter Property="BackgroundColor" Value="Black"/>
<Setter Property="TextColor" Value="White"/>
<Setter Property="FontSize" Value="60"/>
<Setter Property="FontAttributes" Value="None"/>
<Setter Property="VerticalTextAlignment" Value="End"/>
<Setter Property="HorizontalTextAlignment" Value="End"/>
</Style>
<Style x:Key="buttonYellow" TargetType="Button">
<Setter Property="BackgroundColor" Value="#FF9F2A"/>
<Setter Property="TextColor" Value="White"/>
<Setter Property="FontSize" Value="16"/>
</Style>
<Style x:Key="buttonGrey" TargetType="Button">
<Setter Property="BackgroundColor" Value="#AAAAAA"/>
<Setter Property="TextColor" Value="#424242"/>
<Setter Property="FontSize" Value="16"/>
</Style>
<Style x:Key="buttonDarker" TargetType="Button">
<Setter Property="BackgroundColor" Value="#333333"/>
<Setter Property="TextColor" Value="White"/>
<Setter Property="FontSize" Value="16"/>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<!--Content Starts here-->
<ContentPage.Content>
<Grid BackgroundColor="Black">
<!-- Place new controls here -->
<!--Row and Column Definitions-->
<Grid.RowDefinitions>
<RowDefinition Height="2.5*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<!--First Row-->
<Label x:Name="displayLbl" Text="{Binding LblText}" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Style="{StaticResource display}"/>
<!--AC and Divide-->
<Button Text="AC" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Style="{StaticResource buttonGrey}" Clicked="ClearButton"/>
<Button Text="÷" Grid.Row="1" Grid.Column="3" Style="{StaticResource buttonYellow}" Clicked="DivideButton"/>
<!--Third Row-->
<Button Text="7" Grid.Row="2" Grid.Column="0" Style="{StaticResource buttonDarker}" Clicked="CalculatorButtons"/>
<Button Text="8" Grid.Row="2" Grid.Column="1" Style="{StaticResource buttonDarker}" Clicked="CalculatorButtons"/>
<Button Text="9" Grid.Row="2" Grid.Column="2" Style="{StaticResource buttonDarker}" Clicked="CalculatorButtons"/>
<Button Text="×" Grid.Row="2" Grid.Column="3" Style="{StaticResource buttonYellow}" Clicked="MultiplyButton"/>
<!--Fourth Row-->
<Button Text="4" Grid.Row="3" Grid.Column="0" Style="{StaticResource buttonDarker}" Clicked="CalculatorButtons"/>
<Button Text="5" Grid.Row="3" Grid.Column="1" Style="{StaticResource buttonDarker}" Clicked="CalculatorButtons"/>
<Button Text="6" Grid.Row="3" Grid.Column="2" Style="{StaticResource buttonDarker}" Clicked="CalculatorButtons"/>
<Button Text="-" Grid.Row="3" Grid.Column="3" Style="{StaticResource buttonYellow}" Clicked="MinusButton"/>
<!--Fifth Row-->
<Button Text="1" Grid.Row="4" Grid.Column="0" Style="{StaticResource buttonDarker}" Clicked="CalculatorButtons"/>
<Button Text="2" Grid.Row="4" Grid.Column="1" Style="{StaticResource buttonDarker}" Clicked="CalculatorButtons"/>
<Button Text="3" Grid.Row="4" Grid.Column="2" Style="{StaticResource buttonDarker}" Clicked="CalculatorButtons"/>
<Button Text="+" Grid.Row="4" Grid.Column="3" Style="{StaticResource buttonYellow}" Clicked="PlusButton"/>
<!--Sixth Row-->
<Button Text="0" Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3" Style="{StaticResource buttonDarker}" Clicked="CalculatorButtons"/>
<Button Text="=" Grid.Row="5" Grid.Column="3" Style="{StaticResource buttonYellow}" Clicked="EqualButton"/>
</Grid>
</ContentPage.Content>
</ContentPage>
隐藏代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace CalculatorApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Calculator2 : ContentPage
{
private double _Number = 0;
public double Number
{
get
{
return _Number;
}
set
{
_Number = value;
displayLbl.Text = _Number.ToString();
}
}
private double _Result;
public double Result
{
get
{
return _Result;
}
set
{
_Result = value;
displayLbl.Text = _Result.ToString();
}
}
public Calculator2 ()
{
InitializeComponent ();
}
private void CalculatorButtons(object sender, EventArgs e)
{
displayLbl.Text += ((Button)sender).Text;
}
#region Operation Buttons
private void PlusButton(object sender, EventArgs e)
{
Result += Number;
Number = 0.0;
}
private void MinusButton(object sender, EventArgs e)
{
Result -= Number;
Number = 0.0;
}
private void MultiplyButton(object sender, EventArgs e)
{
Result *= Number;
Number = 0.0;
}
private void DivideButton(object sender, EventArgs e)
{
Result /= Number;
Number = 0.0;
}
private void EqualButton(object sender, EventArgs e)
{
Result = Number;
}
private void ClearButton(object sender, EventArgs e)
{
ClearCommand();
}
#endregion
public void ClearCommand() {
displayLbl.Text = null;
}
}
}
任何帮助将不胜感激。
答案 0 :(得分:0)
1。)您的清除命令还应该将结果重置为0,因为通过查看此代码,无论您清除了多少次,结果似乎仍会累积。
2。)form
始终为零,因为除了在每次操作员函数调用后将其重置为0之外,您没有将其设置为任何位置(未绑定到任何按钮)。根据您当前的设计,您可以在Number
函数内执行的操作是继续根据CalculatorButtons
附加数字字符串,并在调用+-*函数后将其转换为数字并执行您的计算。
这种方法是否是最有效的方法(不是)是完全不同的问题。