Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要帮助将变量的输出格式化为列表吗_C#_Wpf - Fatal编程技术网

C# 需要帮助将变量的输出格式化为列表吗

C# 需要帮助将变量的输出格式化为列表吗,c#,wpf,C#,Wpf,所以我得到了一个后端接口,可以从呼叫系统收集一些数据,并将其显示到我正在构建的GUI中。我在这方面是新手,并将其作为一个学习项目。我试图将一些数据过滤到一个列表中,而不是一个巨大的字符串。下面是我用来在文本块中显示数据的代码 public void OnMessageReceived(object sender, MessageReceivedEventArgs e) { try { if (e == null) return;

所以我得到了一个后端接口,可以从呼叫系统收集一些数据,并将其显示到我正在构建的GUI中。我在这方面是新手,并将其作为一个学习项目。我试图将一些数据过滤到一个列表中,而不是一个巨大的字符串。下面是我用来在文本块中显示数据的代码

public void OnMessageReceived(object sender, MessageReceivedEventArgs e)
{
    try
    {
        if (e == null)
            return;

        if (e.CmsData != null)
        {
            Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
            {
                foreach (var item in e.CmsData.Agents)
                {
                    List<string> mylist = new List<string>();
                    mylist.Add(item.AuxReasonDescription);

                }
                textBlock.Text = string.Join(Environment.NewLine, e.CmsData.Agents); }));                   
        }               
    }
    catch (Exception ex)       
}

您在
foreach
循环中声明了
myList
。这就是为什么当您要设置
textBlock.Text
时,编译器不再知道它的原因。更糟糕的是:因此每次迭代都会创建一个新列表

只需将声明移到
foreach
之外:

List<string> mylist = new List<string>();
foreach (var item in e.CmsData.Agents)
{
    mylist.Add(item.AuxReasonDescription);
}
textBlock.Text = string.Join(Environment.NewLine, myList);
(这仅在
e.CmsData.Agents
实现
IEnumerable
时有效。如果它仅实现非泛型
IEnumerable
则必须添加
e.CmsData.Agents.OfType.Select…


但是,在消息处理程序中(以及在接收消息的线程上)构建字符串可能比在UI线程上构建字符串更好:

if (e.CmsData != null)
{
    string text = string.Join(Environment.NewLine, 
                             e.CmsData.Agents.Select(item => item.AuxReasonDescription));
    Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
    {
        textBlock.Text = text;                   
    }
}

再加上1,你应该在投票否决这个问题之前至少发表评论。这太棒了。事实上,我坚持我原来的格式。很高兴这是一个简单的错误,我只是没有赶上。我最近好像做了很多。我想使用string.format将其格式化为数据列,而不是将其转储。。你能很容易地帮我吗?还是我应该发布一个新问题?@mcavanaugh418这听起来像是一个新问题,我已经离开办公室(现在用手机写信),所以我在接下来的12小时内不会回答;)很高兴我能帮忙。如果它解决了这个问题,您可以将此答案标记为已接受。
// no extra list and foreach needed
textBlock.Text = string.Join(Environment.NewLine, 
                             e.CmsData.Agents.Select(item => item.AuxReasonDescription));
if (e.CmsData != null)
{
    string text = string.Join(Environment.NewLine, 
                             e.CmsData.Agents.Select(item => item.AuxReasonDescription));
    Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
    {
        textBlock.Text = text;                   
    }
}