Xamarin iOS Firebase Analytics

pinkon5k  于 12个月前  发布在  iOS
关注(0)|答案(2)|浏览(121)

我不明白如何记录参数中包含数组项的事件。
例如,如果我需要记录类似EventData Constants.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


和func Analytics. Firebase的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)))); 
}


请帮帮我

hts6caw3

hts6caw31#

我也在我的应用程序中使用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&lt;string, string&gt; 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&lt;string, string&gt; { { "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&lt;string, object&gt; 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&lt;string, object&gt; { { "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以下,请使用经典方式。

8mmmxcuj

8mmmxcuj2#

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

var keys = new NSObject[]
{
    new NSString(ItemName),

};

var objects = new NSObject[]
{ 
    new NSString(title),

};

NSDictionary dicionary = new NSDictionary<NSObject, NSObject>(keys, objects);

NSArray items =  NSArray.FromNSObjects(dicionary);

var parameters = new Dictionary<object, object>
{
    { ParameterNamesConstants.Currency, currencyCode },
    { ParameterNamesConstants.Value, value },
    { ParameterNamesConstants.Items, items }
};

字符串

相关问题