【问题标题】:Can't get DbGeography to work in Xamarin.Forms app无法让 DbGeography 在 Xamarin.Forms 应用程序中工作
【发布时间】:2018-05-30 15:45:51
【问题描述】:

执行所有 Gets、Puts、Posts 等操作的 SDK 是 PCL。因此,我无法安装实体框架。

使用 Postman 的基本获取返回以下关于 DbGeography 的数据。

"GeoLocation": {
                        "$id": "6",
                        "Geography": {
                            "$id": "7",
                            "CoordinateSystemId": 4326,
                            "WellKnownText": "POINT (-98.7687222 32.8494985)"
                        }
                    },

由于无法安装实体框架,我尝试创建自己的 DbGeography 和 DbGeographyWellKnownValue。

我从应用程序发出的 API 调用应返回 4 条完整记录。相反,一旦 Json 被反序列化,它就会返回 1 条完整记录和 3 条空记录。我认为这与 DbGeography 字段有关。我试过添加 [JsonIgnore],但这仍然没有得到 4 条完整的记录。这是我的 Location、DbGeography、DbGeographyWellKnownValue 和 DbGeographyConverter 类。

位置

[JsonProperty("GeoLocation")]
[JsonConverter(typeof(DbGeographyConverter))]
public DbGeography GeoLocation { get; set; }

数据库地理

public class DbGeography
    {

        [System.Runtime.Serialization.DataMemberAttribute(Name = "Geography")]
        public DbGeographyWellKnownValue Geography { get; set; }

        /// <summary>
        /// Custom FromText method 
        /// </summary>
        /// <param name="wellKnownText"></param>
        /// <param name="coordinateSystemId"></param>
        /// <returns></returns>
        public static DbGeography FromText(string wellKnownText, int coordinateSystemId)
        {

            DbGeography Geography = new DbGeography()
            {

                Geography = new DbGeographyWellKnownValue()
                {
                    WellKnownText = wellKnownText,
                    CoordinateSystemId = coordinateSystemId,

                }
            };
            return Geography;
        }

    }

DbGeographyWellKnownValue

public class DbGeographyWellKnownValue
    {
        /// <summary>
        /// The coordinate ID
        /// </summary>
        [System.Runtime.Serialization.DataMemberAttribute(Order = 1, IsRequired = false, EmitDefaultValue = false)]
        public int? CoordinateSystemId { get; set; }

        /// <summary>
        /// Optional.
        /// </summary>
        //public string WellKnownBinary { get; set; }

        /// <summary>
        /// The WellKnownText value
        /// </summary>
        [System.Runtime.Serialization.DataMemberAttribute(Order = 2, IsRequired = false, EmitDefaultValue = false)]
        public string WellKnownText { get; set; }

    }

最后,我有一个 DbGeographyConverter 类。

public class DbGeographyConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            //return objectType.IsAssignableFrom(typeof(string));
            return objectType.Equals(typeof(DbGeography));
            //return objectType.Equals(typeof(string));
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {

            //Handles issue with null DbGeography values
            if (reader.TokenType == JsonToken.Null)
                return default(DbGeography);

            JObject location = JObject.Load(reader);
            JToken token = location["Geography"]["WellKnownText"];
            JToken token2 = location["Geography"]["CoordinateSystemId"];
            string point = token.ToString();
            int coordinateId = Convert.ToInt32(token2.ToString()); 

            //If value is null lets return the default
            if (point == null)
                return default(DbGeography);

            //If we get to here lets convert
            var converted = DbGeography.FromText(point, coordinateId);
            return converted;

            //Can't convert, return NULL
            //return default(DbGeography);

        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            // Base serialization is fine
            serializer.Serialize(writer, value);

            //Handle null values
            //var dbGeography = value as DbGeography;
            //serializer.Serialize(writer, dbGeography == null || dbGeography.IsEmpty ? null : value);
            //serializer.Serialize(writer, dbGeography == null ? null : value);

        }
    }

这一切都在我的 MVC 5 Web 应用程序中完美运行,并且使用相同的 API,所以我知道它与 DbGeography 字段有关。

非常感谢任何帮助。谢谢!

【问题讨论】:

    标签: xamarin.forms entity-framework-6 dbgeography


    【解决方案1】:

    好的,这确实有效。问题是我的 Mapper 类无法使用 JSON 参考符号,如下所示。

        {
            "$ref": "3"
        },
        {
            "$ref": "5"
        },
        {
            "$ref": "4"
        } 
    

    我现在正在使用它,它可以工作。

    JsonConvert.DeserializeObject<IEnumerable<MerchantLocation>>(jsonString);
    

    【讨论】:

      猜你喜欢
      • 2016-05-18
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 2014-11-04
      相关资源
      最近更新 更多