locked
Search output to a textbox with suggestion.. can't get it to work properly RRS feed

  • Question

  • I've a textbox.

    <TextBox   Name="outbox" Width="300" Height="20" Text="{Binding constantvalue}"/>

    In the .cs file, I've got the following to suggest in search charms.

    public sealed partial class mainconstantspage : constants.Common.LayoutAwarePage
        {
            private SearchPane searchPane;
    
            private static readonly string[] suggestionList =
                {
                    
                    "Absolute Zero", );        
    
                  }
    
    
    public mainconstantspage()
            {
                this.InitializeComponent();
                searchPane = SearchPane.GetForCurrentView();
                
            }
    
    
            private void OnSearchPaneSuggestionsRequested(SearchPane sender, SearchPaneSuggestionsRequestedEventArgs e)
            {
    
                
    
                var queryText = e.QueryText;
    
                var request = e.Request;
               
    
                foreach (string suggestion in suggestionList)
                {
    
    
                    if (suggestion.StartsWith(queryText, StringComparison.CurrentCultureIgnoreCase))
                    {
                        // Add suggestion to Search Pane
                        request.SearchSuggestionCollection.AppendQuerySuggestion(suggestion);
    
    
    
                    }
                }
    
    
    
    
    
    
            }
    
    
    
    protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
            {
                var queryText = navigationParameter as String;
    
                            
    
                if (queryText == "Absolute Zero") this.DefaultViewModel["constantvalue"] = "-273.15 °C";
    
    .................
    
    }
    
    protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                
                searchPane.SuggestionsRequested += new TypedEventHandler<SearchPane, SearchPaneSuggestionsRequestedEventArgs>(OnSearchPaneSuggestionsRequested);
            }
    
            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                searchPane.SuggestionsRequested -= new TypedEventHandler<SearchPane, SearchPaneSuggestionsRequestedEventArgs>(OnSearchPaneSuggestionsRequested);
            }


     

    If I leave it as is, the suggestion works perfectly, but every time I press enter there's no result in the textbox.  But if I comment out the OnNavigateTo block, then the suggestion does not work but the output in the textbox works perfectly. 


    How do I make it so both will work?

     

     

    Monday, August 12, 2013 8:55 PM

Answers

  • You should work with the QuerySubmitted event handler instead of LoadState.

    Here is your Navigation Handlers:

    protected override async void OnNavigatedTo(NavigationEventArgs e)
            {
    
                SearchPane.GetForCurrentView().SuggestionsRequested += new TypedEventHandler<SearchPane, SearchPaneSuggestionsRequestedEventArgs>(OnSearchPaneSuggestionsRequested);
                SearchPane.GetForCurrentView().QuerySubmitted+=OnQuerySubmitted;
                SearchPane.GetForCurrentView().ShowOnKeyboardInput = true;
                await LoadTrialModeProxyFileAsync();
            }
    
    protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                SearchPane.GetForCurrentView().SuggestionsRequested -= new TypedEventHandler<SearchPane, SearchPaneSuggestionsRequestedEventArgs>(OnSearchPaneSuggestionsRequested);
                SearchPane.GetForCurrentView().ShowOnKeyboardInput = false;
                SearchPane.GetForCurrentView().QuerySubmitted -= OnQuerySubmitted;
                if (licenseChangeHandler != null)
                {
                    CurrentAppSimulator.LicenseInformation.LicenseChanged -= licenseChangeHandler;
                }
            }

    Here is your QuerySubmitted event handler:

    private void OnQuerySubmitted(SearchPane sender, SearchPaneQuerySubmittedEventArgs args)
            {
                var queryText = args.QueryText;
    
                // Communicate results through the view model
    
                if (queryText == "Absolute Zero") outbox.Text = "-273.15 °C";
            }



    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by RandyPete Tuesday, August 13, 2013 3:21 PM
    Tuesday, August 13, 2013 3:15 PM

