【发布时间】:2015-05-13 07:50:12
【问题描述】:
我无法在 Windows Phone 8.1 上的 AutoSuggestBox 中显示结果。我正在使用 MVVM Light 将我的 itemsource 绑定到 Autosuggestbox。
<AutoSuggestBox Header="Van" Text="{Binding SearchTextFrom, Mode=TwoWay}" ItemsSource="{Binding suggestionFrom}">
<AutoSuggestBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding description}"/>
</DataTemplate>
</AutoSuggestBox.ItemTemplate>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="TextChanged">
<core:InvokeCommandAction Command="{Binding SearchChangedFrom}">
</core:InvokeCommandAction>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</AutoSuggestBox>
我的视图模型
private RelayCommand _SearchChangedFrom;
public RelayCommand SearchChangedFrom
{
get
{
return _SearchChangedFrom ?? (_SearchChangedFrom = new RelayCommand(
async () =>
{
if (string.IsNullOrWhiteSpace(user.countrycode))
{
Debug.WriteLine("Could not autocomplete the country because there was no country code provided.");
return;
}
var predictions = await _Service.GetGoogleMapsSuggestionFromQuery(user.countrycode, SearchTextFrom);
suggestionFrom = predictions;
}));
}
}
private List<Prediction> _suggestionFrom;
public List<Prediction> suggestionFrom
{
get { return _suggestionFrom; }
set
{
Set<List<Prediction>>(() => suggestionFrom, ref _suggestionFrom, value);
Debug.WriteLine(suggestionFrom.Count + " were received. Displayong them in the autosuggestbox");
foreach (Prediction prediction in _suggestionFrom)
{
Debug.WriteLine(("Predicition: " + prediction.description));
}
}
}
对象已设置且不为空。
那么他们为什么不出现呢?
更新 我的模型
public class Prediction : ObservableObject
{
private string _description;
public string description
{
get { return _description; }
set{Set<string>(() => description, ref _description, value);}
}
private string _id;
public string id
{
get { return _id; }
set { Set<string>(() => id, ref _id, value); }
}
private List<MatchedSubstring> _matchedSubstrings;
public List<MatchedSubstring> matched_substrings
{
get { return _matchedSubstrings; }
set{Set<List<MatchedSubstring>>(() => matched_substrings, ref _matchedSubstrings, value);}
}
private string _place_id;
public string place_id
{
get { return _place_id; }
set { Set<string>(() => place_id, ref _place_id, value); }
}
private string _reference;
public string reference
{
get { return _reference; }
set { Set<string>(() => reference, ref _reference, value); }
}
private List<Term> _terms;
public List<Term> terms
{
get { return _terms; }
set { Set<List<Term>>(() => terms, ref _terms, value); }
}
private List<string> _types;
public List<string> types
{
get { return _types; }
set { Set<List<String>>(() => types, ref _types, value); }
}
public override string ToString()
{
return this.description;
}
}
【问题讨论】:
-
您是否正在实施 INotifyPropertyChanged?设置
suggestionFrom = predictions;后,您能否检查是否调用了getter? - 放另一个 Debug.WriteLine。 -
我正在实现接口。在 Set 方法之后调用 getter。
-
Prediction 类如何对应于 DataTemplate -
description是有效属性吗?你能展示一下这门课吗?由于调用了 getter,我假设 DataContext 设置正确。 -
我已经添加了我的模型。数据上下文设置正确,因为我收到了在视图中输入的文本。
-
我已经根据您的代码构建了一个示例,它应该可以工作。如果你输入
var predictions = new List<Prediction> { new Prediction {description = "item"}};而不是var predictions = await _Service.GetGoogleMapsSuggestionFromQuery(user.countrycode, SearchTextFrom);,你能检查它是否有效吗?
标签: c# mvvm windows-phone-8.1 mvvm-light autosuggest