Home > Enterprise >  Xamarin AutoSuggestBox Selected Item as Command Parameter
Xamarin AutoSuggestBox Selected Item as Command Parameter

Time:01-12

My panelists ask me if I can put an auto-suggestion in my application. After some research, I've decided to use this AutoSugggestionBox and followed this tutorial. However, I am having a hard time displaying the selected suggestion since I am converting it into an MVVM. In timestamp 16:12, he uses the dotMorten.Xamarin.Forms.AutoSuggestBoxQuerySubmittedEventArgs parameter to get the ChoseSuggestion.

My question is, what will I put in the CommandParameter if I use xct:XamarinCommunityTools EventToCommandBehavior

<control:AutoSuggestBox PlaceholderText="Enter" 
                x:Name="AutoSuggestBox">
    <control:AutoSuggestBox.Behaviors>
        <xct:EventToCommandBehavior EventName="QuerySubmitted"
                                    Command="{Binding quesrySubmitComm}"
                                    CommandParameter=""
    </control:AutoSuggestBox.Behaviors>
</control:AutoSuggestBox>

Or is there a more simplified way to MVVMify an AutoSuggestBox? Thank you in advance for your answers.

CodePudding user response:

Use SuggestionChosen for this!

You don't need this command.

Try this.

<control:AutoSuggestBox PlaceholderText="Enter" 
                        x:Name="AutoSuggestBox"
                        SuggestionChosen="SuggestionChosen"/> 

Create the method and get the chosen.

CodePudding user response:

Based on João's suggestions to use SuggestionChosen, I managed to get a work around with the MVVM

This is in my view

<control:AutoSuggestBox x:Name="box">
    <control:AutoSuggestBox.Behaviors>
        <xct:EventToCommandBehavior EventName="TextChanged"
                                    .../>
        <xct:EventToCommandBehavior EventName="SuggestionChosen"
                                    Command="{Binding showComm}"
                                    CommandParameter="{x:Reference box}"/>
   </control:AutoSuggestBox.Behaviors>
</control:AutoSuggestBox>

And this is in my ViewModel

public ICommand showComm { get; }
private async Task show(object sender)
{
    AutoSuggestBox input = (AutoSuggestBox)sender;
    await App.Current.MainPage.DisplayAlert("", input.Text, "OK");
}
  • Related