首页 \ 问答 \ 同时使用GotFocus和TextChanged - Windows Phone(Using GotFocus and TextChanged simultaneously - Windows Phone)

同时使用GotFocus和TextChanged - Windows Phone(Using GotFocus and TextChanged simultaneously - Windows Phone)

我有一个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用于显示在其中写入文本的建议。

请建议我任何方式来做同样的事情。

提前致谢 :)


I have an AutoSuggestBox which is set to handle the events GotFocus & TextChanged simultaneously. I have cleared the text from the text box in GotFocus event. Now the problem is that when I select any of the suggestions in AutoSuggestBox, after selecting it calls the GotFocus event handler and clears the selected text from it.

This is the MainPage.xaml code using the AutoSuggestBox:

        <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"/>

And this one is the code which I have written in 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;
    }

I want to use both of the event handlers. GotFocus for clearing the data of Text box, and TextChanged for showing the suggestions of writing the text in it.

Please suggest me any way to do the same.

Thanks in Advance :)


原文:https://stackoverflow.com/questions/37227699
更新时间:2019-07-12 07:09

最满意答案

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

设置私有字段:

private bool _isSelectingSuggestion;

将像OnSuggestionChosen这样的方法处理程序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;
}

显然这只有在GotFocus之前引发SuggestionChosen时才有效:当GotFocus开始时,它会像:“好吧,因为刚才刚刚选择了一个建议而得到了关注?如果是真的,我一定不能清除我的文本!否则,我会清楚的!“

让我知道这项工作适合你!


If AutoSuggestBox has an event to handle the selection of a suggested word, like "SuggestionChosen", a possibile solution is using a private flag managed between the various handlers.

Set a private field:

private bool _isSelectingSuggestion;

Link a method-handler like OnSuggestionChosen to the event SuggestionChosen and implement it like that:

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

Then, in GotFocus, check the flag like this:

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

   _isSelectingSuggestion = false;
}

Obviously this works only if SuggestionChosen is raised before GotFocus: when GotFocus begins, it proceeds like: "ok, I've got focus because a suggestion was chosen just a moment ago? If it's true, I must not clear my Text! Otherwise, I shall clear it!".

Let me know it this work for you!

2016-05-14

相关问答

更多

Windows Phone 8中的自动完成位置选择框(AutoComplete Location Selection Box in Windows Phone 8)

