我不明白如何使用参数中的数组项来记录事件。
例如,如果我需要记录类似 EventNamesConstants.Login 的内容,我可以这样做
var parameters = new Dictionary<object, object> {
ParameterNamesConstants.Method, "Google" }
};
Analytics.LogEvent(EventNamesConstants.Login, parameters);
但现在我需要登录
EventNamesConstants.ViewItem
参数之一是项目数组,其中项目有名称。
当我尝试做类似的事情时
var items = new[]{
new Dictionary<object, object> {
ParameterNamesConstants.ItemName, title }
}
};
var parameters = new Dictionary<object, object> {
ParameterNamesConstants.Currency, currencyCode },
ParameterNamesConstants.Value, value },
ParameterNamesConstants.Items, items }
};
我收到一个错误。 我试图找到示例,但他们没有数组。 非常抱歉。
错误是:
不知道如何封送类型的对象
'System.Collections.Generic.Dictionary`2[System.Object,System.Object][]'
to an NSObject
Firebase 的 func Analytics.LogEvent 是:
public static void LogEvent (string name, Dictionary<object, object>? parameters)
{
LogEvent (name, (parameters == null) ? null :
((parameters!.Keys.Count == 0) ? new NSDictionary<NSString, NSObject>
() : NSDictionary<NSString, NSObject>.FromObjectsAndKeys
(parameters!.Values.ToArray (), parameters!.Keys.ToArray (),
nint.op_Implicit (parameters!.Keys.Count))));
}
请帮助我。
作为解决方法,您可以尝试以下方法来创建参数,
var keys = new[]
{
new NSString("key1"),
new NSString("key2"),
};
var objects = new NSObject[]
{
new NSString("object1"),
new NSString("object1"),
};
NSDictionary dicionary = new NSDictionary<NSString, NSObject>(keys, objects);
NSArray items = NSArray.FromNSObjects(dicionary);
var parameters = new Dictionary<object, object>
{
{ ParameterNamesConstants.Currency, currencyCode },
{ ParameterNamesConstants.Value, value },
{ ParameterNamesConstants.Items, items }
};
我还在我的应用中使用 Firebase Analytics。
这是
IDictionary<string, string>
上合适的扩展方法,任何人都可以使用它轻松地将 C# IDictionary
转换为 iOS NSDictionary
/// <summary>
/// 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.
/// </summary>
/// <param name="dictionary">The dictionary to convert. Cannot be null.</param>
/// <returns>An <see cref="NSDictionary"/> with <see cref="NSString"/> keys and <see cref="NSObject"/> values corresponding to the entries in the input dictionary.</returns>
/// <exception cref="ArgumentNullException">Thrown if the input dictionary is null.</exception>
/// <remarks>
/// 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.
/// It's designed to be used as an extension method for any IDictionary<string, string> instance.
/// Note: Null keys or values in the input dictionary will result in exceptions, as <see cref="NSString"/> cannot be instantiated with a null reference.
/// </remarks>
/// <example>
/// <code>
/// var myDict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } };
/// var nsDict = myDict.ToNSDictionary();
/// </code>
/// </example>
public static NSDictionary<NSString, NSObject> ToNSDictionary(this IDictionary<string, string> dictionary)
{
ArgumentNullException.ThrowIfNull(dictionary);
var keysAndValues = dictionary
.Select(kv => (Key: new NSString(kv.Key), Value: new NSString(kv.Value)))
.ToArray();
var keys = keysAndValues
.Select(kv => kv.Key)
.ToArray();
var values = keysAndValues
.Select(kv => kv.Value)
.ToArray();
return new NSDictionary<NSString, NSObject>(keys, values);
}
如果您需要,这里还有一份
IDictionary<string, object>
/// <summary>
/// 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.
/// </summary>
/// <param name="dictionary">The dictionary to convert. Cannot be null.</param>
/// <returns>An <see cref="NSDictionary"/> with <see cref="NSString"/> keys and <see cref="NSObject"/> values corresponding to the entries in the input dictionary.</returns>
/// <exception cref="ArgumentNullException">Thrown if the input dictionary is null.</exception>
/// <remarks>
/// 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.
/// It's designed to be used as an extension method for any IDictionary<string, object> instance.
/// Note: Null keys or values in the input dictionary will result in exceptions, as <see cref="NSString"/> cannot be instantiated with a null reference.
/// </remarks>
/// <example>
/// <code>
/// var myDict = new Dictionary<string, object> { { "key1", object1 }, { "key2", object2 } };
/// var nsDict = myDict.ToNSDictionary();
/// </code>
/// </example>
public static NSDictionary<NSString, NSObject> ToNSDictionary(this IDictionary<string, object> dictionary)
{
ArgumentNullException.ThrowIfNull(dictionary);
var keys = dictionary.Keys
.Select(arg => new NSString(arg))
.ToArray();
var objects = dictionary.Values
.Select(NSObject.FromObject)
.ToArray();
return new NSDictionary<NSString, NSObject>(keys, objects);
}
仅供参考:我使用
ArgumentNullException.ThrowIfNull
=> 仅自 C#10 起可用,如果您在 C#10 下,请使用经典方法。