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

时间:2017-03-28 16:47:40

标签: c# wpf

所以我提供了一个后端接口来从呼叫系统收集一些数据并将其显示到我正在构建的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)       
}

正如您在textBlock.Text中看到的那样,我希望能够将变量mylist放在Environment.NewLine旁边,但无法找到变量。

下面是.CS文件,如果需要了解这个列表,那么将从中提取列表。

通常,这将显示从下面未列出的界面提供的所有已登录代理的列表。我正在尝试这样做,以便每个代理都有一个包含下面的agent.cs文件中的数据字段的列。我可能正朝着完全错误的方向前进。任何可以提供的帮助将不胜感激。

public class Agent : IEquatable<Agent>
{
    public int Extension { get; set; }
    public int WorkModeDirection { get; set; }
    public string WorkModeDirectionDescription { get; set; }
    public TimeSpan AgTime { get; set; }
    public int AuxReason { get; set; }
    public string AuxReasonDescription { get; set; }
    public int DaInQueue { get; set; }
    public int WorkSkill { get; set; }
    public int OnHold { get; set; }
    public int Acd { get; set; }
    public String LoginId { get; set; }
    public string AgName { get; set; }
    public int EId { get; set; }
    public int Preference { get; set; }
    public DateTime DateTimeCreated { get; set; }
    public DateTime DateTimeUpdated { get; set; }
    public int CmId { get; set; }
    #region Implementation of IEquatable<Agent>

    public bool Equals(Agent other)
    {
        if (ReferenceEquals(null, other))
            return false;

        if (ReferenceEquals(this, other))
            return true;

        return (other.LoginId == LoginId & other.CmId == CmId);
    }

    #endregion

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
            return false;

        if (ReferenceEquals(this, obj))
            return true;

        if (obj.GetType() != typeof(Agent))
            return false;

        return Equals((Agent)obj);
    }

    //public override int GetHashCode()
    //{
    //    return LoginId;
    //}
    public override int GetHashCode()
    {
        string combinedNumber = "" + CmId + LoginId;
        int hash = Convert.ToInt32(combinedNumber);
        return hash;
    }

    public static bool operator ==(Agent left, Agent right)
    {
        return Equals(left, right);
    }

    public static bool operator !=(Agent left, Agent right)
    {
        return !Equals(left, right);
    }

    public override string ToString()
    {
        return " Ag: [Ext:" + Extension + " login:" + LoginId + " AgName:" + AgName + " CmId:" + CmId + "]";
    }
    public bool IsValid()
    {
        return LoginId != null;
    }
}

1 个答案:

答案 0 :(得分:0)

您在myList循环中声明了foreach 。当你想设置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);

或使用LINQ:

// no extra list and foreach needed
textBlock.Text = string.Join(Environment.NewLine, 
                             e.CmsData.Agents.Select(item => item.AuxReasonDescription));

(仅当e.CmsData.Agents实施IEnumerable<Agent>时才有效。如果它只实现了非通用IEnumerable,则您必须添加e.CmsData.Agents.OfType<Agent>.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;                   
    }
}
相关问题