저는 카드 덱 및 게임과 관련된 Windows Phone 앱을 만들고 있습니다. 이 예제를 모두 기반으로합니다 How to create a local database app with MVVM for Windows Phone 8. 꽤 명백한 것처럼 갑판 하나는 몇 가지 게임을 할 수 있습니다. 그러므로 저는 Deck이 "Category"이고 게임이 예제에서 "Todo"인 것처럼 그것을 구축했습니다. Model과 ViewModel의 특정 코드가 첨부됩니다.Linq가 새 항목을 추가 할 때 관련 항목을 업데이트하지 않습니다.
새 게임을 추가하면 게임이 속한 덱을 가져옵니다. 이것은 잘 작동합니다.
문제는 갑판 테이블의 DeckGame 컬렉션이 새로운 멤버를 수신했음을 알지 못하므로 모든 게임 데크 정보가 포함 된 페이지로 돌아올 때 새로운 게임이 존재하지 않는다는 것입니다.
간단히 말해서 게임은 그것이 속한 덱을 알고 있지만 덱은 새로운 게임을 가지고 있다는 것을 모릅니다.
앱을 다시 시작하면 모든 새로운 게임이 표시됩니다.
이것은 내가 믿는 제 코드의 일부입니다.
새로운 게임이 등록되면이 메소드가 추가됩니다 (ViewModel).
public void AddGame(GameItem newGameItem)
{
// Add a to-do item to the data context.
_Db.Games.InsertOnSubmit(newGameItem);
// Save changes to the database.
_Db.SubmitChanges();
// Add a to-do item to the "all" observable collection.
Games.Add(newGameItem);
}
이
은 데크 - 게임 관계// Internal column for the associated Deck ID value
[Column]
internal int _gameDeckId;
// Entity reference, to identify the Deck "storage" table
private EntityRef<DeckItem> _gameDeck;
// Association, to describe the relationship between this key and that "storage" table
[Association(Storage = "_gameDeck", ThisKey = "_gameDeckId", OtherKey = "DeckId", IsForeignKey = true)]
public DeckItem GameDeck
{
get { return _gameDeck.Entity; }
set
{
NotifyPropertyChanging("GameDeck");
_gameDeck.Entity = value;
if (value != null)
{
_gameDeckId = value.DeckId;
}
NotifyPropertyChanging("GameDeck");
}
}
의 게임 부분입니다 그리고 이것은 어떤 도움이 평가의 관계
// Define the entity set for the collection side of the relationship.
private EntitySet<GameItem> _deckGames;
[Association(Storage = "_deckGames", OtherKey = "_gameDeckId", ThisKey = "DeckId")]
public EntitySet<GameItem> DeckGames
{
get { return this._deckGames; }
set { this._deckGames.Assign(value); }
}
// Assign handlers for the add and remove operations, respectively.
public DeckItem()
{
_deckGames = new EntitySet<GameItem>(
new Action<GameItem>(this.attach_Game),
new Action<GameItem>(this.detach_Game)
);
}
// Called during an add operation
private void attach_Game(GameItem game)
{
NotifyPropertyChanging("GameItem");
game.GameDeck = this;
}
// Called during a remove operation
private void detach_Game(GameItem game)
{
NotifyPropertyChanging("GameItem");
game.GameDeck = null;
}
의 데크 부분에 주저하지 말고 자세한 내용이나 코드를 요청하십시오!
편집 2015년 1월 1일
// All Game items.
private ObservableCollection<GameItem> _games;
public ObservableCollection<GameItem> Games
{
get { return _games; }
set
{
_games = value;
NotifyPropertyChanged("Games");
}
}
"게임"은 ObservableCollection입니까? – McGarnagle
McGarnagle이 언급했듯이 "게임"은 ObservalbeCollection이어야합니다. – user2250152
예. 이 정보를 포함하도록 질문을 편집했습니다. – oht