gpt4 book ai didi

c# - c#如何区分对象类型

转载 作者:太空狗 更新时间:2023-10-30 01:14:29 26 4
gpt4 key购买 nike

我正在创建一个显示 HomeTeam 的篮球 WPF 应用程序和一个 AwayTeam . 我创建了一个 Player对象并在主窗口中创建了 ObservableCollection主队(HomePlayersList)和客队(AwayPlayersList)的球员对象。我用过INotifyPropertyChanged播放器对象上的接口(interface)所以当IsInGame bool 为 true 玩家被添加到两个 ObservableCollection<Player> 之一取决于计数。 (如果列表一个 ObservableCollection<Player> HomeTeamObservableCollection<Player> AwayTeam 计数为 5,则其余的将添加到替换列表 ObservableCollection<Team> HomeSubObservableCollection<Team> AwaySub。)

但我试图区分球员是在主队还是客队,并且根据球员在哪个名单,球员将被添加到新的主队或客队名单中。

public static ObservableCollection<Player> HomePlayersList;
public static ObservableCollection<Player> AwayPlayersList;
public static ObservableCollection<Player> HomeTeam = new ObservableCollection<Player>();
public static ObservableCollection<Player> AwayTeam = new ObservableCollection<Player>();
public static ObservableCollection<Player> HomeSub = new ObservableCollection<Player>();
public static ObservableCollection<Player> AwaySub = new ObservableCollection<Player>();
public static int HomeSubCount = 7;

public class Player: INotifyPropertyChanged
{
public static bool IsHome = true;
private static int TotalSelected = 1;
public string Id { get; set; } //player ID
public string FirstName { get; set; } //player first name
public string SecondName { get; set; } //player second name
public string KnownName { get; set; } //player known name
public string Position { get; set; } //player position
public string Number { get; set; } //player number

public bool isInGame;

public bool IsInGame
{
get { return isInGame; }

set
{
if (value != isInGame)
{
isInGame = value;

if (isInGame)
{
OnPropertyChanged("IsInGame", true);
}
else
{
OnPropertyChanged("IsInGame", false);
}
}
}
}

protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, e);
}

protected void OnPropertyChanged(string propertyName, bool state)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));

if (state)
{
if (TotalSelected > 5 + MainWindow.HomeSubCount)
{
this.IsInGame = false;
return;
}

if (MainWindow.HomeTeam.Count < 5)
MainWindow.HomeTeam.Add(this);
else
{
if (MainWindow.HomeSub.Count < MainWindow.HomeSubCount)
{
MainWindow.HomeSub.Add(this);
}
}
TotalSelected++;
}
else
{

if (SearchForMe(MainWindow.HomeTeam) != null)
{
MainWindow.HomeTeam.Remove(SearchForMe(MainWindow.HomeTeam));
TotalSelected--;
return;
}

if (SearchForMe(MainWindow.HomeSub) != null)
{
MainWindow.HomeSub.Remove(SearchForMe(MainWindow.HomeSub));
TotalSelected--;
return;
}
}
}

private Team SearchForMe(ObservableCollection<Team> OCT)
{
return OCT.Where(i => i.Number == this.Number).SingleOrDefault();
}

public event PropertyChangedEventHandler PropertyChanged;`enter code here`
}

最佳答案

在Player类中粘贴一个属性

ObservableCollection<Player> CurrnetIn { get; set; } 

并设置此属性,如果您将播放器添加到某个集合中。或者我误解了这个问题。

关于c# - c#如何区分对象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43275497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com