UWP绑定到MVVM中的AutoSuggestBox

kay*_*cee 6 binding mvvm autosuggest uwp

我在UWP中调用AutoSuggestBox控件的QuerySubmitted命令.该命令在视图模型中绑定到ICommand.问题是需要接受AutoSuggestBoxQuerySubmittedEventArgs这是纯UI,并且在MVVM中是不可接受的.

我的代码看起来像这样:

<AutoSuggestBox Name="SearchAutoSuggestBox"
                PlaceholderText="Search by keywords"
                QueryIcon="Find"
                >
    <interactivity:Interaction.Behaviors>
        <core:EventTriggerBehavior EventName="QuerySubmitted">
            <core:InvokeCommandAction Command="{x:Bind ViewModel.SearchCommand}" />
        </core:EventTriggerBehavior>
    </interactivity:Interaction.Behaviors>
</AutoSuggestBox>
Run Code Online (Sandbox Code Playgroud)

我的视图模型看起来像这样:

public DelegateCommand<AutoSuggestBoxQuerySubmittedEventArgs> SearchCommand { get; }

public MainPageViewModel()
{
    SearchCommand = new DelegateCommand<AutoSuggestBoxQuerySubmittedEventArgs>(ExecuteMethod);
}

private void ExecuteMethod(AutoSuggestBoxQuerySubmittedEventArgs o)
{
    // CODE HERE
}
Run Code Online (Sandbox Code Playgroud)

ofcours AutoSuggestBoxQuerySubmittedEventArgs在视图模型中是不可接受的.寻找替代品......同样适用于SuggestionChosen ......

Nic*_*ick 9

InvokeCommandAction有一个名为InputConverter的参数,您可以使用该参数将事件参数转换为可传递给ViewModel的其他参数.

首先创建一个IValueConverter类,从这样的事件args中提取你需要的东西: -

public class AutoSuggestQueryParameterConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
       {
          // cast value to whatever EventArgs class you are expecting here
          var args = (AutoSuggestBoxQuerySubmittedEventArgs)value;
          // return what you need from the args
          return (string)args.ChosenSuggestion;
       }
}
Run Code Online (Sandbox Code Playgroud)

然后在您的XAML中使用该转换器,如下所示:

<Page.Resources>
     <converters:AutoSuggestQueryParameterConverter x:Key="ArgsConverter" />
</Page.Resources>

<AutoSuggestBox Name="SearchAutoSuggestBox"
            PlaceholderText="Search by keywords"
            QueryIcon="Find">
    <interactivity:Interaction.Behaviors>
      <core:EventTriggerBehavior EventName="QuerySubmitted">
        <core:InvokeCommandAction 
              Command="{x:Bind ViewModel.SearchCommand}"
              InputConverter="{StaticResource ArgsConverter}" />
      </core:EventTriggerBehavior>
    </interactivity:Interaction.Behaviors>
</AutoSuggestBox>
Run Code Online (Sandbox Code Playgroud)

最后在viewmodel中更改命令以接受字符串作为参数.所以你的vm中会有以下内容:

public DelegateCommand<string> SearchCommand { get; }

public MainPageViewModel()
{
    SearchCommand = new DelegateCommand<string>(ExecuteMethod);
}

private void ExecuteMethod(string o)
{
    // CODE HERE
}
Run Code Online (Sandbox Code Playgroud)


CFr*_*tas 5

您可以将搜索字符串(Text属性)绑定到视图模型属性,将事件绑定到无参数方法.这样,视图模型就不必处理UI对象:

XAML:

<AutoSuggestBox Header="What's your name?"
                TextChanged="{x:Bind ViewModel.FilterUsuals}"
                QuerySubmitted="{x:Bind ViewModel.ProcessQuery}"
                SuggestionChosen="{x:Bind ViewModel.ProcessChoice}"
                ItemsSource="{x:Bind ViewModel.Usuals, Mode=OneWay}"
                Text="{x:Bind ViewModel.SearchText, Mode=TwoWay}"
                QueryIcon="Find" />
Run Code Online (Sandbox Code Playgroud)

代码背后:

public class MainPageViewModel : SomeViewModelBaseClass
{
    /* Boilerplate code and constructor not included */

    private string _SearchText;
    public string SearchText {/* getter and setter INotyfyPropertyChange compliant */ }

    private List<string> _Usuals; // Initialized on constructor
    public string Usuals {/* getter and setter INotyfyPropertyChange compliant */ }


    public void FilterUsuals()
    {
        // the search string is in SearchText Example:
        Usuals = _UsualsStore.Where(u => u.Contains(_SearchText.ToLower())).ToList();
    }

    public void ProcessQuery() { /* TODO - search string is in SearchText */ }

    public void ProcessChoice() { /* TODO - search string is in SearchText */ }
}
Run Code Online (Sandbox Code Playgroud)