How to add Command Behavior in windows store 8.1 MVVM application
Asked Answered
A

2

11

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

Alow answered 5/5, 2014 at 8:19 Comment(1)
So you were able to use the AutoCompleteBox in Windows 8.1 and Windows Phone 8.1? My visual studio won't let me use it in Windows 8.1, because the AutoCompleteBox is defined as targeted towards Windows Phone 8.1. How did you get around that?Barong
A
18

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.

enter image description here

install the Behavior SDK from the list. enter image description here

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

Alow answered 5/5, 2014 at 8:19 Comment(2)
I'm wondering why you would need a command here. You could update the itemsource in the Set method of SearchText. No?Gross
@Kajzer If you are not using MVVM light than, Just add a new View of type (Basic page) in your windows phone 8.1 Rt mobile application. Visual studio 2013 will automatically add RelayCommand in your projectAlow
B
5

The marked answer is certainly correct and it helped me to discover the Behavior SDK; however, the Behavior SDK seems to already be installed natively in VS 2015 CTP, rather than being an extension. Furthermore, for a Universal app to use the Behavior SDK you must:

  1. Right-mouse click your project's References folder and select Add Reference.... The Reference Manager dialog opens.
  2. Select the Windows Phone 8.1 tab or the Windows 8.1 tab on the left, depending upon which type of project you're updating.
  3. Select the Extensions sub-tab.
  4. On the right, check Behavior SDK (XAML).
  5. In a solution for a universal project, the Shared project can make use of the Behavior SDK just like any other project; however, it doesn't have any References folder, so you must simply add the reference to all of the target platform projects instead using the previous steps; e.g., your .Windows and .WindowsPhone projects.

The XAML namespaces that you must define are still the same:

<UserControl ...
             xmlns:i="using:Microsoft.Xaml.Interactivity"
             xmlns:core="using:Microsoft.Xaml.Interactions.Core"
             ...>
Barna answered 3/4, 2015 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.