如何在 Xamarin.iOS 中将嵌套模型信息上传到 firestore

How to upload nested model information to firestore in Xamarin.iOS

我在 Xamarin.Forms 使用 Xamarin.Firebase.iOS.Firestore 包工作,我有一个嵌套模型,如

public class Grow
{
    public string Id { get; set; }
    public string PlantName { get; set; }
    public List<Day> Days { get; set; }
}
public class Day
{
    public string Id { get; set; }
    public string GrowId { get; set; }
    public string LightsOn { get; set; }
    public string LightsOff { get; set; }
    public List<Nutrient> Nutrients { get; set; }
}
public class Nutrient
{
    public string Id { get; set; }
    public string DayId { get; set; }
    public string NutrientName { get; set; }
    public double NutrientAmount { get; set; }
}

而且我不确定如何在 Xamarin.iOS 中构建它以便上传,我想将其作为字典上传。我应该使用 Dictionary<object, object> 吗?我如何将它转换为键和值的 iOS 特定代码?我是 Xamarin 的新手,如果这看起来像一个初学者问题,我很抱歉! 我试过的是

public async Task<bool> InsertGrow(Grow grow)
{
    try
    {
        var keys = new NSObject[]
        {
            new NSString("plantName"),
            new NSObject(new NSString[] {
                new NSString("lightsOn"),
                new NSString("lightsOff"),
                new NSObject(new NSString[] {
                    new NSString("nutrientName"),
                    new NSString("nutrientAmount")
                })
           })
        }
    }
}

对于键,但我认为这不是正确的方法,将每个模型作为一个单独的字典会更好吗?

  1. 我不会在使用 Xamarin Forms 时存储 iOS 特定对象。只需存储常规的 c# 字符串和对象。

  1. 我没能找到任何有用的“嵌套对象”C# 示例to/from,因此开始“社区”回答并提供一些可能帮助您入门的链接。

Dotnet firestore sample.

展示了一些有用的技巧。特别注意属性 [FirestoreProperty] 以放置应存储的属性。但是不显示嵌套对象。

The above sample was reached from Add Data to Cloud Firestore.

浏览那些官方 Google 文档。请注意它们如何显示不同语言的代码。每个代码片段的右侧是 More 下拉列表。 c# 在该下拉列表中。


miyu's Plugin.CloudFirestore - see section "Data-Mapping".

我看到有人提到它可以更轻松地从 C# 访问 firestore。

Handling Nested Objects in firestore with flutter.

这不是 C#,它是 flutter(Dart 语言)。如果有人为 C# 编写了这样的博客,那就太好了。我提到它是因为它讨论了在 firestore 中嵌套对象所涉及的原则。


android - how to save data in xamarin firestore.

由于这两行而包括在内:

resp = mDatabase.Child(username + "/Phone");
resp.SetValue(phone.Text);

从根本上说,这就是存储“键”和对应的“值”所需要的。在 Firestore 中建立“字典”的一种方法。在这种情况下,键是 username + "/Phone",值是 phone.Text.