我希望在新的Windows Phone 8.1自动完成框控件的TextChange事件中调用一个命令。我正在使用MVVM Light框架。
我希望在新的Windows Phone 8.1自动完成框控件的TextChange事件中调用一个命令。我正在使用MVVM Light框架。
在新的Windows 8.1应用商店中,有一个新的SDK 行为SDK,可以在应用程序中添加行为。它不会默认添加到项目中,您需要将此扩展添加到项目中。以下是如何将此扩展添加到您的项目中:
从列表中安装行为SDK。
现在,在您的XAML页面中,向 InvokeActionCommand 添加以下命名空间,以便调用 ViewModel 上的 ICommand。
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
DataContext="{Binding AutoSuggestionBoxExample, Mode=OneWay, Source={StaticResource Locator}}"
以下是在自动完成框的文本更改事件上调用命令的XAML代码。
<AutoSuggestBox Text="{Binding SearchText,Mode=TwoWay}" ItemsSource="{Binding
Suggesstions}">
<AutoSuggestBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</AutoSuggestBox.ItemTemplate>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="TextChanged">
<core:InvokeCommandAction Command="{Binding SearchChanged}">
</core:InvokeCommandAction>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</AutoSuggestBox>
以下是我的ViewModel中的RelayCommand:
private RelayCommand _searchChanged;
/// <summary>
/// Gets the SearchChanged.
/// </summary>
public RelayCommand SearchChanged
{
get
{
return _searchChanged
?? (_searchChanged = new RelayCommand(
() =>
{
IList<string> sugg = new List<string>();
for (int i = 0; i < 25; i++)
{
sugg.Add(SearchText + " 1" + i);
sugg.Add(SearchText + " 2" + i);
}
Suggesstions = sugg;
}));
}
}
希望这可以帮到您。有关详细信息,请参见以下链接。Windows 8.1 行为 SDK:如何使用 InvokeAction
您必须定义的 XAML 命名空间仍然是相同的:
<UserControl ...
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
...>