在Windows Phone 8中异步调用后,Listpicker未绑定

时间:2014-04-04 10:13:17

标签: windows-phone-8

如果我使用异步方法,那么Listpicker没有绑定正确的items.it绑定项目名称列表。如果我在构造函数中使用相同的代码,它可以正常工作。

我试过这段代码:

C#:

List<Orderlist> GetOrderItems = new List<Orderlist>();
protected async override void OnNavigatedTo(NavigationEventArgs e)
    {

            var resultOrderReq = await this.objOrderMgr.GetOrders(objOrderReq, this.objRequestHeaderHelper.GetRequestHeaders());
            var reslistOrder = resultOrderReq.orderlist;
            foreach (var item in reslistOrder)
            {
                GetOrderItems.Add(new Orderlist() { OrderId = item.orderid });
            }
            this.ProductSub.ItemsSource = GetOrderItems;
        }
    }
    public class Orderlist
    {
        public long OrderId { get; set; }

    }

XAML

<toolkit:ListPicker Grid.Row="0" x:Name="ProductSub" ItemTemplate="{StaticResource PickerItemTemplate}" ExpansionMode="ExpansionAllowed" FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}"/>

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Name="PickerItemTemplate">
        <TextBlock Text="{Binding OrderId}"></TextBlock>
    </DataTemplate>
    <DataTemplate x:Name="PickerFullModeItemTemplate">
        <TextBlock Text="{Binding OrderId}"></TextBlock>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

2 个答案:

答案 0 :(得分:1)

通常,这样的问题归结为两件事之一:

  • 您忘了为更改的属性实施INotifyPropertyChanged
  • 您使用的是不可观察的集合,例如List<T>而不是ObservableCollection<T>

我根据您的问题描述无法确定,但看起来它可能是可观察的收集问题。尝试将List<Orderlist>替换为ObservableCollection<Orderlist>

答案 1 :(得分:0)

//我对你的代码做了一点改动。这可以帮到你。

List<Orderlist> GetOrderItems = new List<Orderlist>();
protected async override void OnNavigatedTo(NavigationEventArgs e)
    {   
       if (e.NavigationMode != NavigationMode.Back)
          {
             var resultOrderReq = await this.objOrderMgr.GetOrders(objOrderReq, this.objRequestHeaderHelper.GetRequestHeaders());
             var reslistOrder = resultOrderReq.orderlist;
             foreach (var item in reslistOrder)
               {
                  GetOrderItems.Add(new { OrderId = item.orderid });
               }
            this.ProductSub.ItemsSource = GetOrderItems;
           }
      }



 public class Orderlist
    {
        public long OrderId { get; set; }
    }