带Template10的AutoSuggestBox不提示任何内容

时间:2017-11-30 17:18:43

标签: c# mvvm uwp template10

我目前正在学习C#和XAML编程以及使用MVVM。我查看了Microsoft的官方AutoSuggestBox示例。我查看了代码并尝试使用模板10实现相同的功能,但没有运气。没有提出任何建议。

MainPage.XML

<AutoSuggestBox RelativePanel.Below="stateTextBox"
                x:Name="asb"
                PlaceholderText="Type a name (e.g. John)"
                DisplayMemberPath="DisplayName"
                TextMemberPath="DisplayName"
                QueryIcon="Find"
                Margin="0,24,0,24"
                MinWidth="296"
                HorizontalAlignment="Left"
                TextChanged="{x:Bind ViewModel.FilterUsuals}"
                QuerySubmitted="{x:Bind ViewModel.ProcessQuery}"
                SuggestionChosen="{x:Bind ViewModel.ProcessChoice}"
                ItemsSource="{Binding Elements}"
                />

MainPageViewModel.cs

Contact _Contact = default(Contact);
public Contact Contact { get { return _Contact; } set { Set(ref _Contact, value); } }

public void FilterUsuals(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
    // We only want to get results when it was a user typing,
    // otherwise we assume the value got filled in by TextMemberPath
    // or the handler for SuggestionChosen
    if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
    {
        var matchingContacts = ContactSampleDataSource.GetMatchingContacts(sender.Text);

        sender.ItemsSource = matchingContacts.ToList();
    }
}

public void ProcessQuery(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
    if (args.ChosenSuggestion != null)
    {
    }
    else
    {
    }
}

public void ProcessChoice(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
    var contact = (Contact)args.SelectedItem;
}

1 个答案:

答案 0 :(得分:0)

我建议在页面的代码隐藏中处理UI事件,以便将UI与视图模型分开。

这是一个例子,删除了一些附加内容以保持简单:

MainPage.xaml中:

<AutoSuggestBox x:Name="FruitsSuggestion"
                ItemsSource="{x:Bind ViewModel.FruitSuggestions, Mode=OneWay}"                    PlaceholderText="Search"
                QueryIcon="Find"
                QuerySubmitted="FruitsSuggestion_QuerySubmitted"
                Text="{x:Bind ViewModel.Query, Mode=TwoWay}" />

MainPage.xaml.cs中:

private void FruitsSuggestion_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
    ViewModel.Filter();
}

MainPageViewModel.cs:

private string _query = default(string);
public string Query { get => _query; set => Set(ref _query, value); }

private List<string> _allFruits = new List<string>
{
    "Apple",
    "Banana",
    "Orange",
    "Plum",
    "Peach",
    "Pineapple"
};
private ObservableCollection<string> _fruitSuggestions = new ObservableCollection<string>();
public ObservableCollection<string> FruitSuggestions => _fruitSuggestions;

public void Filter()
{
    FruitSuggestions.Clear();
    FruitSuggestions.AddRange(from fruit in _allFruits
                              where fruit.Contains(Query)
                              select fruit);
}

当您运行Filter方法时,它会更新ObservableCollectionAutoSuggestBox会更新file(GLOB_RECURSE parts chapters/*.tex) message(STATUS "Remove absolute part of the paths") set(rel_parts) # List of relative paths foreach (_parts ${parts}) # Iterate over list of absolute paths # Obtain relative path into local variable file(RELATIVE_PATH _rel_parts "/home/user/latex_project/main" ${_parts}) # Add relative path into the list list(APPEND rel_parts ${_rel_parts}) endforeach() # Now variable 'rel_parts' contains list of relative paths 中的建议项目。