【问题标题】:Using jagged arrays使用锯齿状数组
【发布时间】:2015-10-21 08:16:48
【问题描述】:

考虑以下代码。

public string[][] CalculateDistance(string origin, string destination)
{
        string[][] result = new string[1][];

        string url = "MyUrl"
        string requesturl = url;
        string content = fileGetContents(requesturl);
        JObject o = JObject.Parse(content);
        result[0] = new string[2];
        result[0][0] = (string)o.SelectToken("routes[0].legs[0].distance.text");
        result[0][1] = (string)o.SelectToken("routes[0].legs[0].duration.text");

        string[][] myArray = new string[2][];
        for (int i = 0; i < result.Length; i++)
        {
            string[] innerArray = result[i];
        }
        return result;
}

我正在尝试返回一个锯齿状数组,然后在 wpf 应用程序的 ListView 上使用它。如果我在for 循环内使用Console.WriteLine(innerArray),我会显示正确的结果。但是,当显示在 ListView 中时,我得到了

字符串[][] 数组

谁能告诉我哪里出错了。我以前从未使用过锯齿状数组,所以我发现很难弄清楚我做错了什么。

XMAL 代码如下:

<ListView Name="MyList" HorizontalAlignment="Left" Height="315" Margin="1289,425,-435,0" VerticalAlignment="Top" Width="421">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name"
                DisplayMemberBinding="{Binding Time}"
                Width="100"/>
            </GridView>
        </ListView.View>
</ListView>

以及将项目添加到我使用的列表的后端:

foreach (var items in GetAddress())
{
  MyList.Items.Add(new Distance() { Time = distance.CalculateDistance(items.FromPostCode, items.DestinationPostCode) });
}

距离类看起来像

public class Distance
{
    public string[][] Time { get; set; }
    //More properties 
}

【问题讨论】:

  • 请出示您的 WPF 代码。没关系,如何填充数组 - 样本数据就足够了。
  • @Dennis 更新了我的问题
  • 在后面的代码中,您只需将数组添加到项目集合中。显然,ListView 调用Distance.Time.ToString() 来获得项目的可视化表示。你想如何显示这个数组?
  • 你想如何在列表视图中显示结果
  • @Dennis 在控制台中,我的数组看起来像 7.1 mi11 mins,我想在我的列表中显示它们,所以第一列变成英里,第二列变成分钟

标签: c# arrays wpf jagged-arrays


【解决方案1】:

首先将您的列表视图更改为类似这样的内容以进行正确的数据绑定。 (使用您自己的可选长度和属性。)

<ListView x:Name="MyList" Height="299" Width="497">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Miles" Width="100" DisplayMemberBinding="{Binding Miles}"/>
            <GridViewColumn Header="Mins" Width="100" DisplayMemberBinding="{Binding Mins}"/>
        </GridView>
    </ListView.View>
</ListView>

这里是使用锯齿状数组的示例。

string[][] list = new[] {new[] {"Hello", "Bye"}, new[] {"Hey", "Ho"}, new[] {"Yep", "Nope"}};
MyList.ItemsSource = list.Select(x => new {Miles = x[0], Mins = x[1]});

但我不明白您使用锯齿状数组的原因。您已经创建了 1 个长度。那没有意义。只需使用长度为 2 的单个数组。如果您需要它来处理您未显示的其他内容,那么您必须显示它,然后我也会更新我的答案。目前我删除了不必要的部分。

public string[] CalculateDistance(string origin, string destination)
{
    string[] result = new string[2];

    string url = "MyUrl"
    string requesturl = url;
    string content = fileGetContents(requesturl);

    JObject o = JObject.Parse(content);

    result[0] = (string)o.SelectToken("routes[0].legs[0].distance.text");
    result[1] = (string)o.SelectToken("routes[0].legs[0].duration.text");

    return result;
}

然后当您要填充项目时。请注意,您也不需要 Distance 类。如果您仅将其用于绑定属性,则只需编写 new {} 来创建匿名类型,这样可以完美运行。

foreach (var items in GetAddress())
{
    var d = distance.CalculateDistance(items.FromPostCode, items.DestinationPostCode);
    MyList.Items.Add(new { Miles = d[0], Mins = d[1] });
}

【讨论】:

  • 谢谢老兄,它完美运行!我将public string[][] 更改为public string[],因为它给了我一个错误,所以你可能想在你的答案中为未来的读者改变它。我使用锯齿状数组的原因是因为我认为我会将每个项目绑定到列,就像你对单个数组所做的那样,我不知道你可以:/
  • @Code 哦,是的。我错过了那部分。修复它。很高兴它有帮助!
猜你喜欢
  • 2015-11-13
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
  • 2015-09-23
  • 2015-07-13
  • 2013-06-21
  • 2015-07-01
  • 2015-05-06
相关资源
最近更新 更多