2014-12-27 3 views
0

저는 카드 덱 및 게임과 관련된 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"); 
     } 
    } 
+1

"게임"은 ObservableCollection입니까? – McGarnagle

+0

McGarnagle이 언급했듯이 "게임"은 ObservalbeCollection이어야합니다. – user2250152

+0

예. 이 정보를 포함하도록 질문을 편집했습니다. – oht

답변

0

나는 오류가합니다 (MSDN 샘플에서, 슬프지만 진실하고,) 코드에서이 가정합니다.

그냥이 코드를 보면 :

public DeckItem GameDeck 
{ 
    get { return _gameDeck.Entity; } 
    set 
    { 
     NotifyPropertyChanging("GameDeck"); 
     _gameDeck.Entity = value; 

     if (value != null) 
     { 
      _gameDeckId = value.DeckId; 
     } 

     NotifyPropertyChanging("GameDeck"); 
    } 
} 

당신은 GameDeck 재산 을 변경에 대한 것을 두 번에 통보,하지만 변경되지 않은 적이있다.

두 번째 NotifyPropertyChanging 메서드 호출은 NotifyPropertyChanged 메서드 호출로 바꿔야합니다.

+0

그것은 나의 눈을 또한 붙 잡았다. 나는 이것을 사용했지만 아무 쓸모가 없었다. 그러나 나는 그 시점에서 다소 엉망이되어서 효과가있을 수 있습니다. 다시 도전 해 볼게요. – oht

+0

다른 곳에서는 문제가있는 것으로 나타났습니다. 비록 이것이이 경우에는 문제가 아니었지만 당신은 여전히 ​​정확합니다. 그래서 저는 당신의 답을 옳다고 표시했습니다. – oht

관련 문제