为什么我不能在Windows 8.1应用程序中删除TextBox文本?

时间:2015-07-04 21:47:18

标签: c# windows-8.1

如何从Windows 8.1 App中的TextBox中删除文本? 每当我点击按钮时,它会在文本框中添加“1”:

private void button1_Click(object sender, RoutedEventArgs e)
        {

            textBox1.Text += 1;
        }

但是当我使用另一个按钮替换/删除textBox中的所有文本时,它不会删除文本:

private void button0_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = "";

        }

我也使用textBox1.Text = string.Empty;但没有任何反应。

它是一个简单的计算器Windows 8.1商店应用程序,完整代码如下:

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;

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

namespace App3
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {


        public MainPage()
        {
            this.InitializeComponent();



        }

        private void button0_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = "";

            //or textBox1.Text = string.Empty; //It also doesn't work!!
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            textBox1.Text += 1;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += 2;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += 3;
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += 4;
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += 5;
        }

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += 6;
        }

        private void button9_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += 7;
        }

        private void button10_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += 8;
        }

        private void button11_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += 9;
        }

        private void button13_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += 0;
        }

        private void button14_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += ".";
        }

        private void button15_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "=";
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "+";
        }

        private void button8_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "-";
        }

        private void button12_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "*";
        }

        private void button16_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "/";
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

    }

}

所以当我点击button0时,它不会清除文本框。

MainPage.xaml中:

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

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Rectangle Fill="#FF707081" HorizontalAlignment="Left" Height="483" Margin="440,59,0,0" Stroke="Black" VerticalAlignment="Top" Width="456"/>
        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="41" Margin="459,135,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="416" FontSize="24" FontFamily="Times New Roman" BorderBrush="#FF1F4BE2" TextChanged="textBox1_TextChanged" Text="Hello World"/>
        <Button x:Name="button2" Content="2" HorizontalAlignment="Left" Height="43" Margin="562,274,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button2_Click"/>
        <Button x:Name="button3" Content="3" HorizontalAlignment="Left" Height="43" Margin="668,274,0,0" VerticalAlignment="Top" Width="107" RenderTransformOrigin="0.495,1.465" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button3_Click"/>
        <Button x:Name="button5" Content="4" HorizontalAlignment="Left" Height="43" Margin="456,340,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button5_Click"/>
        <Button x:Name="button6" Content="5" HorizontalAlignment="Left" Height="43" Margin="562,340,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button6_Click"/>
        <Button x:Name="button7" Content="6" HorizontalAlignment="Left" Height="43" Margin="668,340,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button7_Click"/>
        <Button x:Name="button9" Content="7" HorizontalAlignment="Left" Height="43" Margin="456,410,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button9_Click"/>
        <Button x:Name="button1" Content="1" HorizontalAlignment="Left" Height="43" Margin="456,274,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button1_Click"/>
        <Button x:Name="button10" Content="8" HorizontalAlignment="Left" Height="43" Margin="562,410,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button10_Click"/>
        <Button x:Name="button11" Content="9" HorizontalAlignment="Left" Height="43" Margin="668,410,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button11_Click"/>
        <Button x:Name="button13" Content="0" HorizontalAlignment="Left" Height="43" Margin="456,477,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button13_Click"/>
        <Button x:Name="button14" Content="." HorizontalAlignment="Left" Height="43" Margin="562,477,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button14_Click"/>
        <Button x:Name="button15" Content="=" HorizontalAlignment="Left" Height="43" Margin="668,477,0,0" VerticalAlignment="Top" Width="107" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button15_Click"/>
        <Button x:Name="button0" Content="CLEAR" HorizontalAlignment="Left" Height="46" Margin="459,199,0,0" VerticalAlignment="Top" Width="419" Background="#FF1D1818" BorderBrush="#FF0D22F7"/>
        <Button x:Name="button4" Content="+" HorizontalAlignment="Left" Height="43" Margin="786,274,0,0" VerticalAlignment="Top" Width="92" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button4_Click"/>
        <Button x:Name="button8" Content="-" HorizontalAlignment="Left" Height="43" Margin="786,340,0,0" VerticalAlignment="Top" Width="92" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button8_Click"/>
        <Button x:Name="button12" Content="*" HorizontalAlignment="Left" Height="43" Margin="786,410,0,0" VerticalAlignment="Top" Width="92" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button12_Click"/>
        <Button x:Name="button16" Content="/" HorizontalAlignment="Left" Height="43" Margin="786,477,0,0" VerticalAlignment="Top" Width="92" Background="#FF1D1818" BorderBrush="#FF0D22F7" Click="button16_Click"/>
        <TextBlock HorizontalAlignment="Left" Height="30" Margin="462,76,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="413" FontSize="24" FontFamily="Times New Roman" Foreground="#FF718BE4">
            <Run Text="           "/>
            <Run Foreground="#FF0A0AE0" Text="Hussam's First Calculator"/>
        </TextBlock>
    </Grid>
</Page>

1 个答案:

答案 0 :(得分:1)

看看Xaml。执行清除的方法是孤立,并且没有订阅到该方法Click事件。您需要订阅它:

<Button x:Name="button0" Content="CLEAR" Click="button0_Click"/>

作为大多数操作的一部分,您可以更改它们以调用一个集中式方法,该方法从调用它的按钮中提取必要的文本:

XAML:

<Button Content="1" Click="OperationsClick"/>
<Button Content="2" Click="OperationsClick"/>

代码隐藏

private void OperationsClick(object sender, RoutedEventArgs e)
{
    var btn = sender as Button;

    if (btn != null)
        textbox1.Text += btn.Content.ToString();
}