无法使用C#从Xamarin.Forms中的类构建数组

问题描述 投票:0回答:2

我正在使用C#开发Xamarin.Forms项目,以连接到OPC服务器并读取值。我能够读取这些值,但是在将它们整理到列表或数组时遇到了麻烦。完成后,我想将值转换为ASCII。

下面是传递的代码;

var readRequest = new ReadRequest
{
    // set the NodesToRead to an array of ReadValueIds.
    NodesToRead = new[] {
    // construct a ReadValueId from a NodeId and AttributeId.
    new ReadValueId {
        // you can parse the nodeId from a string.
        // e.g. NodeId.Parse("ns=2;s=Demo.Static.Scalar.Double")
        NodeId = NodeId.Parse("ns=2;s=Link_CatConHybrid.2D.InStr1"),

        //NodeId.Parse(VariableIds.Server_ServerStatus),
        // variable class nodes have a Value attribute.
        AttributeId = AttributeIds.Value
    },

    new ReadValueId
    {
        NodeId = NodeId.Parse("ns=2;s=Link_CatConHybrid.2D.InStr2"),
        AttributeId = AttributeIds.Value
    }
}

};
// send the ReadRequest to the server.
var readResult = await channel.ReadAsync(readRequest);

// DataValue is a class containing value, timestamps and status code.
// the 'Results' array returns DataValues, one for every ReadValueId.

DataValue dvr = readResult.Results[0];
DataValue dvr2 = readResult.Results[1];

Console.WriteLine("The value of Instr1 is {0}, InStr2 is {1}", dvr.Variant.Value, dvr2.Variant.Value);

我在做什么错或忽视?

编辑:如何将所有readResults合并为一个?

readResults

c# arrays xamarin xamarin.forms
2个回答
1
投票

只需创建一个DataValue列表并将其存储。尝试:

List<DataValue> endResult = new List<DataValue>();
foreach (DataValue  value in readResult.Results)
{
    endResult.Add(value);
}

0
投票

由于结果是DataValue的老旧集合,所以只能说

var dataValueCollection = readResult.Results; // if you want return collection you can just say return readResult.Results

如果尝试将值写入控制台,则可以直接在readResult.Results上进行循环,如下所示:

foreach(var dv in readResult.Results)
{
    Console.WriteLine("The Value of InStr = {0}", dv.Variant.Value);
    Console.WriteLine("The Value of InStr = {0}", dv.Variant.ReadValueId); // This line shows how to access ReadValueId.
    // You can access other properties same as above
}
© www.soinside.com 2019 - 2024. All rights reserved.