2017-03-28 65 views
0

因此我提供了一個後端接口來從呼叫系統收集一些數據並將其顯示到我正在構建的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; 
    } 
} 
+0

加1 ,在投票之前,您應該至少發表評論,然後投票拒絕投票。 – tabby

回答

0

你宣佈myListforeach循環。這就是爲什麼當你想設置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;     
    } 
} 
+0

這真棒。我實際上堅持我的原始格式。很高興這是一個容易犯的錯誤,我只是沒有抓到。我似乎最近在做很多這些。我想使用string.format將其格式化爲數據列,而不是隻是傾銷它..你能夠很容易地幫助或者我應該發佈一個新的問題嗎? – mcavanaugh418

+0

@ mcavanaugh418這聽起來更像是一個新問題,我已經離開了辦公室(現在從我的手機上寫下來),所以在接下來的12個小時內我不會回答;)我很高興能夠提供幫助。如果它解決了這個問題,你可以將這個答案標記爲已接受。 –