2016-08-25 50 views
1

尽量使数据绑定数据绑定(C#UWP)

写入该类

using System; 
using System.Collections.Generic; 
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() 
     { 



      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(); 


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

      return order; 
     } 


    } 

这是其中数据需要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" /> 
 
       <TextBlock FontSize="16" Text="{x:Bind Name }" HorizontalAlignment="Center" /> 
 
       <TextBlock FontSize="10" Text="{x:Bind Adress}" HorizontalAlignment="Center" /> 
 
     </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>

还有xaml.cs文件

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(); 


     } 

     private void GridView_ItemClick(object sender, ItemClickEventArgs e) 
     { 


     } 
    } 
} 

我编译时有此错误:

Error

我怎么能解决这个错误。

关于它在说什么索引?

感谢您的帮助。

+0

您是否调试过该应用程序?你应该能够在调试器的帮助下回答你的问题。 – Reddy

+0

是的。我调试。你可以看到调试器在屏幕截图上显示的错误@Reddy –

回答

2

从下面的代码删除命令[0],命令[1],...。这应该可以解决你的问题。

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

您正试图在集合中没有数据时从订单访问第0个元素。

+0

我写命令[0] .date_created,没有看到这个错误。但我的屏幕是白色的。我如何使它变成黑色? –

+0

当您的数据源中没有数据时,您如何看待屏幕显示为黑色?相应地更改您的XAML。使用系统的 – Reddy

+0

; using System.Collections.Generic;使用System.Linq的 ;使用System.Threading的 ; 公共类节目 { \t公共类订单 { 公共字符串日期{获得;组; } public string Name {get;组; } public string Adress {get;组; } } \t \t 公共静态无效的主要() \t { \t \t变种顺序=新列表(); order.Add(new Orders {Date =“”+ order [0] .Date}); order.Add(new Orders {Date =“”}); \t} } 这是无效的。 – Reddy