我建議你在你的App.xaml.cs文件中覆蓋Application.OnStartup
方法。在那裏,我會執行檢查你的數據是否存在。如果沒有,我會創建您的LimitWindow
並將其顯示爲對話框。 之後,可以初始化並顯示應用程序主窗口。該代碼看起來有點像這樣:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Check if the database needs to be updated; if yes, show the corresponding window as a dialog
var limit = CheckLimit();
if (limit.UL == null && limit.LL == null)
{
var limitWindow = new LimitWindow();
var dialogResult = limitWindow.ShowDialog();
if (dialogResult)
{
// Update your database here
}
}
// Show the actual main window of the application after the check
this.MainWindow = new MainWindow();
this.MainWindow.Show();
}
請記住,你應該刪除從Application
元素StartupUri
屬性在App.xaml文件。
的CheckLimit
函數將實例化DbContext
或實體框架的ObjectContext
和執行查詢,通常使用LINQ:
private Limit CheckLimit()
{
// Create the context and perform the query
var dbContext = new ClassDerivedFromDbContext();
var limit = dbContext.Limits.FirstOrDefault();
dbContext.Close();
return limit;
}
由於我不知道你的實體框架類看起來怎麼樣,你會自己實施CheckLimit
。
希望這會有所幫助。
確定這是可能的。在數據庫上激發一個select語句,讓窗口彈出或不彈出。 ; o)在你的文章的其餘部分,你必須提供更多的細節。數據如何加載a.s.o .... – DHN 2013-04-26 14:59:41
@DHN最後的評論可能沒有意義。據我所知,它通過使用ADO.NET實體框架進行連接。 – user2278479 2013-04-26 15:53:15