如何在 windows store 8.1 MVVM 应用程序中添加命令行为 [英] How to add Command Behavior in windows store 8.1 MVVM application

查看:11
本文介绍了如何在 windows store 8.1 MVVM 应用程序中添加命令行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在新的 windows phone 8.1 AutoCompleteBox 控件的 TextChange 事件上调用命令.我正在使用 MVVM Light.

解决方案

在新的 Windows Store 8.1 应用程序中有一个新的 SDK

I want to invoke a command on TextChange event of new windows phone 8.1 AutoCompleteBox Control. I am using MVVM Light.

解决方案

In new windows store 8.1 apps there is a new SDK Behavior SDK for adding behaviors in the application. it is not added by default you have to add this Extension in your project. below is how to add this extension in your project.

install the Behavior SDK from the list.

Now in your XAML page add following namespaces to InvokeActionCommand that is capable of invoking ICommand on ViewModel

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

...

here is the code XAML code for invoking command on textchange event of the autocompletebox.

<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>

Following is my RelayCommand in ViewModel

 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;

                                      }));
        }
    }

Hope this helps for detail see the following link. Windows 8.1 Behavior SDK: How to use InvokeAction

这篇关于如何在 windows store 8.1 MVVM 应用程序中添加命令行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