gpt4 book ai didi

mvvm - 如何在 Windows Store 8.1 MVVM 应用程序中添加命令行为

转载 作者:行者123 更新时间:2023-12-04 01:37:58 25 4
gpt4 key购买 nike

我想在新的 Windows Phone 8.1 AutoCompleteBox 控件的 TextChange 事件上调用命令。我正在使用 MVVM Light。

最佳答案

在新的 Windows Store 8.1 应用程序中有一个新的 SDK Behavior SDK用于在应用程序中添加行为。默认情况下不添加它,您必须在项目中添加此扩展。下面是如何在你的项目中添加这个扩展。

enter image description here

从列表中安装行为 SDK。
enter image description here

现在,在您的 XAML 页面中,将以下命名空间添加到能够在 ViewModel 上调用 ICommand 的 InvokeActionCommand

  xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
DataContext="{Binding AutoSuggestionBoxExample, Mode=OneWay, Source={StaticResource Locator}}"

...

这是用于在自动完成框的 textchange 事件上调用命令的代码 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 Behavior SDK: How to use InvokeAction

关于mvvm - 如何在 Windows Store 8.1 MVVM 应用程序中添加命令行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23467911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com