首页 \ 问答 \ 同时使用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 Mango与Windows Phone 7(Windows Phone Mango vs Windows Phone 7)

Mango确实是Windows Phone 7.5更新的代号。 Windows Phone 7.5中有许多新的API和功能,它带来了一个新的SDK(版本7.1)。 针对Windows Phone 7.0 SDK的应用在7.1下工作正常,但针对Windows Phone 7.1 SDK的应用需要运行Mango更新。 Windows Phone站点列出了Windows Phone 7.5(Mango更新)中的新功能, MSDN列出了7.0 SDK和7.1 SDK之间的API更改。 Mango was

WPF窗口GotFocus / LostFocus(WPF Window GotFocus / LostFocus)

尝试使用“激活/禁用”事件。 GotFocus / LostFocus旨在仅用于控件(例如,当您单击窗口内的控件时它会触发)。 Try using the Activated/Deactivated event. GotFocus/LostFocus is intended to be used only with controls (e.g. it fires when you click on controls inside the window).

我可以使用Windows 7在VS 2015上为Windows Phone 8编码吗?(Can I code for Windows Phone 8 on VS 2015 with Windows 7?)

Windows 7不支持Windows Phone 8 SDK。因此,您将无法开发Windows Phone 8应用程序。 Windows Phone 8 SDK is not supported by Windows 7. Therefore, you will not be able to develop windows phone 8 applications.

为Windows Phone 8构建C ++ 11?(Building C++11 for Windows Phone 8?)

Windows Phone SDK 8使用的是Visual Studio 2012 C ++编译器。 Windows Phone SDK 8.1将使用Visual Studio 2013 C ++编译器,预计将于2014年4月2日在Microsoft BUILD会议期间发布。 Windows Phone SDK 8 is using Visual Studio 2012 C++ compiler. Windows Phone SDK 8.1 will be using Visual Studio

动画在Windows Phone 8.1中不起作用(Animation not working in Windows Phone 8.1)

正如MSDN教程中所述,动画仅适用于Windows Phone 8.1 Silverlight应用程序。 http://msdn.microsoft.com/en-us/library/windows/apps/jj206955(v=vs.105).aspx#BKMK_Startingstoppingpausingandresuming 适用于:Windows Phone 8和Windows Phone Silverlight 8.1 | Windows Phone OS 7.1 它不适用于Wi

要在Windows Phone 8中使用的数据库(Database to be used in Windows Phone 8)

您可以使用广泛用于Android和IOS的SQLite数据库。 看似与这篇帖子linq to sql(Windows Phone的本地数据库)相似更好? (或)一个sqlite-net版本的移动更好? 用于Windows Phone 8本地数据库创建 You can Probably use SQLite database which is widely used for Android and IOS also. Seems similar with this post linq to sql

在Windows 7上将Windows Phone 8.1部署/构建到设备(而不是模拟器)(Deploy/Build Windows Phone 8.1 to device (not emulator) on Windows 7)

当然,但您应该使用开发人员解锁设备来部署您的应用程序。 要解锁设备,您可以使用SDK附带的Windows Phone开发人员注册工具。 Sure, but you should have developer unlocked device to deploy your application. To unlock your device, you can use Windows Phone Developer Registration tool coming with SDK.

如何在Windows Phone 7中序列化对象(How to serialize an object in windows phone 7)

Windows Phone不支持JavaScriptSerializer 。 另一种方法是使用JSON.NET (您可以通过NuGet添加它)。 代码将如下所示: string stringData = JsonConvert.SerializeObject(object); JavaScriptSerializer is not supported on Windows Phone. An alternative is to use JSON.NET (you can add it via

相关文章

更多

最新问答

更多
  • 使用javascript为用户延迟加载内容的更好方法是什么?(What is a better way to lazy load content for users with javascript?)
  • 如何修复无法将类型的值...转换为预期的参数类型inout _(How to fix Cannot convert value of type … to expected argument type inout _)
  • 从哪里开始解析JSON?(Where to start with parsing JSON?)
  • Mail.php不适用于.htaccess(Mail.php doesn't work with .htaccess)
  • 使用htaccess设置子域(Setting subdomains using htaccess)
  • mod_pagespeed和java应用服务器(mod_pagespeed and java app servers)
  • Win32:如何崩溃?(Win32: How to crash?)
  • FFmpeg中RGB到YUV转换的错误(Error in RGB to YUV conversion in FFmpeg)
  • 如何在Android中首次打开应用时创建弹出窗口?(How to create pop-up when first open app in Android?)
  • 通过动态传递表名和列名来反转COLUMN DATA(Reversing COLUMN DATA by dynamically passing table name and Column Name)
  • 如何计算C#中ICollection中有多少元素?(How can I count how many elements are in an ICollection in C#)
  • 从pg-promise查询中获得结果(get result out of a pg-promise query)
  • 使用管理器注册一个类,然后调用子类重写方法(Register a class with a manager, then call the sub classes overridden methods)
  • 传递&在查询字符串中(Passing & in query string)
  • 谷歌浏览器打印预览不会第一次加载页面(google chrome print preview does not load the page the first time)
  • FsLexYacc:Tests / MiniProject“在.fsproj中导入错误”(FsLexYacc : Tests/MiniProject “incorrect Import in .fsproj”)
  • 创建一个模型方法,计算用户的帖子并在rspec(Rails)中测试?(Create a model method that counts a user's posts and test in rspec (Rails)?)
  • 记录线程的奇怪行为(Strange behaviour of logging thread)
  • 移动数组中的索引(Moving indices in an array)
  • MVC 5 - > MVC 5.1迁移。(MVC 5 -> MVC 5.1 Migration. Intellisense issues)
  • 使用函数在z3中创建列表(Creating List in z3 using function)
  • 如何进行html随机重定向(How to make a html random redirect)
  • 角度js:使用角度过滤器分割日期,月份和时间(Angular js: spliting date,month and time using angular filter)
  • 将所有模块导入一个文件夹,并按其属性之一使用它们(Import all modules in one folder and use them by one of its attributes)
  • 只需要在git的当前分支上获得合并列表(Just get list of merge happened on my current branch in git)
  • React-Native导入库并在多个组件中可用(React-Native import a library and make available in multiple components)
  • 如何使用jQuery为固定(偏移)位置设置div动画?(How to animate div for a fixed (offset) position using jQuery?)
  • operator ==和vector confusion(operator== and vector confusion)
  • 如何获取所选图像映射alt值?(How to get selected image map alt value?)
  • 在角度,如何使用取消用户事件的美元间隔,如页面更改?(In angular, how to use cancel an $interval on user events, like page change?)