首先,请安装以下nuget包
https://www.nuget.org/packages/Microsoft.Xaml.Behaviors.WinUI.Managed
然后您可以使用交互并在 xaml 中绑定您的方法(来自视图模型)
<AutoSuggestBox x:Name="Control2" PlaceholderText="Type a control name"
TextChanged="SearchBox_TextChanged" QueryIcon="Find" Width="300" HorizontalAlignment="Left">
<i:Interaction.Behaviors>
<ic:EventTriggerBehavior EventName="QuerySubmitted">
<ic:EventTriggerBehavior.Actions>
<ic:InvokeCommandAction Command="{x:Bind ViewModel.SearchBox_QuerySubmitted}" />
</ic:EventTriggerBehavior.Actions>
</ic:EventTriggerBehavior>
<ic:EventTriggerBehavior EventName="SuggestionChosen">
<ic:EventTriggerBehavior.Actions>
<ic:InvokeCommandAction Command="{x:Bind ViewModel.SearchBox_SuggestionChosen}" />
</ic:EventTriggerBehavior.Actions>
</ic:EventTriggerBehavior>
</i:Interaction.Behaviors>
</AutoSuggestBox>
和csharp:
public void SearchBox_QuerySubmitted(AutoSuggestBoxQuerySubmittedEventArgs args)
{
}
public void SearchBox_SuggestionChosen(AutoSuggestBoxSuggestionChosenEventArgs args)
{
}