我很難用異步數據加載網格。從GridView中的API獲取JSON異步
我得到這個崩潰/錯誤,我假設因爲我還沒有數據或什麼?但我不能完全確定:
我的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's和MSDN'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>
你得到的錯誤是什麼? – NicoRiff
感謝您的問題/迴應!我碰到了一個崩潰,它出現在我提出的問題的屏幕截圖中,所以會發生崩潰,它會打開一個「MainPage.g.cs」文件,其中顯示錯誤「值不在預期範圍內」。有任何想法嗎?讓我知道你是否有其他問答。謝謝! – SRMR
如果您嘗試打開設計視圖,會發生什麼情況?你能否在發現異常時發佈你擁有的調用堆棧? – Chadley08