C# 未调用UWP EventTriggerBehavior命令

C# 未调用UWP EventTriggerBehavior命令,c#,xaml,mvvm,icommand,uwp,C#,Xaml,Mvvm,Icommand,Uwp,我目前正在开发一个UWP应用程序 我有一个AutoSuggestBox控件,我想使用命令处理它的一些事件(因为我遵循MVVM模式)。为此,我引用了Microsoft.Xaml.Interactivity(来自Blend) 我使用的代码如下所示: <AutoSuggestBox x:Name="autoSuggestBox" Width="256" Horizont

我目前正在开发一个UWP应用程序

我有一个AutoSuggestBox控件,我想使用命令处理它的一些事件(因为我遵循MVVM模式)。为此,我引用了Microsoft.Xaml.Interactivity(来自Blend)

我使用的代码如下所示:

        <AutoSuggestBox x:Name="autoSuggestBox"
                            Width="256"
                            HorizontalAlignment="Right"
                            ItemsSource="{Binding SearchResults}"
                            MaxSuggestionListHeight="256"
                            QueryIcon="Find">
                <i:Interaction.Behaviors>
                    <core:EventTriggerBehavior EventName="SuggestionChosen">
                        <core:InvokeCommandAction Command="{Binding SuggestionChosenCommand}" CommandParameter="{Binding ElementName=autoSuggestBox}" />
                    </core:EventTriggerBehavior>
                    <core:EventTriggerBehavior EventName="TextChanged">
                        <core:InvokeCommandAction Command="{Binding ChangeSearchResultsCommand}" CommandParameter="{Binding Text, ElementName=autoSuggestBox}" />
                    </core:EventTriggerBehavior>
                </i:Interaction.Behaviors>
                <AutoSuggestBox.ItemTemplate>
                    ...
                </AutoSuggestBox.ItemTemplate>
            </AutoSuggestBox>
    Command<string> _changeSearchResultsCommand = default(Command<string>);
    public Command<string> ChangeSearchResultsCommand { get { return _changeSearchResultsCommand ?? (_changeSearchResultsCommand = new Command<string>(async (param) => { await ExecuteChangeSearchResultsCommand(param); }, CanExecuteChangeSearchResultsCommand)); } }
    private bool CanExecuteChangeSearchResultsCommand(string p) {  return !IsSearchBusy; }
    private async Task ExecuteChangeSearchResultsCommand(string text)
    {
        ...
    }

    Command<ShowModel> _suggestionChosenCommand = default(Command<ShowModel>);
    public Command<ShowModel> SuggestionChosenCommand { get { return _suggestionChosenCommand ?? (_suggestionChosenCommand = new Command<ShowModel>(async (param) => { await ExecuteSuggestionChosenCommand(param); }, CanExecuteSuggestionChosenCommand)); } }
    private bool CanExecuteSuggestionChosenCommand(ShowModel p) { return true; }
    public async Task ExecuteSuggestionChosenCommand(ShowModel show)
    {
        ...
    }

...
在后面的ViewModel中,我定义了如下命令:

        <AutoSuggestBox x:Name="autoSuggestBox"
                            Width="256"
                            HorizontalAlignment="Right"
                            ItemsSource="{Binding SearchResults}"
                            MaxSuggestionListHeight="256"
                            QueryIcon="Find">
                <i:Interaction.Behaviors>
                    <core:EventTriggerBehavior EventName="SuggestionChosen">
                        <core:InvokeCommandAction Command="{Binding SuggestionChosenCommand}" CommandParameter="{Binding ElementName=autoSuggestBox}" />
                    </core:EventTriggerBehavior>
                    <core:EventTriggerBehavior EventName="TextChanged">
                        <core:InvokeCommandAction Command="{Binding ChangeSearchResultsCommand}" CommandParameter="{Binding Text, ElementName=autoSuggestBox}" />
                    </core:EventTriggerBehavior>
                </i:Interaction.Behaviors>
                <AutoSuggestBox.ItemTemplate>
                    ...
                </AutoSuggestBox.ItemTemplate>
            </AutoSuggestBox>
    Command<string> _changeSearchResultsCommand = default(Command<string>);
    public Command<string> ChangeSearchResultsCommand { get { return _changeSearchResultsCommand ?? (_changeSearchResultsCommand = new Command<string>(async (param) => { await ExecuteChangeSearchResultsCommand(param); }, CanExecuteChangeSearchResultsCommand)); } }
    private bool CanExecuteChangeSearchResultsCommand(string p) {  return !IsSearchBusy; }
    private async Task ExecuteChangeSearchResultsCommand(string text)
    {
        ...
    }

    Command<ShowModel> _suggestionChosenCommand = default(Command<ShowModel>);
    public Command<ShowModel> SuggestionChosenCommand { get { return _suggestionChosenCommand ?? (_suggestionChosenCommand = new Command<ShowModel>(async (param) => { await ExecuteSuggestionChosenCommand(param); }, CanExecuteSuggestionChosenCommand)); } }
    private bool CanExecuteSuggestionChosenCommand(ShowModel p) { return true; }
    public async Task ExecuteSuggestionChosenCommand(ShowModel show)
    {
        ...
    }
Command\u changesearchresultcommand=default(命令);
公共命令changesearchresultcommand{get{return}changesearchresultcommand??(\u changesearchresultcommand=new命令(async(param)=>{await executechangesearchresultcommand(param);},canexecutechangesearchresultcommand));}
私有布尔CanExecuteChangeSearchResultCommand(字符串p){return!IsSearchBusy;}
专用异步任务ExecuteChangeSearchResultsCommand(字符串文本)
{
...
}
命令_suggestionChosenCommand=默认值(命令);
公共命令SuggestionChosenCommand{get{return}SuggestionChosenCommand??(\u SuggestionChosenCommand=newcommand(async(param)=>{await ExecuteSuggestionChosenCommand(param);},CanExecuteSuggestionChosenCommand));}
私有bool CanExecuteSuggestionChosenCommand(ShowModel p){return true;}
公共异步任务执行SuggestionChosenCommand(ShowModel show)
{
...
}
搜索结果的定义如下:

    private ObservableCollection<ShowModel> _searchResults = default(ObservableCollection<ShowModel>);
    public ObservableCollection<ShowModel> SearchResults { get { return _searchResults; } set { Set(ref _searchResults, value); } }
private ObservableCollection\u searchResults=默认值(ObservableCollection);
公共observeCollection SearchResults{get{return}SearchResults;}set{set(ref}SearchResults,value);}

我的问题是“TextChanged”事件工作正常。只要事件触发,就会调用该命令。但是,SuggestionSelected事件从不触发命令。如果我将SuggestionSelected事件直接附加到控件,它将触发,但我希望调用该命令。这两个事件的代码大致相同,因此我似乎无法理解为什么调用一个而不调用另一个。

我自己设法解决了这个问题。问题在于SuggestionSelected事件绑定。绑定未返回命令所期望的值类型(ShowModel)


我所做的是将命令类型更改为AutoSuggestBoxSuggestionChosenEventArgs,并且没有在XAML中传递任何命令参数。该命令是以参数形式调用的,我甚至可以从参数中访问所选项。

您可以在双向模式下在ViewModel中绑定查询字符串属性,而不是在TextChanged事件中调用命令,并且在查询属性的setter中更新Suggestions ObservableCollection。这将是一种更好的MVVM方法。