C#& XAML如何在第一次点击时获得响应按钮

时间:2017-05-13 22:37:10

标签: c# wpf xaml windows-phone-8.1

我实际上已经完成了大部分代码。我很欣赏这些输入,它指出了我正确的方向。我现在遇到的问题是让按钮在第一次点击时工作。

我感谢任何可以帮助我改进代码的输入。

以下是我的XAML(MainPage.xaml)& C#(MainPage.xaml.cs)代码。

<Page
x:Class="Calculator_Application.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Calculator_Application"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
    <TextBlock x:Name="textBlock_title" TextWrapping="Wrap"     Text="Calculator Application" VerticalAlignment="Top" Height="58" Width="252" Margin="86,30,0,0" SelectionChanged="textBlock_SelectionChanged" HorizontalAlignment="Left" FontSize="24" RenderTransformOrigin="-0.039,0.549" FontFamily="Calibri" Foreground="#FFBCB7B6"/>
    <Button x:Name="button_info" Content="Information" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="135,561,0,0" Click="button_Click" FontFamily="Global User Interface" Foreground="#FFF05D5D"/>
    <TextBox x:Name="textBox_number1" HorizontalAlignment="Left" Margin="29,191,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" TextChanged="textBox_number1_TextChanged"/>
    <TextBox x:Name="textBox_number2" HorizontalAlignment="Left" Margin="29,387,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
    <TextBlock x:Name="textBlock_info" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Please input numbers to calculate:" VerticalAlignment="Top" Height="30" Width="252" FontSize="16" FontStyle="Italic" Margin="29,134,0,0" SelectionChanged="textBlock_info_SelectionChanged" Foreground="#FFE5957F"/>
    <Button x:Name="button_add" Content="+" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="29,278,0,0" MinWidth="25" Height="20" FontSize="24" FontFamily="Global User Interface" Width="39" Background="Black"/>
    <Button x:Name="button_subtract" Content="-" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="106,278,0,0" MinWidth="25" Height="20" FontSize="24" FontFamily="Global User Interface" Width="39"/>
    <Button x:Name="button_multiply" Content="*" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="179,278,0,0" MinWidth="25" Height="20" FontSize="24" FontFamily="Global User Interface" Width="39"/>
    <Button x:Name="button_divide" Content="/" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="257,278,0,0" MinWidth="25" Height="20" FontSize="24" FontFamily="Global User Interface" Width="39"/>
    <TextBlock x:Name="textBlock_equals" HorizontalAlignment="Left" TextWrapping="Wrap" Text="=" VerticalAlignment="Top" Height="39" Width="38" Margin="164,387,0,0" FontSize="36"/>
    <TextBlock x:Name="textBlock_answer" HorizontalAlignment="Left" TextWrapping="Wrap" Text="answer" VerticalAlignment="Center" Height="23" Width="139" Margin="207,387,0,230" FontSize="22"/>
</Grid>
</Page>

这是C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// Added to ensure popup is availible
using Windows.UI.Popups;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641


