2016-08-26 35 views
0

我創造了這樣的名單:數據不顯示(C#UWP)

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using WooCommerceNET; 
using WooCommerceNET.WooCommerce; 

namespace xBindDataMilano.Models 
{ 

    public class Orders 
    { 
     public string Date { get; set; } 
     public string Name { get; set; } 
     public string Adress { get; set; } 


    } 
    public class OrderManager 
    { 

     public static async Task<List<Orders>> GetOrders_1() 
     { 



      RestAPI rest = new RestAPI("http://simplegames.com.ua/wp-json/wc/v1/", "ck_9d64c027d2c5f81b8bed3342eeccc6d337be813d", "cs_60697b1e6cbdeb8d62d19e0765e339f8e3334754"); 
      WCObject wc = new WCObject(rest); 
      //Get all products 
      var orders = await wc.GetOrders(); 

      Debug.WriteLine(" SMOTRET TUT"); 



      var order = new List<Orders>(); 
      order.Add(new Orders { Date = "" + orders[0].date_created, Name = "" + orders[0].billing.first_name, Adress = "" + orders[0].shipping.address_1 + "      " + orders[0].shipping.address_2 }); 
      order.Add(new Orders { Date = "" + orders[1].date_created, Name = "" + orders[1].billing.first_name, Adress = "" + orders[1].shipping.address_1 + "      " + orders[1].shipping.address_2 }); 
      order.Add(new Orders { Date = "" + orders[2].date_created, Name = "" + orders[2].billing.first_name, Adress = "" + orders[2].shipping.address_1 + "      " + orders[2].shipping.address_2 }); 
      order.Add(new Orders { Date = "" + orders[3].date_created, Name = "" + orders[3].billing.first_name, Adress = "" + orders[3].shipping.address_1 + "      " + orders[3].shipping.address_2 }); 
      order.Add(new Orders { Date = "" + orders[4].date_created, Name = "" + orders[4].billing.first_name, Adress = "" + orders[4].shipping.address_1 + "      " + orders[4].shipping.address_2 }); 



      return order; 



     } 




    } 

} 

並嘗試像這樣顯示的:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 
using xBindDataMilano.Models; 



namespace Milano 
{ 
    /// <summary> 
    /// An empty page that can be used on its own or navigated to within a Frame. 
    /// </summary> 
    public sealed partial class Test : Page 
    { 

     private List<Orders> Orders; 
     public Test() 
     { 
      this.InitializeComponent(); 
      Disp(); 
     } 

     public async void Disp() { 

      Orders = await OrderManager.GetOrders_1(); 


     } 

     private void GridView_ItemClick(object sender, ItemClickEventArgs e) 
     { 


     } 
    } 
} 

我的XAML文件:

<Page 
    x:Class="Milano.Test" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:Milano" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:data="using:xBindDataMilano.Models" 
    mc:Ignorable="d"> 


<Page.Resources> 
    <DataTemplate x:DataType="data:Orders" x:Key="BookDataTemplate"> 
     <StackPanel HorizontalAlignment="Center"> 
       <TextBlock FontSize="16" Text="{x:Bind Date}" HorizontalAlignment="Center" Foreground="#FF0C0B0B" /> 
       <TextBlock FontSize="16" Text="{x:Bind Name }" HorizontalAlignment="Center" Foreground="#FF0C0B0B" /> 
       <TextBlock FontSize="10" Text="{x:Bind Adress}" HorizontalAlignment="Center" Foreground="#FF0C0B0B"/> 
     </StackPanel> 
    </DataTemplate> 
</Page.Resources> 
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="20"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="100" /> 
    </Grid.RowDefinitions> 

     <GridView ItemsSource="{x:Bind Orders}" 
        IsItemClickEnabled="True" 
        ItemClick="GridView_ItemClick" 
        ItemTemplate="{StaticResource BookDataTemplate}"> 
     </GridView> 


     <TextBlock Grid.Row="1" 
        Name="ResultTextBlock" 
        FontSize="24" 
        Foreground="Red" 
        FontWeight="Bold" 
        Margin="0,20,0,0" /> 

</Grid> 
</Page> 

但我得到白色的屏幕,而不是錯誤。 我等了大約2分鐘,但應用沒有崩潰,數據也沒有顯示

爲什麼我沒有看到數據?

非常感謝的幫助!

回答

1

x:Bind正在執行一次綁定,意味着您的頁面加載時GridView的ItemsSource被設置。它被設置爲空值,因爲此時您的訂單屬性仍爲空。

在你的異步方法中,你正在用你的數據初始化Orders屬性,但你永遠不會通知GridView屬性已經設置。

根據您嘗試實現的內容,您可以使用Bindings.Update()來請求刷新綁定。這將基本上刷新所有的綁定。

public async void Disp() 
{ 
    Orders = await OrderManager.GetOrders_1(); 
    Bindings.Update(); 
} 

或者您可以使用依賴項屬性來存儲您的列表並將綁定更改爲單向綁定。

public static readonly DependencyProperty OrdersProperty = DependencyProperty.Register(nameof(Orders), typeof(List<Order>), typeof(Test), new PropertyMetadata(null)); 

public List<Order>Orders 
{ 
    get { return (List<Order>) GetValue(OrdersProperty); } 
    set { SetValue(OrdersProperty, value); } 
} 
+0

它有幫助,感謝的 –

+0

非常感謝! –