2017-03-09 72 views
1

我正在開發一個uwp應用程序。在這種情況下,我沒有遵循MVVM。事情是我從網址獲取json數據並使用Newtonsoft解析它,並且想要將其綁定到DataGrid控件。問題是datagrid根本沒有顯示出來。在頁面加載時,我使用Dispatcher填充collectionviewsource並綁定到GridView。但沒有幫助。請幫忙。GridView不顯示uwp中的記錄

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

     } 
    } 

這裏是我的根對象類。

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; } 
    } 
+0

您確定'factor.Passcode'具有良好的價值,因爲我認爲在'Page_Loaded'事件處理程序之後調用了'OnNavigatedTo'。 –

+0

是的,我保持斷點,數據完全填充在列表中。 – nikhil

回答

-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> 
+0

我知道如何使用MVVM做到這一點,但問題是如何在viewmodel中實現OnNavigatedTo。它不允許我/ – nikhil

+0

這個問題不是關於MVVM模式的! – Paul

0

確定這裏是我做的步驟。如果它有助於某人。首先這裏是我對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 =初始化組件下面這條線一起,在異步頁面加載結束時,我剛纔說的這一點。 Bindings.Update();作爲最後一行,它的工作原理。這是相同類型的問題和答案。 Data not displaying (C# UWP)

+0

對我來說,只是將'ItemsSource =「{Binding viewSource.View}」'更改爲'ItemsSource =「{x:Bind AllAllergies}」'工作。 – Paul