2011-08-23 33 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

谢谢凯德,我无法感谢你! –