我現在在VS 2013從接口創建通用對象?
編程爲Win Phone 8的,我有一個遊戲類有2個觀察集合:
public class Games
{
public Games() { }
public ObservableCollection<Game1> Games1 { get; set; }
public ObservableCollection<Game2> Games2 { get; set; }
}
遊戲1和遊戲2類:
public class Game1: GameBase<PersonG1>
{
public Game1(){}
public string Game1Property{ get; set; }
}
public class Game2: GameBase<PersonG2>
{
public Game2(){}
public string Game2Property{ get; set; }
}
GameBaseClass :
public abstract class GameBase<TPerson> where TPerson : Person
{
public string GameStatus { get; set; }
public string GameDuration { get; set; }
public ObservableCollection<TPerson> Persons { get; set; }
}
Person Classes:
public class Person
{
public string Name { get; set; }
}
public class PersonG1: Person
{
public string PersonG1Property{ get; set; }
}
public class PersonG2: Person
{
public string Person2Property{ get; set; }
}
啓動後,手機應用程序,我會選擇一個遊戲,我去一個XAML頁面,在這裏的DataContext =的Game1或GAME2
但我有一個頁面:MakePerson.xaml,該頁面不瞭解DataContext的或任何東西,我用它來增加人員每場比賽,並會使用相同的MakePerson.xaml的所有遊戲,我去Makeperson.xaml頁面如下:
NavigationService.Navigate(new Uri(GlobalResources.MakePersonPage + "?index=" + _gameIndex.ToString() + "&pId=-1&gametype=Game1", UriKind.Relative));
其中_gameIndex代表指數對於ObservableCollection中的遊戲,pID表示ObservableCollection Persons中的personIndex,和GT代表遊戲類型,到達Makeperson.xaml頁我做的:
private object currGame;
private Person currPerson;
DataContext = currentPerson;
_gameIndex = int.Parse(NavigationContext.QueryString["index"]);
_personIndex = int.Parse(NavigationContext.QueryString["pId"]);
_gameType = NavigationContext.QueryString["gametype"];
switch (_gameType)
{
case "Game1":
{
currGame = Games.Games1[gameIndex];
currPerson = Games1[gameindex].Persons.ElementAt(personindex);
}
case "Game2":
currGame = Games.Games2[gameIndex]
currPerson = Games2[gameindex].Persons.ElementAt(personindex);
}
我怎樣才能改變這種結構,所以我可以在makePerson.xaml更舒適?我想通用接口?
如何我必須改變我的課,所以我一直在同一個對象,例如可能是這樣的:
switch (_gameType)
{
case "Game1":
IGame currGame = Games.Game1s[_gameIndex];
currentPerson = currGame.Persons.ElementAt(persondindex);
break;
case "Game2":
IGame currGame = Games.Games2[_gameIndex];
currentPerson = currGame.Persons.ElementAt(persondindex);
break;
default:
break;
}
或其他解決方案可以使用嗎?
謝謝。 (對不起英語不好)
使其更加簡單,難以使用。 –