【发布时间】:2015-05-20 07:14:14
【问题描述】:
我正在尝试获取客户数据以将它们解析为客户对象以显示在 TableView 上。以下代码有时有效,有时无效。每当它崩溃时,即使我每次都运行相同的代码,它也会在 foreach 循环中显示客户数据为空。我不知道在这种情况下可能出了什么问题。我是这个平台的新手。如果我遗漏任何内容/额外信息,请告诉我。
namespace TableViewExample
{
public partial class MyDataServices : ContentPage
{
private ODataClient mODataClient;
private IEnumerable <IDictionary<string,object>> Customers;
public MyDataServices ()
{
InitializeComponent ();
InitializeDataService ();
GetDataFromOdataService ();
TableView tableView = new TableView{ };
var section = new TableSection ("Customer");
foreach (var customers in Customers) {
//System.Diagnostics.Debug.WriteLine ((string)customers ["ContactName"]);
var name = (string)customers ["ContactName"];
var cell = new TextCell{ Text = name };
section.Add (cell);
}
tableView.Root.Add (section);
Padding = new Thickness (10, 20, 10, 10);
Content = new StackLayout () {
Children = { tableView }
};
}
private void InitializeDataService(){
try {
mODataClient = new ODataClient ("myURL is here");
}
catch {
System.Diagnostics.Debug.WriteLine("ERROR!");
}
}
private void GetDataFromOdataService (){
try {
Customers = mODataClient.For ("Customers").FindEntries ();
}
catch {
System.Diagnostics.Debug.WriteLine("ERROR!");
}
}
}
}
【问题讨论】:
-
它在哪里崩溃?你得到什么例外?你在什么上面运行它?你在玩模拟器吗?还是在设备上?哪个模拟器?哪个设备?
-
它在模拟器上崩溃了,我还没有在设备上测试过。我只在模拟器中使用带有 iOS 8.0 的 iPhone 5 进行测试
-
显示客户为空。我试图得到这个错误,但很幸运,即使我已经运行了该应用程序近 30 次,它也没有出现。
-
如果
Customers为空,我不明白它为什么会崩溃(foreach将被跳过,您添加一个空的section)。它真的崩溃了吗?有例外吗?还是只是没有达到您的预期? -
我希望我能得到崩溃的快照。但是在我发布这个问题后我无法得到。我发现我应该使用异步。用于网络服务方法。我说的对吗?
标签: xamarin xamarin.forms