Getters and Setters,获得多个领域

时间:2011-08-23 20:08:56

标签: c# windows-phone-7 getter-setter

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; }
    }

我遇到了一个问题,当我尝试将我的字符串组合在一起并且有一个LONG字符串包含解析XML时的所有值时,我只得到第一组值,

基本上我得到了

Team1 Score1 : Score2 Team2

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

我将我的控件绑定到CombinedTeams

你能帮助我吗?我只想存储前一个字符串,然后将新字符串与旧字符串结合起来,我看不出它很难但是这让我感到困惑并且阅读它让我更加困惑......

谢谢,

约翰

3 个答案:

答案 0 :(得分:2)

您的代码将新值连接到空字符串(last = "") 您可能希望连接到之前的值。

答案 1 :(得分:2)

我不确定您的期望,last始终初始化为“”,因此+ =无关紧要。

好像名为Teams的班级真的是游戏吗?

我不认为一次又一次地设置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);
        }
    }
}

答案 2 :(得分:1)

您获得错误结果的原因是因为您有一个属性引用另一个属性,而第二个属性始终返回特定值。

当从其他地方调用此代码块时,将返回您在下面定义的名为“Combined”的其他变量的结果...

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

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

其他一切都是学术性的,因为你的得主基本上总是返回“”+ 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;              
    }                     
}

当然有更好的方法可以做到这一点,但我希望这可以帮助你。

一般规则(由code-ninjas一直打破,这很好)是一个Property不应该对它自己做任何计算,它实际上允许公共访问类中的私有数据。

通过一些关于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