可能:是的 高效:没有 Windows Phone sdk中没有自动完成位置选择框控件但是,可以通过创建文本框并订阅其TextChanged事件并将服务调用ReverseGeoCode来调用事件中文本框中提供的文本来模仿。 像这样的东西 private void tbxPlayerTwoName_TextChanged(object sender, TextChangedEventArgs e) { //Start reverseGeoCoding } 但如 ...

在Windows Phone 7.5上集成支付(integrate payments on Windows Phone 7.5)

这不是被禁止的。 WP7应用程序没有本地应用程序内购买,因此您可以使用任何其他方式。 另外,如果您的应用程序在WP8设备上运行,则可以使用Reflection访问InAP库 http://channel9.msdn.com/Shows/Inside+Windows+Phone/Inside-Windows-Phone-45--Adding-In-App-Purchase-as-a-light-up-feature-to-your-Windows -电话-7游戏 Windows Phone的应用程序 ...

Windows Phone 8.1 - 页面导航(Windows Phone 8.1 - Page Navigation)

在Windows Phone 8.1中,页面导航方法是这样的: Frame.Navigate(typeof(SecondPage), param); 这意味着你将导航到“SecondPage”,并通过“param”(一个基于对象的类)。 如果您不需要传递任何参数,可以使用: Frame.Navigate(typeof(SecondPage)); 您可以找到此MSDN链接的文档 In Windows Phone 8.1, Page Navigation method is like this: ...

将事件GotFocus分配给WPF-Aplication中的所有文本框(Assigning event GotFocus to all TextBoxes in WPF-Aplication)

这个问题,因为我看到你正在使用一个Winforms application与一个Wpf application ,在Winforms中,你的控件可以添加到主窗体,因此this.Controls工作,在Wpf中你的MainWindow有一个Content属性其中只能包含一个项目,通常是某种Grid , Canvas或Panel 。 这个对象是你的TextBox's位置。 注意:只有当你的TextBox是MainWindows LayoutControl的子类时,这个方法才有效,如果嵌入更深,你就不需 ...

“Windows Phone HTML5 App”和“Javascript Windows Store Project”之间的区别(Difference between “Windows Phone HTML5 App” and “Javascript Windows Store Project”)

Windows Phone HTML5应用程序项目仅在Windows Phone上运行,是使用托管HTML的WebBrowser控件的应用程序。 没有来自底层操作系统的本机支持,只有浏览器显示HTML页面。 您可以将JavaScript方法绑定到C#,可以对其进行调试。 Windows应用商店项目仅在Windows 8上作为“Metro”或“Modern UI”应用运行。 JavaScript托管在WinRT运行时中,并与操作系统的许多部分集成。 这两个模板不可互换。 The Windows Ph ...

Windows Phone:Google Maps api用于获取餐馆(Windows Phone: Google Maps api for fetching restaurants)

我建议你使用Here Maps API,因为我在我的一个应用程序中实现了。 在您的情况下,它非常容易使用Here Maps。 首先浏览Windows Phone API - HereMaps ,它将帮助您继续使用Here Maps API的基本知识。 正如您所说,首先将您的API集成到您的应用程序中。 只需一个术语“ 附近的餐馆 ”将其传递到Here Maps中的搜索词,就可以使用Here Drive获取您在此地图中的所有附近餐馆以及路线导航。 它使用起来非常简单,易于操作。 希望它能帮到你。 I ...

我们可以在Windows Phone 8的XAML中编写或调用“C代码”(Can we write or call “C code” in XAML for Windows Phone 8)

Windows Phone不支持P / Invoke,所以不支持。 你不能。 特别是如果您希望您的应用程序获得App Store批准: 4.2.2 对于Windows Phone OS 7.1,以Windows Phone OS 7.1为目标的应用程序不得使用PInvoke或COM互操作性来调用本机代码API。 对于Windows Phone 8,以Windows Phone 8为目标的应用程序不得调用除Win32和Windows API for Windows Phone 8中列出的API之外的 ...

保持Windows服务和Windows Phone应用程序之间的连接(Keep connection between Windows service and Windows Phone app alive)

您可以使用地理定位服务,并将您的应用程序保留在后台甚至是假音频代理。 您可以自由选择,因为公司应用程序没有公开认证流程。 以下是有关公司枢纽和Windows Phone 8应用程序的企业部署的一些信息。 You can use geolocation service and keep your app in background or even fake audio agent. You are free with choice because company apps have no publi ...

相关文章

更多

最新问答

更多
  • Android宽度:100%修复(网站接管问题)(Android width:100% fix (website takeover issue))
  • C ++函数/方法设计的良好实践(Good practice in C++ function/method design)
  • 计算其他表中不存在的所有记录 - SQL查询(Count all records that does not exist to other table - SQL Query)
  • 为什么我要用JPA共享ID?(Why do I get shared Ids with JPA?)
  • asp.net - 如何显示来自html格式的数据行的字段(asp.net - how to display a field from data row that is in html format)
  • 我们如何使用ActiveRecord从连接表中删除行?(How can we delete rows from a join table by using ActiveRecord?)
  • ng-class搞乱了类的顺序(ng-class messing with the order of classes)
  • oracle 12g无效数字错误(oracle 12g invalid number error)
  • 更改ng-src值onclick(Change ng-src value onclick)
  • 如何在android中自动添加自定义依赖项以创建新项目?(How to add custom dependencies automatically in android for ever a new project is created?)
  • datetime函数在PHP中(datetime function in php)
  • 在javascript中获取会话数组的值(in javascript get the value of a session array)
  • 如何在UTF8中编译LaTeX?(How can I compile LaTeX in UTF8? [closed])
  • Rspec:“array.should == another_array”,但不用担心订单(Rspec: “array.should == another_array” but without concern for order)
  • Logcat错误:无法在android片段中加载视图(Logcat error: unable to load view in android fragments)
  • JavaFX的。(JavaFX. Adding items to the list in different threads is not working)
  • 从GDATA日历资源迁移到Google Calendar Resource api(Migrate from GDATA calendar resource to Google Calendar Resource api)
  • SSRS 2008 - 以零情景处理分割(SSRS 2008 - Dealing with division by zero scenarios)
  • 我如何以编程方式添加一个listView列标题的点击事件(How can I add a listView column header a click event programmatically)
  • Wxpython:无法检索有关列表控件项XXX的信息(Wxpython: Couldn't retrieve information about list control item XXX)
  • 使用Tortoise SVN在SVN存储库中移动目录(Move Directory across SVN repository using Tortoise SVN)
  • 天蓝色服务结构集群中的web api无状态服务是否在一段时间不活动后进入休眠状态?(Do web api stateless services in azure service fabric cluster go to sleep after a period of inactivity?)
  • 我可以设置intelliJ来突出显示PHP编码风格吗?(Can I set intelliJ to highlight php coding style?)
  • 用javafx创建一个Truetype字体文件(Creating a Truetype Font file with javafx)
  • Spring ftp配置错误(Spring ftp configuration is wrong)
  • 使用gsub去除多个字符(Using gsub to strip multiple characters)
  • 续订推送证书并保持当前的App Store App正常工作(Renew Push certificate and keep current App Store App working)
  • js:ES5和ES6之间关于'this'关键字用法的一个令人困惑的观点(js: one confusing point about 'this' keyword usage between ES5 and ES6)
  • window.onload vs $(document).ready()(window.onload vs $(document).ready())
  • 在Swift中,如何声明一个符合一个或多个协议的特定类型的变量?(In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?)