GridView没有在uwp中显示记录

时间:2017-03-09 20:57:04

标签: c# wpf uwp

我正在开发一个uwp应用。我不是在这种情况下关注MVVM。我正在从url获取json数据并使用Newtonsoft解析它并希望将其绑定到DataGrid控件。问题是数据网格根本没有出现。在页面加载时,我使用Dispatcher填充collectionviewsource并绑定到GridView。但没有帮助。请帮忙。

XAML:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0" Margin="20">
            <TextBlock Text="Search:"></TextBlock>
            <TextBox Height="30" Width="140" Margin="20,0,0,0"></TextBox>
        </StackPanel>

        <GridView Grid.Row="1" ItemsSource="{Binding viewSource.View}" Height="500" SelectionMode="Single" Width="Auto">
            <GridView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding recordID}"></TextBlock>
                        <TextBlock Text="{Binding allergy_name}"></TextBlock>
                        <TextBlock Text="{Binding reaction}"></TextBlock>
                        <TextBlock Text="{Binding allergy_date}"></TextBlock>
                        <TextBlock Text="{Binding severity}"></TextBlock>
                        <TextBlock Text="{Binding status}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>
</Grid>

这是我的XAML.cs

TwoFactor factor;
public CollectionViewSource viewSource { get; set; }
        private List<AllergyRecord> _allergyrecords;
        public List<AllergyRecord> AllAllergies
        {
            get
            {
                return _allergyrecords;
            }
            set { _allergyrecords = value; }
        }
        public AddAllergy()
        {
            this.InitializeComponent(); 
            this.DataContext = this;
            viewSource = new CollectionViewSource();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            factor = (TwoFactor)e.Parameter;
        }

        public async Task<string> jsonResult(string url)
        {
            string data;
            var client = new HttpClient();
            HttpResponseMessage response = await client.GetAsync(url);
            using (HttpContent content = response.Content)
            {
                data = await content.ReadAsStringAsync();
            }

            return data;
        }



        private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            AllAllergies = new List<AllergyRecord>();
            string json = await jsonResult("http://ec2-54-83-191-130.compute-1.amazonaws.com:8080/Sanjeevani/rest/SV/GetAllergy/" + factor.Passcode);
            RootObject data = JsonConvert.DeserializeObject<RootObject>(json);

            foreach (var item in data.AllergyRecords)
            {
                AllAllergies.Add(item);
            }
            Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
    viewSource.Source = AllAllergies;
});

        }
    }

这是我的Root Object类。

public class AllergyRecord
    {
        public string allergy_date { get; set; }
        public string allergy_name { get; set; }
        public string reaction { get; set; }
        public string recordID { get; set; }
        public string severity { get; set; }
        public string status { get; set; }
    }

    public class RootObject
    {
        public List<AllergyRecord> AllergyRecords { get; set; }
        public string ForceLogOn { get; set; }
        public string returnCode { get; set; }
    }

2 个答案:

答案 0 :(得分:1)

好的,这是我做的步骤。万一它可以帮助某人。首先是我对gridview部分的XAML更改。

<GridView x:Name="grdData" Grid.Row="1" ItemsSource="{x:Bind AllAllergies}" Height="300" SelectionMode="Single" Width="Auto" Background="AliceBlue">
            <GridView.ItemTemplate>
                <DataTemplate x:DataType="local:AllergyRecord">
                    <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
                        <TextBlock Text="{x:Bind recordID}"></TextBlock>
                        <TextBlock Text="{x:Bind allergy_name}"></TextBlock>
                        <TextBlock Text="{x:Bind reaction}"></TextBlock>
                        <TextBlock Text="{x:Bind allergy_date}"></TextBlock>
                        <TextBlock Text="{x:Bind severity}"></TextBlock>
                        <TextBlock Text="{x:Bind status}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>

我在XAML.cs中所做的更改是

注释掉this.DataContext =这一行下面的Initialize组件一起在最后的异步页面加载我刚说了这个.Bindings.Update();作为最后一行,它的工作原理。这是同一类型的问题和答案。 Data not displaying (C# UWP)

答案 1 :(得分:-1)

既然你提到了MVVM,我会建议一个MVVM模式的答案。

首先,您需要将Web调用移动到ViewModel类。所以它的外观如下。

public class AllAllergiesViewModel
{
    public RootObject rootObject { get; set; }
    public async Task GetAllAllergies()
    {
        string json = await jsonResult("http://ec2-54-83-191-130.compute-1.amazonaws.com:8080/Sanjeevani/rest/SV/GetAllergy/" + factor.Passcode);
        this.rootObject = JsonConvert.DeserializeObject<RootObject>(json);
    }

    internal async Task<string> jsonResult(string url)
    {
        string data;
        var client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync(url);
        using (HttpContent content = response.Content)
        {
            data = await content.ReadAsStringAsync();
        }
        return data;
    }
}

您的RootObject将如下所示。

public class RootObject
{
    public ObservableCollection<AllergyRecord> AllergyRecords { get; set; }
    public string ForceLogOn { get; set; }
    public string returnCode { get; set; }
}

MainPage_Loaded中,您可以使用下面的DataContext

private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    var viewModel = new AllAllergiesViewModel();
    grdData.DataContext = viewModel;
    await viewModel.GetAllAllergies();
}

你的XAML将是

<GridView Grid.Row="1" ItemsSource="{Binding AllergyRecords}" Height="500" SelectionMode="Single" Width="Auto" x:Name="grdData">
    <GridView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding recordID}"></TextBlock>
                <TextBlock Text="{Binding allergy_name}"></TextBlock>
                <TextBlock Text="{Binding reaction}"></TextBlock>
                <TextBlock Text="{Binding allergy_date}"></TextBlock>
                <TextBlock Text="{Binding severity}"></TextBlock>
                <TextBlock Text="{Binding status}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>