未调用 UWP EventTriggerBehaviour 命令
UWP EventTriggerBehaviour command not called
我目前正在开发 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>
在后面的 ViewModel 中,我这样定义我的命令:
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)
{
...
}
SearchResults 的定义如下:
private ObservableCollection<ShowModel> _searchResults = default(ObservableCollection<ShowModel>);
public ObservableCollection<ShowModel> SearchResults { get { return _searchResults; } set { Set(ref _searchResults, value); } }
我的问题是 'TextChanged' 事件运行正常。只要事件触发,就会调用该命令。但是,SuggestionChosen 事件从不触发命令。如果我将 SuggestionChosen 事件直接附加到控件,它将触发,但我希望调用该命令。这两个事件的代码或多或少是相同的,所以我似乎无法弄清楚为什么调用一个而不调用另一个。
我自己设法解决了这个问题。问题出在 SuggestionChosen 事件绑定上。绑定未返回命令期望的值类型 (ShowModel)。
我所做的是将命令类型更改为AutoSuggestBoxSuggestionChosenEventArgs,并且没有在XAML中传递任何命令参数。该命令是使用参数作为参数调用的,我什至可以从参数访问所选项目。
我目前正在开发 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>
在后面的 ViewModel 中,我这样定义我的命令:
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)
{
...
}
SearchResults 的定义如下:
private ObservableCollection<ShowModel> _searchResults = default(ObservableCollection<ShowModel>);
public ObservableCollection<ShowModel> SearchResults { get { return _searchResults; } set { Set(ref _searchResults, value); } }
我的问题是 'TextChanged' 事件运行正常。只要事件触发,就会调用该命令。但是,SuggestionChosen 事件从不触发命令。如果我将 SuggestionChosen 事件直接附加到控件,它将触发,但我希望调用该命令。这两个事件的代码或多或少是相同的,所以我似乎无法弄清楚为什么调用一个而不调用另一个。
我自己设法解决了这个问题。问题出在 SuggestionChosen 事件绑定上。绑定未返回命令期望的值类型 (ShowModel)。
我所做的是将命令类型更改为AutoSuggestBoxSuggestionChosenEventArgs,并且没有在XAML中传递任何命令参数。该命令是使用参数作为参数调用的,我什至可以从参数访问所选项目。