Linq不会更新添加新项目的相关项目

时间:2014-12-27 10:06:36

标签: c# linq mvvm windows-phone

我正在构建一个与卡片组和游戏有关的Windows Phone应用程序。我已经将这一切都基于这个例子How to create a local database app with MVVM for Windows Phone 8。很明显,一个Deck可以玩几个游戏。因此,我已经建立了它,以便甲板是"类别"而游戏就是" Todo"从例子中。附加了Model和ViewModel的特定代码。

当我添加一个新游戏时,它会获得它所属的Deck。这很好。

问题是Deck表中的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);
    }

这是Deck-Game关系的游戏部分

// 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-01-01

    // All Game items.
    private ObservableCollection<GameItem> _games;
    public ObservableCollection<GameItem> Games
    {
        get { return _games; }
        set
        {
            _games = value;
            NotifyPropertyChanged("Games");
        }
    }

1 个答案:

答案 0 :(得分: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方法调用。