2017-05-24 58 views
1

我很難用異步數據加載網格。從GridView中的API獲取JSON異步

我得到這個崩潰/錯誤,我假設因爲我還沒有數據或什麼?但我不能完全確定:

enter image description here

我的JSON從http://ramsey.api.com/hello拉很簡單,只要:

[{ 
    "middleName": "Sarco", 
    "firstName": "Marco", 
    "city": "Neverland", 
    "lastName": "Ramsey" 
}, { 
    "middleName": "Jarco", 
    "firstName": "Taco", 
    "city": "Niagra", 
    "lastName": "Ramsey" 
}] 

一切都在就好拉,我也映射它只是它的模型類:

public class Ramsey 
{ 
    public string firstName { get; set; } 
    public string city { get; set; } 
    public string lastName { get; set; } 
    public string middleName { get; set; } 
} 

但是,當我試圖拉入數據並將其映射到其網格綁定,我得到的錯誤。下面是我使用的方法:

public static async Task<List<Ramsey>> GetListAsyncModelPeopleManager() 
    { 
     var ramsey = new List<Ramsey>(); 

     var api = "http://ramsey.api.com/hello"; 
     var client = new HttpClient(); 
     var response = await client.GetAsync(api); 
     var result = await response.Content.ReadAsStringAsync(); 

     IEnumerable<Ramsey> results = JsonConvert.DeserializeObject<IEnumerable<Ramsey>>(result); 

     foreach (Ramsey r in results) 
     { 

      var first = r.firstName; 
      var middle = r.middleName; 
      var last = r.lastName; 
      var city = r.city; 

      var item = new Ramsey(); 
      item.firstName = first; 
      item.middleName = middle; 
      item.lastName = last; 
      item.city = city; 

      ramsey.Add(item); 
     } 

     return ramsey; 
    } 


} 

我知道我的綁定發現網格,因爲我可以運行這個硬編碼的數據,它的罰款:

public static List<Ramsey> GetListSyncModelPeopleManager() 
    { 
     var ramsey = new List<Ramsey>(); 
     ramsey.Add(new Ramsey { firstName = "Marco", city = "No City", lastName = "Ramsey" }); 
     ramsey.Add(new Ramsey { firstName = "Karpo", city = "Niagra", lastName = "Cropo" }); 
     ramsey.Add(new Ramsey { firstName = "Sarco", city = "Nacity", lastName = "Ramsey" }); 

     return ramsey; 
    } 

我更新這個所以即使我已經查看了像Stephen Cleary'sMSDN's這樣的文檔,我仍然試圖找出所有內容。

編輯

每請求,這裏是個例外:

