【问题标题】:Autosuggestbox not displaying results WP8.1 Universal AppAutosuggestbox 不显示结果 WP8.1 通用应用程序
【发布时间】:2015-02-04 22:30:41
【问题描述】:

自动建议框中的结果模板未显示,我不知道为什么。我在应用程序中使用 MVVM Light。让我们来看看代码:)

在页面中:

<AutoSuggestBox x:Name="txtBusqueda" x:Uid="txtBusqueda" 
        ItemsSource="{Binding Lugares}"
        Grid.Row="1" Margin="24 24 24 12" 
        MaxSuggestionListHeight="4" AutoMaximizeSuggestionArea="True" 
        TextMemberPath="Nombre"
    >
    <i:Interaction.Behaviors>
            <core:EventTriggerBehavior EventName="TextChanged">
                <core:InvokeCommandAction Command="{Binding ChangeTextCommand}" CommandParameter="{Binding Text, ElementName=txtBusqueda}" />
            </core:EventTriggerBehavior>
        </i:Interaction.Behaviors>
        <AutoSuggestBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Nombre}" TextWrapping="Wrap" />
            </DataTemplate>
        </AutoSuggestBox.ItemTemplate>
    </AutoSuggestBox>

在虚拟机中:

private List<LugarInfo> lugares;
public List<LugarInfo> Lugares
{
    get { return this.lugares; }
    set
    {
        if (this.lugares == value) return;
        this.lugares = value;
        RaisePropertyChanged(() => Lugares);
    }
}
public RelayCommand<string> ChangeTextCommand { get; set; }
this.ChangeTextCommand = new RelayCommand<string>(async (s) =>
{
    var result = await Api.GetInfoLugares(texto);
    List<LugarInfo> lugares = new List<LugarInfo>();
    foreach (var lugar in result)
    {
            lugares.Add(new LugarInfo()
            {
                Id = int.Parse(lugar.Id),
                Tipo = lugar.Tipo,
                Nombre = lugar.Nombre
            });
    }

    this.Lugares = lugares;
});

所以,如您所见,没有太多麻烦。 结果被返回并分配给属性:

问题是结果没有显示在控件中,如您在图像中看到的那样。 我们将不胜感激。

【问题讨论】:

  • 你添加了那个红色轮廓吗?这通常表明存在绑定问题。
  • @Paul Abbott 当我在虚拟机中设置属性时出现底部红线...

标签: windows-phone-8.1 mvvm-light autosuggest


【解决方案1】:

您的 Lugares 属性永远不会引发 RaisePropertyChanged,因为在您的代码中,您将对象添加到 lugares 中,而不是将其分配给 Setter 中相同的 This.Lugares,因此它不会引发属性更改并且 UI 不会更新。将您的代码更改为如下。

var myLugares = new List<LugarInfo>();
foreach (var lugar in result)
{
        myLugares.Add(new LugarInfo()
        {
            Id = int.Parse(lugar.Id),
            Tipo = lugar.Tipo,
            Nombre = lugar.Nombre
        });
}

this.Lugares = myLugares;

【讨论】:

  • 我会在几个小时后回家试试这个并给你反馈,谢谢。
  • 几分钟前我已经更改并测试了代码,不幸的是结果没有出现在控件中:(现在属性更改正在提高,但正如我所说......没有运气跨度>
  • 两天后,我意识到这一点:MaxSuggestionListHeight="4"....真可惜...对不起各位,感谢您的帮助。它现在正在工作......
猜你喜欢
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-17
  • 2023-03-27
  • 1970-01-01
  • 2012-12-14
  • 2017-12-08
相关资源
最近更新 更多