Xamarin iOS Firebase Analytics

pinkon5k  于 2024-01-04  发布在  iOS
关注(0)|答案(2)|浏览(151)

我不明白如何记录参数中包含数组项的事件。
例如,如果我需要记录类似EventData Constants.Login的内容,

  1. var parameters = new Dictionary<object, object> {
  2. ParameterNamesConstants.Method, "Google" }
  3. };
  4. Analytics.LogEvent(EventNamesConstants.Login, parameters);

字符串
但是现在我需要记录EventNamesConstants.ViewItem,其中一个参数是项目数组,其中项目有一个名称。

  1. var items = new[]{
  2. new Dictionary<object, object> {
  3. ParameterNamesConstants.ItemName, title }
  4. }
  5. };
  6. var parameters = new Dictionary<object, object> {
  7. ParameterNamesConstants.Currency, currencyCode },
  8. ParameterNamesConstants.Value, value },
  9. ParameterNamesConstants.Items, items }
  10. };


我得到了一个错误。我试图找到例子,但他们没有一个数组。我很抱歉。
错误是:
不知道如何封送类型为

  1. 'System.Collections.Generic.Dictionary`2[System.Object,System.Object][]'
  2. to an NSObject


和func Analytics. Firebase的LogEvent是:

  1. public static void LogEvent (string name, Dictionary<object, object>? parameters)
  2. {
  3. LogEvent (name, (parameters == null) ? null :
  4. ((parameters!.Keys.Count == 0) ? new NSDictionary<NSString, NSObject>
  5. () : NSDictionary<NSString, NSObject>.FromObjectsAndKeys
  6. (parameters!.Values.ToArray (), parameters!.Keys.ToArray (),
  7. nint.op_Implicit (parameters!.Keys.Count))));
  8. }


请帮帮我

hts6caw3

hts6caw31#

我也在我的应用程序中使用Firebase Analytics。
下面是IDictionary<string, string>上的一个合适的扩展方法,任何人都可以使用它轻松地将C# IDictionary转换为iOS NSDictionary

  1. /// <summary>
  2. /// Converts an <see cref="IDictionary{TKey, TValue}"/> of string key-value pairs to an <see cref="NSDictionary"/> of <see cref="NSString"/> keys and <see cref="NSObject"/> values.
  3. /// </summary>
  4. /// <param name="dictionary">The dictionary to convert. Cannot be null.</param>
  5. /// <returns>An <see cref="NSDictionary"/> with <see cref="NSString"/> keys and <see cref="NSObject"/> values corresponding to the entries in the input dictionary.</returns>
  6. /// <exception cref="ArgumentNullException">Thrown if the input dictionary is null.</exception>
  7. /// <remarks>
  8. /// This method iterates over the input dictionary to create a pair of arrays for keys and values, which are then used to construct the NSDictionary.
  9. /// It's designed to be used as an extension method for any IDictionary&lt;string, string&gt; instance.
  10. /// Note: Null keys or values in the input dictionary will result in exceptions, as <see cref="NSString"/> cannot be instantiated with a null reference.
  11. /// </remarks>
  12. /// <example>
  13. /// <code>
  14. /// var myDict = new Dictionary&lt;string, string&gt; { { "key1", "value1" }, { "key2", "value2" } };
  15. /// var nsDict = myDict.ToNSDictionary();
  16. /// </code>
  17. /// </example>
  18. public static NSDictionary<NSString, NSObject> ToNSDictionary(this IDictionary<string, string> dictionary)
  19. {
  20. ArgumentNullException.ThrowIfNull(dictionary);
  21. var keysAndValues = dictionary
  22. .Select(kv => (Key: new NSString(kv.Key), Value: new NSString(kv.Value)))
  23. .ToArray();
  24. var keys = keysAndValues
  25. .Select(kv => kv.Key)
  26. .ToArray();
  27. var values = keysAndValues
  28. .Select(kv => kv.Value)
  29. .ToArray();
  30. return new NSDictionary<NSString, NSObject>(keys, values);
  31. }

字符串
如果你需要,这里是另一个在IDictionary<string, object>

  1. /// <summary>
  2. /// Converts an <see cref="IDictionary{TKey, TValue}"/> of string key-value pairs to an <see cref="NSDictionary"/> of <see cref="NSString"/> keys and <see cref="NSObject"/> values.
  3. /// </summary>
  4. /// <param name="dictionary">The dictionary to convert. Cannot be null.</param>
  5. /// <returns>An <see cref="NSDictionary"/> with <see cref="NSString"/> keys and <see cref="NSObject"/> values corresponding to the entries in the input dictionary.</returns>
  6. /// <exception cref="ArgumentNullException">Thrown if the input dictionary is null.</exception>
  7. /// <remarks>
  8. /// This method iterates over the input dictionary to create a pair of arrays for keys and values, which are then used to construct the NSDictionary.
  9. /// It's designed to be used as an extension method for any IDictionary&lt;string, object&gt; instance.
  10. /// Note: Null keys or values in the input dictionary will result in exceptions, as <see cref="NSString"/> cannot be instantiated with a null reference.
  11. /// </remarks>
  12. /// <example>
  13. /// <code>
  14. /// var myDict = new Dictionary&lt;string, object&gt; { { "key1", object1 }, { "key2", object2 } };
  15. /// var nsDict = myDict.ToNSDictionary();
  16. /// </code>
  17. /// </example>
  18. public static NSDictionary<NSString, NSObject> ToNSDictionary(this IDictionary<string, object> dictionary)
  19. {
  20. ArgumentNullException.ThrowIfNull(dictionary);
  21. var keys = dictionary.Keys
  22. .Select(arg => new NSString(arg))
  23. .ToArray();
  24. var objects = dictionary.Values
  25. .Select(NSObject.FromObject)
  26. .ToArray();
  27. return new NSDictionary<NSString, NSObject>(keys, objects);
  28. }


仅供参考:我使用ArgumentNullException.ThrowIfNull =>仅从C#10开始可用,如果您在C#10以下,请使用经典方式。

展开查看全部
8mmmxcuj

8mmmxcuj2#

作为一种解决方法,您可以尝试以下方法来创建参数,

  1. var keys = new NSObject[]
  2. {
  3. new NSString(ItemName),
  4. };
  5. var objects = new NSObject[]
  6. {
  7. new NSString(title),
  8. };
  9. NSDictionary dicionary = new NSDictionary<NSObject, NSObject>(keys, objects);
  10. NSArray items = NSArray.FromNSObjects(dicionary);
  11. var parameters = new Dictionary<object, object>
  12. {
  13. { ParameterNamesConstants.Currency, currencyCode },
  14. { ParameterNamesConstants.Value, value },
  15. { ParameterNamesConstants.Items, items }
  16. };

字符串

展开查看全部

相关问题