数据绑定UWP(c#)

本文关键字:UWP 数据绑定 | 更新日期: 2023-09-27 18:08:02

我正在为UWP编写应用程序。

我尝试使用数据绑定。

我有类

 public class Billing
    {
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string company { get; set; }
        public string address_1 { get; set; }
        public string address_2 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string postcode { get; set; }
        public string country { get; set; }
        public string email { get; set; }
        public string phone { get; set; }

    }
    public class Shipping
    {
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string company { get; set; }
        public string address_1 { get; set; }
        public string address_2 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string postcode { get; set; }
        public string country { get; set; }
    }
    public class RootObject
    {
        public int id { get; set; }
        public int parent_id { get; set; }
        public string status { get; set; }
        public string order_key { get; set; }
        public string currency { get; set; }
        public string version { get; set; }
        public bool prices_include_tax { get; set; }
        public string date_created { get; set; }
        public string date_modified { get; set; }
        public int customer_id { get; set; }
        public double discount_total { get; set; }
        public double discount_tax { get; set; }
        public double shipping_total { get; set; }
        public double shipping_tax { get; set; }
        public double cart_tax { get; set; }
        public double total { get; set; }
        public double total_tax { get; set; }
        public Billing billing { get; set; }
        public Shipping shipping { get; set; }
        public string payment_method { get; set; }
        public string payment_method_title { get; set; }
        public string transaction_id { get; set; }
        public string customer_ip_address { get; set; }
        public string customer_user_agent { get; set; }
        public string created_via { get; set; }
        public string customer_note { get; set; }
        public string date_completed { get; set; }
        public string date_paid { get; set; }
        public string cart_hash { get; set; }
        public List<object> line_items { get; set; }
        public List<object> tax_lines { get; set; }
        public List<object> shipping_lines { get; set; }
        public List<object> fee_lines { get; set; }
        public List<object> coupon_lines { get; set; }
    }

I declare ObservableCollection, public ObservableCollection<RootObject> Orders { get; set; }

我得到这样的JSON:
  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(new Dictionary<string, string>() {
            { "per_page", "100" }});


        string products = orders.ToFormattedJsonString();
        List<RootObject> rootObjectData = JsonConvert.DeserializeObject<List<RootObject>>(products);

尝试像这样绑定数据

  foreach (RootObject root in rootObjectData)
        {

            string date = root.date_created;
            //string name = root.billing.first_name + root.billing.last_name;

            Debug.WriteLine(date.ToString());

           Orders = new ObservableCollection<RootObject> { new RootObject { date_created = date },
            new RootObject { date_created = date }
            };

但是我在屏幕上只看到一个日期。在调试控制台中大约有28个日期。

如何使所有日期可见?

我的xaml:

<Page
x:Class="Milano.InWork"
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"
mc:Ignorable="d">
<Grid BorderBrush="White" BorderThickness="1">
    <Grid.Background>
        <ImageBrush Stretch="Fill" ImageSource="Images/Background.png"/>
    </Grid.Background>
    <Grid HorizontalAlignment="Left" Height="720" VerticalAlignment="Top" Width="60" BorderBrush="#FFF5F1F1" BorderThickness="0,0,1,0">
        <Button x:Name="MenuButton" Content="" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="38" Width="38">
            <Button.Background>
                <ImageBrush Stretch="Uniform" ImageSource="Images/Menu-100.png"/>
            </Button.Background>
        </Button>
        <Button x:Name="logoutbutton" Content="" HorizontalAlignment="Left" Margin="10,650,0,0" VerticalAlignment="Top"  Height="43" Width="38">
            <Button.Background>
                <ImageBrush Stretch="Uniform" ImageSource="Images/Logout_Button.png"/>
            </Button.Background>
        </Button>
    </Grid>
    <Grid HorizontalAlignment="Left" Height="47" Margin="63,2,-121,0" VerticalAlignment="Top" Width="1338" BorderBrush="#FFFDFDFD" Padding="0,0,0,1" BorderThickness="0,0,0,1">
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="В Работе" VerticalAlignment="Top" Height="37" Width="1218" FontSize="32" FontFamily="SF UI Display" Padding="550,0,0,0" Foreground="White"/>
    </Grid>
    <ScrollViewer HorizontalAlignment="Left" Height="668" Margin="63,52,0,0" VerticalAlignment="Top" Width="350">
        <GridView   x:Name="OrdersGridView" >
            <GridView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding date_created}" />

                    </StackPanel>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>
    </ScrollViewer>

</Grid>

数据绑定UWP(c#)

它只是显示一个结果,因为这一行:

Orders = new ObservableCollection<RootObject> { new RootObject { date_created = date },
            new RootObject { date_created = date }
            };

你刚刚添加了两个RootObject到你的集合。

如果你想添加和rootObjectData一样多的日期,你应该这样做:

ObservableCollection<RootObject> aux = new ObservableCollection<RootObject>();
foreach (RootObject root in rootObjectData)
        {

            string date = root.date_created;
            //string name = root.billing.first_name + root.billing.last_name;

            Debug.WriteLine(date.ToString());

           aux.Add(root);
}
Orders = aux;