2016-01-22 101 views
0

我現在在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; 
     } 

或其他解決方案可以使用嗎?

謝謝。 (對不起英語不好)

+0

使其更加簡單,難以使用。 –

回答

0

你的遊戲已經從GameBase繼承,所以你應該可以使用正常的多態。因此,而不是你的currGame類型的「對象」它可以是「GameBase」類型。

GameBase currGame; 
switch (_gameType) 
    { 
     case "Game1": 
      currGame = Games.Game1s[_gameIndex]; 
      currentPerson = currGame.Persons.ElementAt(persondindex); 
      break; 
     case "Game2": 
      currGame = Games.Games2[_gameIndex]; 
      currentPerson = currGame.Persons.ElementAt(persondindex); 
      break; 
     default: 
      break; 
    } 

編輯: 你不想投給一個接口,因爲它是多餘的。 但是,您可能可以將您的GameBase更改爲:

public abstract class GameBase 
{ 
    public string GameStatus { get; set; } 
    public string GameDuration { get; set; } 
    public ObservableCollection<Person> Persons { get; set; } 
} 
+0

嗨克里斯,感謝您的回答,我已經嘗試過,所以,但後來我有Ambiguos ObservableCollection人..我不知道我的心靈尋找泛型和接口...唯一我會有一個GameBase,然後從基地派生GameA,GameB,每個派生的遊戲必須有一個PersonList與特定的人類型,我的目標是有一個對象代表我一個人名單的遊戲,我可以施放,所以我可以調用CurrGame.Persons(沒有知道CUrrGame是GameA還是GameB,我可以做到這一點嗎?無需泛型和接口...或僅使用接口?謝謝。 –