在 ObservableCollection 中自动从数据库获取数据:WPF MVVM Caliburn Micro



我正在从事在WinForms中设计的数据管理项目,而不是我在WPF(MVVM)中重新设计它,我显然正在使用caliburnmicro来减少开发时间和灵活性,我遇到了一个抱团,我想在将数据保存到数据库后立即刷新数据网格。以下是我想要实现的目标的解释。

1)游戏模型:包含5个与数据库相同的属性(游戏ID,游戏名称,游戏类别,游戏公司,游戏价格)

private string _gameID;
private string _gameName;
private string _gameComp;
private string _gameCat;
private int _gamePrice;
public int GamePrice
{
get { return _gamePrice; }
set
{
_gamePrice = value;
NotifyOfPropertyChange(() => GamePrice);
}
}
public string GameCat
{
get { return _gameCat; }
set { _gameCat = value;
NotifyOfPropertyChange(() => GameCat);
}
}
public string GameComp
{
get { return _gameComp; }
set { _gameComp = value;
NotifyOfPropertyChange(() => GameComp);
}
}
public string GameName
{
get { return _gameName; }
set { _gameName = value;
NotifyOfPropertyChange(() => GameName);
}
}
public string GameID
{
get { return _gameID; }
set { _gameID = value;
NotifyOfPropertyChange(() => GameID);
}
}

2)游戏数据(DataAccessLayer):为了简单起见,包含将数据添加到数据库中的方法,我在这个例子中使用DataTable来获得答案。

DataTable dt;
public GameData()
{
dt = new DataTable("gameMaster");
DataColumn col1 = new DataColumn("Srno", typeof(int));
col1.AutoIncrement = true;
col1.AutoIncrementSeed = 1;
dt.Columns.Add(col1);
dt.Columns.Add("GameID", typeof(string));
dt.Columns.Add("GameName", typeof(string));
dt.Columns.Add("GameComp", typeof(string));
dt.Columns.Add("GameCat", typeof(string));
dt.Columns.Add("GamePrice", typeof(int));
}
public bool AddNewGame(GameModel gm)
{
try
{
dt.Rows.Add(null, gm.GameID, gm.GameName, gm.GameComp, gm.GameCat, gm.GamePrice);
return true;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return false;
}
}
public List<GameModel> GetAllGames()
{
var gameList = new List<GameModel>();
foreach(DataRow row in dt.Rows)
{
var game = new GameModel
{
GameID = row[1].ToString(),
GameName = row[2].ToString(),
GameComp= row[3].ToString(),
GameCat= row[4].ToString(),
GamePrice= (int)row[5],
};
gameList.Add(game);
}
return gameList;
}

3)GameViewModel:包含ObservableCollection,它从GameData:GetAllGames方法获取数据。

public GameData _gameData;
public GameModel _gameModel;
public GameViewModel()
{
GameCat.Add("Action");
GameCat.Add("Arcade");
GameCat.Add("Racing");
GameCat.Add("Puzzle");
GameCat.Add("Other");
GameComp.Add("EA");
GameComp.Add("EA Sports");
GameComp.Add("Ubisoft");
_gameData = new GameData();
RefreshGames();
}
private ObservableCollection<GameModel> _allGames;
public ObservableCollection<GameModel> AllGames
{
get { return _allGames; }
set
{
_allGames = value;
NotifyOfPropertyChange(() => AllGames);
}
}
public void AddNewGame(string gameID, string gameName, string gameCat, string gameComp, int gamePrice)
{
_gameModel = new GameModel
{
GameID = gameID,
GameName = gameName,
GameCat = gameCat,
GameComp = gameComp,
GamePrice = gamePrice
};
if (_gameData.AddNewGame(_gameModel))
{
RefreshGames();
MessageBox.Show("Game Added succesfully !");
}
}
public void RefreshGames()
{
var obj = new ObservableCollection<GameModel>(_gameData.GetAllGames());
AllGames = obj;
}

在这里,我必须使用另一种方法来刷新可观察集合,或者您可以说从数据库填充数据。我希望每当我添加任何数据时,都应该自动完成。如何让它知道任何插入都已完成。

作为参考,我分享代码视图的最后一部分如下:

<!--row1-->
<TextBox Grid.Row="1" Grid.Column="1" materialDesign:HintAssist.Hint="GameID" materialDesign:HintAssist.IsFloating="True" x:Name="GameID"/>
<!--row2-->
<TextBox Grid.Row="2" Grid.Column="1" x:Name="GameName"/>
<!--row3-->
<ComboBox Grid.Row="3" Grid.Column="1" x:Name="GameCat" SelectedIndex="0"/>
<!--row4-->
<ComboBox Grid.Row="4" Grid.Column="1" x:Name="GameComp" SelectedIndex="0"/>
<!--row5-->
<TextBox Grid.Row="5" Grid.Column="1" x:Name="GamePrice"/>
<!--row6-->
<StackPanel Orientation="Horizontal" Grid.Row="6" Grid.Column="1" HorizontalAlignment="Right">
<Button Content="Save" Width="60" x:Name="AddNewGame"/>
<Button Content="Exit" Width="60" x:Name="Exit"/>
</StackPanel>
<!--row7-->
<ListView Grid.Column="1" Grid.Row="7" ItemsSource="{Binding Path=AllGames}">
<ListView.View>
<GridView>
<GridViewColumn Header="GameID" Width="Auto" DisplayMemberBinding="{Binding Path=GameID}"/>
<GridViewColumn Header="GameName" Width="Auto" DisplayMemberBinding="{Binding Path=GameName}"/>
<GridViewColumn Header="Category" Width="Auto" DisplayMemberBinding="{Binding Path=GameComp}"/>
<GridViewColumn Header="Company" Width="Auto" DisplayMemberBinding="{Binding Path=GameCat}"/>
<GridViewColumn Header="Price" Width="Auto" DisplayMemberBinding="{Binding Path=GamePrice}"/>
</GridView>
</ListView.View>
</ListView>

绑定工作正常,数据显示正常,但我希望它自动完成,请让我知道如何在不调用 RefreshGames 方法的情况下执行此操作。

提前感谢您,如果这个问题很长,很抱歉,但为了理解,我不得不把所有东西都放进去。

如果有人想分享他们对代码的评论,请分享。

可观察集合会在您添加或删除条目时通知集合已更改。 你似乎在谈论

public ObservableCollection<GameModel> AllGames

它与AddNewGame处于同一视图模型中。 如果你只是这样做

AllGames.Add(_gameModel);

在AddNewGame中。
然后,您的列表视图会将新游戏显示为新行。

这里的假设是一个用户正在添加和查看游戏。
如果其他用户可以插入一个新游戏,并且您希望看到它出现,那么简单的解决方案是每隔几分钟轮询一次数据库。
如果您有很多用户并且有很多数据在更改,这是不切实际的,但这看起来不像是那种要求。

最新更新