All replies

  • Please help anyone?
    Tuesday, August 13, 2013 5:01 AM
  • but every time I press enter there's no result in the textbox.
    Do you mean the query text entered in search charm is cleared off and you want to persist that?

    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, August 13, 2013 10:16 AM
  • Hi huge ,

    Welcome to MSDN forum!

    While you press enter, the event  SearchPane.QuerySubmitted | querysubmitted event is fired.

    So, you could change the OutText data here instead. Also you could do it like below MSDN sample.

     private void OnSearchPaneSuggestionsRequested(SearchPane sender, SearchPaneSuggestionsRequestedEventArgs e)
            {
                var queryText = e.QueryText;
                if (string.IsNullOrEmpty(queryText))
                {
                    MainPage.Current.NotifyUser("Use the search pane to submit a query", NotifyType.StatusMessage);
                }
                else
                {
                    var request = e.Request;
                    foreach (string suggestion in suggestionList)
                    {
                        if (suggestion.StartsWith(queryText, StringComparison.CurrentCultureIgnoreCase))
                        {
                            // Add suggestion to Search Pane
                            request.SearchSuggestionCollection.AppendQuerySuggestion(suggestion);
    
                            // Break since the Search Pane can show at most 5 suggestions
                            if (request.SearchSuggestionCollection.Size >= MainPage.SearchPaneMaxSuggestions)
                            {
                                break;
                            }
                        }
                    }
    
                    if (request.SearchSuggestionCollection.Size > 0)
                    {
                        MainPage.Current.NotifyUser("Suggestions provided for query: " + queryText, NotifyType.StatusMessage);
                    }
                    else
                    {
                        MainPage.Current.NotifyUser("No suggestions provided for query: " + queryText, NotifyType.StatusMessage);
                    }
                }
            }

    And the NotifyUser function defined like this:

     public void NotifyUser(string strMessage, NotifyType type)
            {
                switch (type)
                {
                    // Use the status message style.
                    case NotifyType.StatusMessage:
                        StatusBlock.Style = Resources["StatusStyle"] as Style;
                        break;
                    // Use the error message style.
                    case NotifyType.ErrorMessage:
                        StatusBlock.Style = Resources["ErrorStyle"] as Style;
                        break;
                }
                StatusBlock.Text = strMessage;
    
                // Collapse the StatusBlock if it has no text to conserve real estate.
                if (StatusBlock.Text != String.Empty)
                {
                    StatusBlock.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }
                else
                {
                    StatusBlock.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                }
            }
    Best Regard!


    Tuesday, August 13, 2013 11:24 AM
    Moderator
  • Forgive me.  You misunderstood what I meant.

    I have a textbox in the xaml page. 

     <TextBox   Name="outbox"  Text="{Binding constantvalue}" Style="{StaticResource inout}" Margin="0,30,0,0"/>

    In the .cs page, I have the following code to do suggestion.

    private void OnSearchPaneSuggestionsRequested(SearchPane sender, SearchPaneSuggestionsRequestedEventArgs e)
            {
    
                
    
                var queryText = e.QueryText;
    
                var request = e.Request;
               
    
                foreach (string suggestion in suggestionList)
                {
    
    
                    if (suggestion.StartsWith(queryText, StringComparison.CurrentCultureIgnoreCase))
                    {
                        // Add suggestion to Search Pane
                        request.SearchSuggestionCollection.AppendQuerySuggestion(suggestion);
    
                    }
                }
    
            }

    It's pretty much what you suggested edited for my purposes.

    Here are some codes that goes back to the outbox in the xaml page.

    protected override  void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
            {
                
                var queryText = navigationParameter as String;
    
                // Communicate results through the view model
    
                if (queryText == "Absolute Zero") outbox.Text = "-273.15 °C";
    
    ......................

    So, with everything above, when I type in "Absolute Zero" and press enter, in the textbox "outbox" appears "-273.15 °C".  But the suggestion doesn't work.  In other words, when I start typing Absolute Zero, it doesn't suggest it.

    In order for suggestion to work, I have to have the following codes.

    protected override async void OnNavigatedTo(NavigationEventArgs e)
            {
                
                searchPane.SuggestionsRequested += new TypedEventHandler<SearchPane, SearchPaneSuggestionsRequestedEventArgs>(OnSearchPaneSuggestionsRequested);
                SearchPane.GetForCurrentView().ShowOnKeyboardInput = true;
                await LoadTrialModeProxyFileAsync();
            }
    
            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                searchPane.SuggestionsRequested -= new TypedEventHandler<SearchPane, SearchPaneSuggestionsRequestedEventArgs>(OnSearchPaneSuggestionsRequested);
                SearchPane.GetForCurrentView().ShowOnKeyboardInput = false;
    
                if (licenseChangeHandler != null)
                {
                    CurrentAppSimulator.LicenseInformation.LicenseChanged -= licenseChangeHandler;
                }
                
    
    
            }

    With the codes right above, suggestion works perfectly.  But when I press enter or choose the suggestion nothing happens in the textbox "outbox" in the xaml page. 

    Tuesday, August 13, 2013 2:11 PM
  • but every time I press enter there's no result in the textbox.
    Do you mean the query text entered in search charm is cleared off and you want to persist that?

    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    No.  There is a textbox in the xaml page called "outbox".

    <TextBox   Name="outbox"  Text="{Binding constantvalue}" Style="{StaticResource inout}" Margin="0,30,0,0"/>

    The following code will output a number when I do a search.

    protected override  void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
            {
                
                var queryText = navigationParameter as String;
    
                // Communicate results through the view model
    
                if (queryText == "Absolute Zero") outbox.Text = "-273.15 °C";
    
    .....................

    Just having those 2 blocks of codes and the appropriate code in the App.xaml.cs

    protected async override void OnSearchActivated(Windows.ApplicationModel.Activation.SearchActivatedEventArgs args)
            {
                // TODO: Register the Windows.ApplicationModel.Search.SearchPane.GetForCurrentView().QuerySubmitted
                // event in OnWindowCreated to speed up searches once the application is already running
    
                // If the Window isn't already using Frame navigation, insert our own Frame
                var previousContent = Window.Current.Content;
                var frame = previousContent as Frame;
    
                // If the app does not contain a top-level frame, it is possible that this 
                // is the initial launch of the app. Typically this method and OnLaunched 
                // in App.xaml.cs can call a common method.
                if (frame == null)
                {
                    // Create a Frame to act as the navigation context and associate it with
                    // a SuspensionManager key
                    frame = new Frame();
                    constants.Common.SuspensionManager.RegisterFrame(frame, "AppFrame");
    
                    if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
                    {
                        // Restore the saved session state only when appropriate
                        try
                        {
                            await constants.Common.SuspensionManager.RestoreAsync();
                        }
                        catch (constants.Common.SuspensionManagerException)
                        {
                            //Something went wrong restoring state.
                            //Assume there is no state and continue
                        }
                    }
                }
    
                frame.Navigate(typeof(mainconstantspage), args.QueryText);
    
                
                
                Window.Current.Content = frame;
    
                // Ensure the current window is active
                Window.Current.Activate();
            }

    As you can see, I have the search result pass the QueryText to the loadstate in mainconstantspage.  So, when I have just the above codes, everytime I type in "Absolute Zero" in the textbox 'outbox' should appear the number "-273.15 °C". 

    But no search suggestion.  So, the following code blocks are for search suggestion.  But when I put it in when I press enter nothing happens in the textbox "outbox".  Suggestion works, though.

    private void OnSearchPaneSuggestionsRequested(SearchPane sender, SearchPaneSuggestionsRequestedEventArgs e)
            {
    
                
    
                var queryText = e.QueryText;
    
                var request = e.Request;
               
    
                foreach (string suggestion in suggestionList)
                {
    
    
                    if (suggestion.StartsWith(queryText, StringComparison.CurrentCultureIgnoreCase))
                    {
                        // Add suggestion to Search Pane
                        request.SearchSuggestionCollection.AppendQuerySuggestion(suggestion);
    
                    }
                }
    
            }
    
    
    protected override async void OnNavigatedTo(NavigationEventArgs e)
            {
                
                searchPane.SuggestionsRequested += new TypedEventHandler<SearchPane, SearchPaneSuggestionsRequestedEventArgs>(OnSearchPaneSuggestionsRequested);
                SearchPane.GetForCurrentView().ShowOnKeyboardInput = true;
                await LoadTrialModeProxyFileAsync();
            }
    
            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                searchPane.SuggestionsRequested -= new TypedEventHandler<SearchPane, SearchPaneSuggestionsRequestedEventArgs>(OnSearchPaneSuggestionsRequested);
                SearchPane.GetForCurrentView().ShowOnKeyboardInput = false;
    
                if (licenseChangeHandler != null)
                {
                    CurrentAppSimulator.LicenseInformation.LicenseChanged -= licenseChangeHandler;
                }
                
    
    
            }
    
    

    If I take out OnNavigateTo and OnNavigateFrom, then suggestion does not work but output to the textbox "outbox" works.  How do I have both?

    I've tried to explain in 2 different ways.  Hopefully, you will understand me this time. 

    Tuesday, August 13, 2013 2:21 PM
  • You should work with the QuerySubmitted event handler instead of LoadState.

    Here is your Navigation Handlers:

    protected override async void OnNavigatedTo(NavigationEventArgs e)
            {
    
                SearchPane.GetForCurrentView().SuggestionsRequested += new TypedEventHandler<SearchPane, SearchPaneSuggestionsRequestedEventArgs>(OnSearchPaneSuggestionsRequested);
                SearchPane.GetForCurrentView().QuerySubmitted+=OnQuerySubmitted;
                SearchPane.GetForCurrentView().ShowOnKeyboardInput = true;
                await LoadTrialModeProxyFileAsync();
            }
    
    protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                SearchPane.GetForCurrentView().SuggestionsRequested -= new TypedEventHandler<SearchPane, SearchPaneSuggestionsRequestedEventArgs>(OnSearchPaneSuggestionsRequested);
                SearchPane.GetForCurrentView().ShowOnKeyboardInput = false;
                SearchPane.GetForCurrentView().QuerySubmitted -= OnQuerySubmitted;
                if (licenseChangeHandler != null)
                {
                    CurrentAppSimulator.LicenseInformation.LicenseChanged -= licenseChangeHandler;
                }
            }

    Here is your QuerySubmitted event handler:

    private void OnQuerySubmitted(SearchPane sender, SearchPaneQuerySubmittedEventArgs args)
            {
                var queryText = args.QueryText;
    
                // Communicate results through the view model
    
                if (queryText == "Absolute Zero") outbox.Text = "-273.15 °C";
            }



    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by RandyPete Tuesday, August 13, 2013 3:21 PM
    Tuesday, August 13, 2013 3:15 PM
  • That worked perfectly.  Thank you.
    Tuesday, August 13, 2013 3:22 PM