文本框点击事件不会在 Windows 8 上触发鼠标单击

本文关键字:单击 鼠标 Windows 事件 文本 | 更新日期: 2023-09-27 18:35:10

我有以下代码为我构建了一个文本框。该应用程序要求我使所有控件通用,因为这是我们的 API 发送给我们的方式。

TextBox txtcontent = new TextBox();
txtcontent.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
txtcontent.SetValue(Grid.ColumnProperty, 1);
txtcontent.SetValue(Grid.RowProperty, RowCount);
txtcontent.Width = 320;
txtcontent.FontSize = 20;
txtcontent.TextWrapping = TextWrapping.Wrap;
txtcontent.Margin = new Thickness(10);
txtcontent.Foreground = App.scbAccTechGrayDark;
txtcontent.BorderBrush = App.scbAccTechGrayLight;
txtcontent.Background = App.scbAccTechGreenLight;
txtcontent.Tapped += txtcontent_Tapped;

现在的问题是,当我单击文本框时,它不会触发点击事件。我了解鼠标单击会触发应用商店应用的点击事件?

这是点击事件的第一部分,但我从未达到它。

void txtcontent_Tapped(object sender, TappedRoutedEventArgs e)
{
    TextBox txtFinder = (sender as TextBox);
    string[] myconver = (sender as TextBox).Tag.ToString().Split(',');
    int BlockIndex = Convert.ToInt16(myconver[4].ToString());
    int FieldID = Convert.ToInt16(myconver[5].ToString());
    string ColumnName = Convert.ToString(myconver[6].ToString());
    string FieldCode = Convert.ToString(myconver[7]);
    string BlockName = Convert.ToString(myconver[8]);
}

谁能告诉我我必须做什么,或者我在这里做错了什么?

文本框点击事件不会在 Windows 8 上触发鼠标单击

你想要使用 TextBox 控件的 GotFocus 事件。

可以使用以下代码将 Tapped 事件关联到 Textbox 使用 AddHandler 方法。

txtcontent.AddHandler(TappedEvent, new TappedEventHandler(txtcontent_Tapped), true);
  void txtcontent_Tapped(object sender, TappedRoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Tested!!");
        }

这是相同的完整代码片段。

public MainPage()
        {
            this.InitializeComponent();
            AddControl();
        }

        private void AddControl()
        {
            TextBox txtcontent = new TextBox();
            txtcontent.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
            txtcontent.SetValue(Grid.ColumnProperty, 1);
            txtcontent.SetValue(Grid.RowProperty, 0);
            txtcontent.Width = 150;
            txtcontent.FontSize = 20;
            txtcontent.TextWrapping = TextWrapping.Wrap;
            txtcontent.Margin = new Thickness(10);
            txtcontent.AddHandler(TappedEvent, new TappedEventHandler(txtcontent_Tapped), true);
            //txtcontent.Foreground = new Solid
            //txtcontent.BorderBrush = App.scbAccTechGrayLight;
            //txtcontent.Background = App.scbAccTechGreenLight;
            this.LayoutRoot.Children.Add(txtcontent);
        }

        void txtcontent_Tapped(object sender, TappedRoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Tested by WmDev!!");
        }

希望对您有所帮助。

谢谢。

    private void txtbox_GotFocus(object sender, RoutedEventArgs e)
    {
        txtbox.Text = ""; // da e34n el txtbox yb2a empty when clicked ;)
    }

试试这个;)