WP8简单的计算器
<Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitlePanel 包含应用程序的名称和页标题--> <StackPanel Grid.Row="0" Margin="12,17,0,28"> <TextBlock Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel> <!--ContentPanel - 在此处放置其他内容--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> </Grid> <Grid x:Name="MyGrid" HorizontalAlignment="Left" Height="597" Margin="7,0,0,0" Grid.Row="1" VerticalAlignment="Top" Width="463"> <TextBlock x:Name="InputInformation" HorizontalAlignment="Left" Height="68" TextWrapping="Wrap" VerticalAlignment="Top" Width="453" FlowDirection="LeftToRight" TextAlignment="Right"/> <TextBlock x:Name="OperationResult" HorizontalAlignment="Left" Height="65" Margin="0,73,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="453" FlowDirection="LeftToRight" TextAlignment="Right"/> <Button x:Name="B0" Content="0" HorizontalAlignment="Left" Height="110" Margin="19,487,0,0" VerticalAlignment="Top" Width="120" Click="Btn_ButClick"/> <Button x:Name="Result" Content="=" HorizontalAlignment="Left" Height="110" Margin="119,487,0,0" VerticalAlignment="Top" Width="120" Click="ResultJG"/> <Button x:Name="Del" Content="Del" HorizontalAlignment="Left" Height="110" Margin="221,487,0,0" VerticalAlignment="Top" Width="120" Click="Delclick"/> <Button x:Name="Divide" Content="/" HorizontalAlignment="Left" Height="110" Margin="321,487,0,0" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.5,0.5" Click="YunSuan"/> <Button x:Name="B1" Content="1" HorizontalAlignment="Left" Height="110" Margin="19,393,0,0" VerticalAlignment="Top" Width="120" Click="Btn_ButClick"/> <Button x:Name="B2" Content="2" HorizontalAlignment="Left" Height="110" Margin="119,393,0,0" VerticalAlignment="Top" Width="120" Click="Btn_ButClick"/> <Button x:Name="B3" Content="3" HorizontalAlignment="Left" Height="110" Margin="221,393,0,0" VerticalAlignment="Top" Width="120" Click="Btn_ButClick"/> <Button x:Name="Multiply" Content="*" HorizontalAlignment="Left" Height="110" Margin="321,393,0,0" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.5,0.5" Click="YunSuan"/> <Button x:Name="B4" Content="4" HorizontalAlignment="Left" Height="110" Margin="19,299,0,0" VerticalAlignment="Top" Width="120" Click="Btn_ButClick"/> <Button x:Name="B5" Content="5" HorizontalAlignment="Left" Height="110" Margin="119,299,0,0" VerticalAlignment="Top" Width="120" Click="Btn_ButClick"/> <Button x:Name="B6" Content="6" HorizontalAlignment="Left" Height="110" Margin="221,299,0,0" VerticalAlignment="Top" Width="120" Click="Btn_ButClick"/> <Button x:Name="Minus" Content="-" HorizontalAlignment="Left" Height="110" Margin="321,299,0,0" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.5,0.5" Click="YunSuan"/> <Button x:Name="B7" Content="7" HorizontalAlignment="Left" Height="110" Margin="19,205,0,0" VerticalAlignment="Top" Width="120" Click="Btn_ButClick"/> <Button x:Name="B8" Content="8" HorizontalAlignment="Left" Height="110" Margin="119,205,0,0" VerticalAlignment="Top" Width="120" Click="Btn_ButClick"/> <Button x:Name="B9" Content="9" HorizontalAlignment="Left" Height="110" Margin="221,205,0,0" VerticalAlignment="Top" Width="120" Click="Btn_ButClick"/> <Button x:Name="Plus" Content="+" HorizontalAlignment="Left" Height="110" Margin="321,205,0,0" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.5,0.5" Click="YunSuan"/> </Grid> </Grid>
namespace PhoneApp1 { public partial class Calculator : PhoneApplicationPage { public string Operation = "";//记录一下之前一次按下的运算符 public double Num = 0;//结果 public Calculator() { InitializeComponent(); } private void Btn_ButClick(object sender, RoutedEventArgs e) { if (Operation == "=") { OperationResult.Text = ""; InputInformation.Text = ""; Num = 0; } string s = (sender as Button).Content.ToString(); OperationResult.Text = OperationResult.Text + s; InputInformation.Text = InputInformation.Text + s; } private void YunSuan(object sender, RoutedEventArgs e) { string fh= (sender as Button).Content.ToString(); OperationNum(fh); InputInformation.Text = InputInformation.Text + fh; OperationResult.Text = "" ; } private void OperationNum(string fh) { if (OperationResult.Text != "") { if (Operation == "+") { Num = Num +double.Parse( OperationResult.Text.ToString()); Operation = fh; } else if (Operation == "-") { Num = Num - double.Parse(OperationResult.Text.ToString()); Operation = fh; } else if (Operation == "*") { Num = Num * double.Parse(OperationResult.Text.ToString()); Operation = fh; } else if (Operation == "/") { if (double.Parse(OperationResult.Text.ToString()) != 0) Num = Num / double.Parse(OperationResult.Text.ToString()); else Num = 0; Operation = fh; } else//(Operation=="" || Operation="=") { Num = double.Parse(OperationResult.Text.ToString()); Operation = fh; } } } private void ResultJG(object sender, RoutedEventArgs e) { OperationNum("="); OperationResult.Text = Num.ToString(); } private void Delclick(object sender, RoutedEventArgs e) { OperationResult.Text =""; InputInformation.Text = ""; Num = 0; } } }