数据不显示 (C# UWP) [英] Data not displaying (C# UWP)

查看:24
本文介绍了数据不显示 (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 文件:

My xaml file:

  <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 分钟,但应用没有崩溃,数据也没有显示

But I get white screen and not errors. I waiting about 2 minutes, but app didn't crash and data didn't shows

为什么我没有看到数据?

Why I didn't see data?

非常感谢您的帮助!

推荐答案

x:Bind 正在执行一次性绑定,这意味着在加载页面时设置 GridView 的 ItemsSource.它被设置为空值,因为此时您的 Orders 属性仍为空.

The x:Bind is performing a one-time binding meaning that the ItemsSource of your GridView is set when your page is loaded. It is set to a null value since your Orders property is still null at this time.

在您的异步方法中,您正在使用您的数据初始化 Orders 属性,但您从未通知 GridView 该属性已设置.

In your async method, you are initializing the Orders property with your data but you are never notifying the GridView that the property has been set.

根据您尝试实现的目标,您可以使用 Bindings.Update() 请求刷新绑定.这将基本上刷新所有绑定.

Depending on what you try to achieve, you can juste request a refresh of the binding using Bindings.Update(). Which will basically refresh all the bindings.

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

或者您可以使用依赖项属性来存储您的列表并将绑定更改为单向绑定.

Or you can use a dependency property to store your list and change the binding to a one-way binding.

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

这篇关于数据不显示 (C# UWP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