[英]C# & XAML how to get button to respond on first click
I actually got the bulk of my code to work. 实际上,我的大部分代码都可以工作。 I appreciate the input, it pointed me in the correct direction. 我感谢您的投入,它为我指明了正确的方向。 The problem I am having now is getting the buttons to work on the first click. 我现在遇到的问题是让按钮在第一次单击时起作用。
I appreciate any input that would help me improve my code. 我感谢任何有助于我改进代码的输入。
Below are my XAML (MainPage.xaml) & C# (MainPage.xaml.cs) code. 以下是我的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>
Here is the C# 这是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();
}
}
}
}
You have an event handler configured for the button_info button. 您已经为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"/>
I don't see where you've configured handlers for the other buttons. 我看不到您为其他按钮配置了处理程序的位置。
There should be something like this in the XAML. 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" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.