C#和XAML如何让按钮在第一次点击时响应
Posted
技术标签:
【中文标题】C#和XAML如何让按钮在第一次点击时响应【英文标题】:C# & XAML how to get button to respond on first click 【发布时间】:2017-10-13 00:09:40 【问题描述】:实际上,我的大部分代码都可以工作了。我很感激您的意见,它为我指明了正确的方向。我现在遇到的问题是让按钮在第一次点击时工作。
感谢任何可以帮助我改进代码的意见。
以下是我的 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();
【问题讨论】:
Stack Overflow 不是教程信息的合适来源,而这正是您需要的。阅读描述 WPF API 以及如何使用它的许多可用教程中的任何一个。您的第一步将是通过使用“视图模型”数据结构来轻松地将数据与 UI 分开,这些数据结构具有绑定到 UI 元素属性以进行显示的公共属性。您还需要了解ICommand
,如何实现和绑定到 Button.Command
属性,以处理计算器按钮的用户输入。
你最好使用 MVVM。如果您有兴趣,请告诉我。
如上所说,使用MVVM、WPF、INotifyPropertyChanged、ICommand就好了。在跳转到代码之前,您需要阅读一些内容。
我不是在寻求教程,只是朝着正确的方向迈出了一步。我从来没有要求你为我编码。我只是在寻找资源来帮助我更好地掌握 C# 和 XAML 的概念。就像我提到的那样,我很欣赏输入(无论是好是坏)。我肯定会研究 MVVM 和提到的其他一些事情。
【参考方案1】:
您为 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" />
【讨论】:
谢谢,我会继续添加事件处理程序。我一定是忘了把它们放进去,完全忽略了它们。感谢您的帮助。 我仔细检查了我的代码并且事件处理程序现在在那里,但是仍然需要单击该按钮两次才能将我的答案放入答案文本块中。是因为我正在使用模拟器还是我仍然错过了什么?以上是关于C#和XAML如何让按钮在第一次点击时响应的主要内容,如果未能解决你的问题,请参考以下文章