【问题标题】:Data binding UWP(C#)数据绑定 UWP(C#)
【发布时间】:2017-01-29 20:33:27
【问题描述】:

我为 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; }

    }

我声明ObservableCollectionpublic ObservableCollection&lt;RootObject&gt; 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>

【问题讨论】:

  • 只是OrdersGridView.ItemsSource = rootObjectData
  • 你在哪里绑定Orders?还是您只在后面的代码中设置 OrdersGridView.ItemsSource ?问题可能是为rootObjectData 中的每个对象创建一个新的Collection 而不是添加新项目? (Orders = new ObservableCollection&lt;RootObject&gt; ...)。
  • 谢谢@AVKNaidu

标签: c# visual-studio data-binding uwp


【解决方案1】:

因为这条线,它只是显示了一个结果:

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;

【讨论】:

  • 问题是我有OrdersGridView.ItemsSource = Orders; 而不是OrdersGridView.ItemsSource = rootObjectData;
猜你喜欢
  • 2020-07-04
  • 1970-01-01
  • 2018-04-13
  • 2021-09-09
  • 1970-01-01
  • 2023-03-30
  • 2017-04-30
  • 2017-05-19
  • 1970-01-01
相关资源
最近更新 更多