System.ArgumentException occurred 
    HResult=0x80070057 
    Message=Value does not fall within the expected range. 
    Source=<Cannot evaluate the exception source> 
    StackTrace: 
    at Windows.UI.Xaml.Controls.ItemsControl.put_ItemsSource(Object value) 
    at qlckitWindows.MainPage.XamlBindingSetters.Set_Windows_UI_Xaml_Controls_ItemsControl_ItemsSource(ItemsControl obj, Object value, String targetNullValue) in \\Mac\code_repository\KIT\kitWindows\kitWindows\obj\x86\Debug\MainPage.g.cs:line 26 
    at qlckitWindows.MainPage.MainPage_obj1_Bindings.Update_list3(Task`1 obj, Int32 phase) in \\Mac\code_repository\QLC\qlckitWindows\qlckitWindows\obj\x86\Debug\MainPage.g.cs:line 248 
    at qlckitWindows.MainPage.MainPage_obj1_Bindings.Update_(MainPage obj, Int32 phase) in \\Mac\code_repository\QLC\qlckitWindows\qlckitWindows\obj\x86\Debug\MainPage.g.cs:line 240 
    at qlckitWindows.MainPage.MainPage_obj1_Bindings.Update() in \\Mac\code_repository\QLC\qlckitWindows\qlckitWindows\obj\x86\Debug\MainPage.g.cs:line 213 
    at qlckitWindows.MainPage.MainPage_obj1_Bindings.Initialize() in \\Mac\code_repository\QLC\qlckitWindows\qlckitWindows\obj\x86\Debug\MainPage.g.cs:line 207 
    at qlckitWindows.MainPage.MainPage_obj1_Bindings.Loading(FrameworkElement src, Object data) in \\Mac\code_repository\QLC\qlckitWindows\qlckitWindows\obj\x86\Debug\MainPage.g.cs:line 230 

這裏是我如何綁定我DataGrid的ItemsSource時:

<GridView x:Name="RamseyGridView" ItemsSource="{x:Bind list4}" IsItemClickEnabled="True" ItemClick="GridView_ItemClick"> 
     <GridView.ItemTemplate> 
       <DataTemplate x:DataType="data:Ramsey"> 
        <StackPanel x:Name="RamseyStackPan" Orientation="Horizontal" HorizontalAlignment="Center"> 
        <StackPanel x:Name="RamseyStackPanel" Margin="20,20,0,0" Background="LightGray" > 
         <TextBlock x:Name="RamseyFirstName" FontSize="18" Text="{x:Bind firstName}" HorizontalAlignment="Center"></TextBlock> 
         <TextBlock x:Name="RamseyLastName" FontSize="10" Text="{x:Bind lastName}" HorizontalAlignment="Center"></TextBlock> 
        </StackPanel> 
       </StackPanel> 
      </DataTemplate> 
     </GridView.ItemTemplate> 
    </GridView> 
+0

你得到的錯誤是什麼? – NicoRiff

+0

感謝您的問題/迴應!我碰到了一個崩潰,它出現在我提出的問題的屏幕截圖中,所以會發生崩潰,它會打開一個「MainPage.g.cs」文件,其中顯示錯誤「值不在預期範圍內」。有任何想法嗎?讓我知道你是否有其他問答。謝謝! – SRMR

+0

如果您嘗試打開設計視圖,會發生什麼情況?你能否在發現異常時發佈你擁有的調用堆棧? – Chadley08

回答

2

其實,你的方法用硬編碼值與實際方法不完全相同。你的第一個代碼示例返回Task<List<Ramsey>>,硬編碼返回的只是一個普通的List<Ramsey>

以下是我爲證明自己的觀點所做的代碼示例。

我的數據類:

public class DataModel 
{ 
    public string Name { get; set; } 
    public string city { get; set; } 
} 

功能:

public static async Task<List<DataModel>> GetListAsync() 
{ 
    await Task.Delay(500); 

    var list = new List<DataModel> 
    { 
     new DataModel() { Name = "me", City = "Atlantis" }, 
     new DataModel() { Name = "you", City = "Timbuktu" } 
    }; 
    return list; 
} 

public static List<DataModel> GetList() 
{ 
    var list = new List<DataModel> 
    { 
     new DataModel() { Name = "me", City = "Atlantis" }, 
     new DataModel(0 { Name = "you, City = "Timbuktu" } 
    } 
    return list; 
} 

所以,有了這些功能,並已在XAML一個DataGrid名爲DataGrid中MainWindow類:

<Window x:Class="WpfApp1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://sxhemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://xchemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://sxhemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApp1" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <DataGrid x:Name="dataGrid" /> 
    </Grid> 
</Window> 

然後下面的構造函數工作得很好,你會得到一個包含數據的數據網格:

public MainWindow() 
{ 
    InitializeComponent(); 

    dataGrid.ItemsSource = GetList(); 
} 

下面的構造甚至不會編譯,因爲任務的返回:

public MainWindow() 
{ 
    InitializeComponent(); 

    dataGrid.ItemsSource = GetListAsync(); 
} 

下面的構造函數編譯但導致程序無限期掛起:

public MainWindow() 
{ 
    InitializeComponent(); 

    var data = GetListAsync(); 
    dataGrid.ItemsSource = data.Result; 
} 

而且這裏有一個實際工作的構造函數:

public MainWindow() 
{ 
    InitializeComponent(); 

    LoadData(); 

    //placing a function within a method is available in C#7 (Visual Studio 2017) 
    //if you're using an older editor or C# version, just make this a private method of the MainWindow class 
    async void LoadData() 
    { 
     dataGrid.ItemsSource = await GetListAsync(); 
    } 
} 

所以,我不知道你是如何綁定你的DataGrid的ItemsSource屬性的,但是看起來你可能在XAML中這樣做了,然後XAML的綁定方法在這個Task上窒息。如果你用一個Async Void方法把它改成某種初始化,就像我在最後一個構造函數中演示的那樣,那麼它應該可以工作。

+0

太好了!所以我認爲這是開始有意義的地方在我的邏輯小點丟失!正如我期待通過你的答案,我的數據類看起來不錯迄今... – SRMR

+0

當我到的功能,我想知道,如果你正在展示這兩種方法,但實際上只是年底說只使用'GetListAsync'方法?而在這種方法中,它看起來像是對數據模型項進行硬編碼,而不是像我一樣從API中拉取它們? – SRMR

+0

那麼對於XAML,您有然後命名它的內部,在那裏我有然後它裏面......我應該做的,你是怎麼做的,還是用我的但是稍微修改? – SRMR