【问题标题】:Using GotFocus and TextChanged simultaneously - Windows Phone同时使用 GotFocus 和 TextChanged - Windows Phone
【发布时间】:2016-09-10 16:41:13
【问题描述】:

我有一个AutoSuggestBox,它设置为同时处理 GotFocusTextChanged 事件。我已经清除了 GotFocus 事件中文本框中的文本。现在的问题是,当我在AutoSuggestBox 中选择任何建议时,在选择它后会调用 GotFocus 事件处理程序并从中清除选定的文本。

这是使用 AutoSuggestBox 的 MainPage.xaml 代码:

        <AutoSuggestBox
            x:Name="auto_text_from"
            HorizontalAlignment="Left"
            VerticalAlignment="Center"
            PlaceholderText="Enter Source"
            Height="auto"
            Width="280"
            GotFocus="auto_text_from_GotFocus"
            TextChanged="AutoSuggestBox_TextChanged"/>

这是我写在MainPage.xaml.cs的代码:

    private void auto_text_from_GotFocus(object sender, RoutedEventArgs e)
    {
        auto_text_from.Text = "";
    }


    string[] PreviouslyDefinedStringArray = new string[] {"Alwar","Ajmer","Bharatpur","Bhilwara",
        "Banswada","Jaipur","Jodhpur","Kota","Udaipur"};


    private void AutoSuggestBox_TextChanged(AutoSuggestBox sender,AutoSuggestBoxTextChangedEventArgs args)
    {
        List<string> myList = new List<string>();
        foreach (string myString in PreviouslyDefinedStringArray)
        {
            if (myString.ToLower().Contains(sender.Text.ToLower()) == true)
            {
                myList.Add(myString);
            }
        }
        sender.ItemsSource = myList;
    }

我想同时使用这两个事件处理程序。 GotFocus 用于清除文本框的数据,TextChanged 用于显示在其中写入文本的建议。

请建议我做同样的事情。

在此先感谢 :)

【问题讨论】:

    标签: c# visual-studio xaml event-handling windows-phone


    【解决方案1】:

    如果AutoSuggestBox 有一个事件来处理对建议词的选择,例如“SuggestionChosen”,则可能的解决方案是使用在各个处理程序之间管理的私有标志。

    设置私有字段:

    private bool _isSelectingSuggestion;
    

    OnSuggestionChosen 之类的方法处理程序链接到事件SuggestionChosen 并像这样实现它:

    private void OnSuggestionChosen(object sender, RoutedEventArgs e)
    {
        _isSelectingSuggestion = true;
    }
    

    然后,在 GotFocus 中,像这样检查标志:

    private void auto_text_from_GotFocus(object sender, RoutedEventArgs e)
    {
        if (_isSelectingSuggestion)
            e.Handled = true;
        else
            auto_text_from.Text = "";
    
       _isSelectingSuggestion = false;
    }
    

    显然,这只有在SuggestionChosenGotFocus 之前提出时才有效:当GotFocus 开始时,它会继续:“好的,我已经获得焦点,因为刚才选择了一个建议?如果是真的,我不能清除我的文本!否则,我将清除它!”。

    让我知道这对你有用!

    【讨论】:

      【解决方案2】:

      @MK87:是的,它只需要一点点改变! :)

          private bool _isSelectingSuggestion;
      
          private void OnSuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
          {
              _isSelectingSuggestion = true;
          }
      
          private void auto_text_from_GotFocus(object sender, RoutedEventArgs e)
          {
              if (!_isSelectingSuggestion)
                  auto_text_from.Text = "";
      
              _isSelectingSuggestion = false;
          }
      

      我不得不删除这一行:

      e.Handled == true;
      

      因为它给了我RoutedEventArgs does not contain a definition for 'Handled'的错误。

      感谢您的帮助 :) :)

      【讨论】:

      • ... RoutedEventArgs 类提供了一个公共属性 bool Handled,用于设置事件是否已经被完全管理,或者是否必须转发到下一个事件处理程序。
      猜你喜欢
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多