2011-08-23 57 views
0
public class Teams : INotifyPropertyChanged 
    { 
     public string CombinedTeams 
     { 
      get 
      {     
       return Combined; 
      } 

      set 
      {      
       {            
        CombinedTeams += value; 
        NotifiyPropertyChanged("Combined"); 
       } 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void NotifiyPropertyChanged(string p) 
     { 
      if (null != p) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(p)); 
      } 
     } 
     private string Combined 
     { 

      get 
      { 
       return " " + HomeTeam + " " + HomeScore + " - " + AwayScore + " " + AwayTeam; 
      } 
      set 
      { 
       { 
        Combined += value; 
       } 
      } 
     } 
     public string HomeTeam { get; set; } 
     public string AwayTeam { get; set; } 
     public string HomeScore { get; set; } 
     public string AwayScore { get; set; } 
    } 

我有一個問題,想我的琴絃結合在一起,具有包含當我分析我的XML中的所有值一個很長的字符串時,我只得到了第一組值,getter和setter,越來越多領域

基本上我得到

Team1 Score1 : Score2 Team2

,而不是 Team1 Score1 : Score2 Team2 Team3 Score3 : Score4 Team4 Team5 Score5 : Score6 Team6

我綁定我的控制到CombinedTeams

你們能幫我嗎?我只是想保存以前的字符串,然後用舊的結合新的字符串,我不能看到它是很難,但這是困惑我,讀了它讓我更糊塗了......

謝謝,

約翰

+0

我編輯了以上,但我還在掙扎,我不能用我的頭周圍的getter和setter和inotifiedproperty –

+1

另一個小紙條的人INotifyPropertyChanged的。在設置實際值之前,您不應該調用this.NotifyPropertyChanged(propertyName)。您已經在通知並更新Combined。 – invalidusername

回答

1

你所得到的結果不正確的原因是因爲你有一個屬性引用另一個屬性,第二個屬性總是返回一個特定的值。

的代碼,從其他地方調用時該塊,會返回一些所謂的「組合拳」等變量,你下面定義的結果...

public string CombinedTeams      
{      
    get      
    {           
     return Combined;      
    }      
    ... 
}   

private string Combined       
{       
    get       
    {       
     return " " + HomeTeam + " " + HomeScore + " - " + AwayScore + " " + AwayTeam;       
    } 
    ... 
} 

其他的都是學術,因爲你消氣(s)基本上總是返回「」+ HomeTeam +「」+ HomeScore +「 - 」+ AwayScore +「」+ AwayTeam。

我懷疑你將要調整你的代碼,以更多的東西像這樣

public class Teams : INotifyPropertyChanged 
{ 
    private string Combined; // Backing for CombinedTeams 
    public string CombinedTeams 
    { 
     get 
     { 
      return Combined; 
     } 
     set 
     { 
      // This only concatinates values; Combined will get longer each time. 
      Combined += value; 
      // ViewModels should always notify after the vale has changed 
      NotifyOfPropertyChange("CombinedTeams"); 
     } 
    } 

    // Adds a new team, assuming HomeTeam, HomeScore, AwayScore, and AwayTeam have been initialized 
    public void AddTeam() 
    { 
     CombinedTeams = " " + HomeTeam + " " + HomeScore + " - " + AwayScore + " " + AwayTeam;    
    }      
} 

當然有更好的方法來做到這一點,但應該讓你開始,我希望。

一般規則(由代碼忍者一直打破,這很好)是屬性不應該對它自己進行任何計算,它實際上允許公共訪問類中的私有數據。

在C#Properties中運行幾篇文章可能是值得的。這裏有一些建議,讓你開始:http://msdn.microsoft.com/en-us/library/x9fsa0sw(v=vs.80).aspxhttp://msdn.microsoft.com/en-us/library/aa288470(v=vs.71).aspx,當然,一些Good Search Results

+0

EtherDragon我真的很感謝你的時間來解釋和概述這一點,謝謝 –

+0

NP,很高興做到這一點,很高興它幫助。 – EtherDragon

2

您的代碼將新值連接到空字符串(last = "")。
您可能想要連接到以前的值。

+0

我將如何存儲最後一個值? –

+1

只需閱讀財產。 – SLaks

2

我不確定你在期待什麼,last總是被初始化爲「」,所以+ =無關緊要。

看起來像名爲團隊的類真的是一個遊戲?

我不認爲一次又一次設置HomeTeam,AwayTeam,HomeScore和AwayScore(然後以某種方式內部保存此內容)是跟蹤多個遊戲的好方法。

你爲什麼不看看使用一系列遊戲?

嘗試這樣:

在GamesLib庫:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ComponentModel; 

namespace GamesLib 
{ 
    public class Game 
    { 
     public string HomeTeam { get; private set; } 
     public string AwayTeam { get; private set; } 
     public string HomeScore { get; private set; } 
     public string AwayScore { get; private set; } 

     public string Combined 
     { 
      get 
      { 
       return " " + HomeTeam + " " + HomeScore + " - " + AwayScore + " " + AwayTeam; 
      } 
     } 

     public Game(string HomeTeam, string AwayTeam, string HomeScore, string AwayScore) 
     { 
      this.HomeTeam = HomeTeam; 
      this.HomeScore = HomeScore; 
      this.AwayTeam = AwayTeam; 
      this.AwayScore = AwayScore; 
     } 
    } 

    public class Games : List<Game>, INotifyPropertyChanged 
    { 
     public string CombinedTeams 
     { 
      get 
      { 
       var str = ""; 
       foreach (Game g in this) 
       { 
        str += g.Combined; 
       } 
       return str; 
      } 
     } 

     public new void Add(Game g) 
     { 
      base.Add(g); 
      if (PropertyChanged != null) { 
       PropertyChanged(this, new PropertyChangedEventArgs("CombinedTeams")); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
    } 
} 

在控制檯程序:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using GamesLib; 

namespace TestHarness 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var gs = new GamesLib.Games(); 
      gs.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(gs_PropertyChanged); 
      var g = new Game("hometeam", "awayteam", "1", "0"); 
      gs.Add(g); 
      g = new Game("lions", "bears", "1", "0"); 
      gs.Add(g); 
      Console.WriteLine("Final result:" + gs.CombinedTeams); 
     } 

     static void gs_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
     { 
      var gs = sender as Games; 
      Console.WriteLine("Changed: " + gs.CombinedTeams); 
     } 
    } 
} 
+0

我改變了代碼,但我仍然沒有得到它,我想我只是沒有看到我一直在尋找這個愚蠢的事情,像2小時的問題 –

+1

@John Antony Daniel Nolan我已經爲你發佈了一個完整的例子。 –

+0

謝謝凱德,我無法感謝你! –