namespace Calculator_Application
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
    private double valHolder1 = 0;
    private double valHolder2 = 0;
    private double answer = 0;

    public MainPage()
    {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.
    /// This parameter is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // TODO: Prepare page for display here.

        // TODO: If your application contains multiple pages, ensure that you are
        // handling the hardware Back button by registering for the
        // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
        // If you are using the NavigationHelper provided by some templates,
        // this event is handled for you.
    }

    private void textBlock_SelectionChanged(object sender, RoutedEventArgs e)
    {

    }



    private void textBlock_info_SelectionChanged(object sender, RoutedEventArgs e)
    {

    }

    private async void button_Click(object sender, RoutedEventArgs e)
    {
        //Creating instance for the MessageDialog Class  
        //and passing the message in it's Constructor  
        MessageDialog msgbox = new MessageDialog("Page Lynn Potter, MBL 410, 05-15-2017, Robin Deitsch");
        //Calling the Show method of MessageDialog class  
        //which will show the MessageBox  
        await msgbox.ShowAsync();
    }

    private void textBox_number1_TextChanged(object sender, TextChangedEventArgs e)
    {

    }


    private void button_add_Click(object sender, RoutedEventArgs e)
    {
        //Make sure out text box has a value
        if (textBox_number1.Text.Length != 0)
        {
            //Assign the value in text box to holder
            valHolder1 = System.Double.Parse(textBox_number1.Text);
            //Empty the text box          
            textBox_number1.Text = string.Empty;

            if (textBox_number2.Text.Length != 0)
            {
                //Assign the value in text box to holder
                valHolder2 = System.Double.Parse(textBox_number2.Text);
                //Empty the text box          
                textBox_number2.Text = string.Empty;
            }
        }

        else
        {
            //Calculate answer
            answer = valHolder1 + valHolder2;
            //Assign answer to text block
            textBlock_answer.Text = answer.ToString();
        }
     }

    private void button_subtract_Click(object sender, RoutedEventArgs e)
    {
        //Make sure out text box has a value
        if (textBox_number1.Text.Length != 0)
        {
            //Assign the value in text box to holder
            valHolder1 = System.Double.Parse(textBox_number1.Text);
            //Empty the text box          
            textBox_number1.Text = string.Empty;

            if (textBox_number2.Text.Length != 0)
            {
                //Assign the value in text box to holder
                valHolder2 = System.Double.Parse(textBox_number2.Text);
                //Empty the text box          
                textBox_number2.Text = string.Empty;
            }
        }

        else
        {
            //Calculate answer
            answer = valHolder1 - valHolder2;
            //Assign answer to text block
            textBlock_answer.Text = answer.ToString();
        }
    }

    private void button_multiply_Click(object sender, RoutedEventArgs e)
    {
        //Make sure out text box has a value
        if (textBox_number1.Text.Length != 0)
        {
            //Assign the value in text box to holder
            valHolder1 = System.Double.Parse(textBox_number1.Text);
            //Empty the text box          
            textBox_number1.Text = string.Empty;

            if (textBox_number2.Text.Length != 0)
            {
                //Assign the value in text box to holder
                valHolder2 = System.Double.Parse(textBox_number2.Text);
                //Empty the text box          
                textBox_number2.Text = string.Empty;
            }
        }

        else
        {
            //Calculate answer
            answer = valHolder1 * valHolder2;
            //Assign answer to text block
            textBlock_answer.Text = answer.ToString();
        }
    }

    private void button_divide_Click(object sender, RoutedEventArgs e)
    {
        //Make sure out text box has a value
        if (textBox_number1.Text.Length != 0)
        {
            //Assign the value in text box to holder
            valHolder1 = System.Double.Parse(textBox_number1.Text);
            //Empty the text box          
            textBox_number1.Text = string.Empty;

            if (textBox_number2.Text.Length != 0)
            {
                //Assign the value in text box to holder
                valHolder2 = System.Double.Parse(textBox_number2.Text);
                //Empty the text box          
                textBox_number2.Text = string.Empty;
            }
        }

        else
        {
            //Calculate answer
            answer = valHolder1 / valHolder2;
            //Assign answer to text block
            textBlock_answer.Text = answer.ToString();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您为 button_info 按钮配置了事件处理程序。

<Button x:Name="button_info" Content="Information" 
        HorizontalAlignment="Left" VerticalAlignment="Top" 
        Margin="135,561,0,0" 
        FontFamily="Global User Interface" Foreground="#FFF05D5D"
        Click="button_Click"/>

我没有看到你为其他按钮配置处理程序的位置。

XAML中应该有这样的东西。

<Button x:Name="button_add" Click = "button_add_Click" />
<Button x:Name="button_subtract" Click = "button_subtract_Click"   />  
<Button x:Name="button_multiply" Click = "button_multiply_Click" />
<Button x:Name="button_divide"Click = "button_divide_Click" />