我有一个名为str的数组列表,我想查看该数组列表中的元素,我应该如何使用消息框来显示它?
P.S。显示元素的第一个字段也很好
示例代码(我创建的用于从api检索数据的方法:
void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
Stream responseStream = e.Result;
StreamReader responseReader = new StreamReader(responseStream);
string response = responseReader.ReadToEnd();
string[] split1 = Regex.Split(response, "},{");
List<string> pri1 = new List<string>(split1);
pri1.RemoveAt(0);
string last = pri1[pri1.Count() - 1];
pri1.Remove(last);
str = pri1;
}
}
答案 0 :(得分:2)
显示逗号分隔值:
var list = new List<string> { "tes1", "test2", "test3" };
var message = string.Join(",", list);
MessageBox.Show(message);
<强> [更新] 强>
您可以在代码中进行一些增强:
private void button1_Click(object sender, RoutedEventArgs e)
{
var response = "asdf},{asaaa},{shf";
var split = Regex.Split(response, "},{");
var elements = split
.Skip(1)
.Take(split.Length - 2)
.ToList();
var message = string.Join(",", elements);
MessageBox.Show(message);
}
答案 1 :(得分:1)
MessageBox.Show(String.Join(“”,str)但是你真的确定你想要这个吗?我的意思是在Silverlight ChildWindow中显示的ItemsControl中显示该列表会不会更有吸引力?
<ChildWindow>
<ItemsControl ItemsSource="{Binding str}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl
</ChildWindow>
答案 2 :(得分:1)
我通常做这样的事情:
public void PrintList(IList<object> list)
{
string printString = "List Elements";
foreach (object o in list)
{
// Add the fields you want to show here
printString += "\n" + o.ToString();
}
MessageBox.Show(printString);
}
对于您的实施,请将object
替换为存储您从API中检索过的信息的类,并将要查看的字段添加到printString += ...
行。
答案 3 :(得分:0)
Linq为所需格式增加了灵活性:
var list = new List<string>{"one","two", "three"};
var consolidatedList = list.Aggregate((s1,s2) => String.Format("{0} {1}",s1,s2));
//consolidatedList = "one two three"
MessageBox.Show(consolidatedList)