【发布时间】:2019-06-01 22:22:52
【问题描述】:
我有一个使用 UWP、Android 和 iOS 的 Xamarin Forms 项目。我在后端使用托管在 Azure 中的 SQL 数据库,用户可以搜索食物并获取与食物相关的不同测量值和营养值。问题是我使用 foreach 循环遍历查询的食物列表并创建和对象以保存更高级别的食物属性(如名称和度量)并组合包含所有营养素和营养值的第二个列表食物。在创建对象的过程中,仅在 iOS 上,当循环对象中有数据时,未设置 Measure 属性,并且在调试模式下,我看到 Measure 的值为 {...} 并且属性 Measure 从实例化的对象。这只发生在 iOS 上,而不是 UWP 或 Android。这是它在调试中的图片: Food item with data 这是新对象实例化的缺失属性: NewFood without some properties 这是我从未见过的 Measure 属性: Measure Property
这里是食物对象类
public class FoodNutrients : BindableObject
{
public string NDB { get; set; }
public string Food { get; set; }
public string Measure { get; set; }
public string MeasureType { get; set; }
public string MeasureAmount { get; set; }
public double Protein { get; set; }
public double Sodium { get; set; }
public double Potassium { get; set; }
public double Phosphorus { get; set; }
public double Carbs { get; set; }
public double Fat { get; set; }
public double SaturatedFat { get; set; }
public double Calories { get; set; }
public string SodiumRec { get; set; }
public string ProteinRec { get; set; }
public string PotassiumRec { get; set; }
public string PhosphorusRec { get; set; }
public string CarbsRec { get; set; }
public string FatRec { get; set; }
public string SatFatRec { get; set; }
public string CaloriesRec { get; set; }
public string Recommendation { get; set; }
public string RecColor { get; set; }
}
这是foreach循环
foreach (var food in foodList1)
{
FoodNutrients newFood = new FoodNutrients();
string measureString = "- " + food.MeasureAmount + " " + food.Measure;
newFood.NDB = food.NDB;
newFood.Food = food.Food;
newFood.MeasureType = food.Measure;
newFood.MeasureAmount = food.MeasureAmount;
newFood.Measure = measureString;
newFood.Protein = FoodNutrients.GetProteinValue(foodList2, food.NDB, (double)food.MeasureWeightGrams);
newFood.Sodium = FoodNutrients.GetSodiumValue(foodList2, food.NDB, (double)food.MeasureWeightGrams);
newFood.Potassium = FoodNutrients.GetPotassiumValue(foodList2, food.NDB, (double)food.MeasureWeightGrams);
newFood.Phosphorus = FoodNutrients.GetPhosphorusValue(foodList2, food.NDB, (double)food.MeasureWeightGrams);
newFood.Carbs = FoodNutrients.GetCarbValue(foodList2, food.NDB, (double)food.MeasureWeightGrams);
newFood.Fat = FoodNutrients.GetFatValue(foodList2, food.NDB, (double)food.MeasureWeightGrams);
newFood.SaturatedFat = FoodNutrients.GetSatFatValue(foodList2, food.NDB, (double)food.MeasureWeightGrams);
newFood.Calories = FoodNutrients.GetCalorieValue(foodList2, food.NDB, (double)food.MeasureWeightGrams);
newFood = DailyAllowanceController.GetRecommendation(newFood, user);
foodList.Add(newFood);
}
再一次,在 Android 和 UWP 中没有语法错误,而且这个代码词完美。请查看以上描述中的图片,因为它们提供了一些真实的细节。
【问题讨论】:
标签: c# xamarin xamarin.forms xamarin.ios