2013-04-26 36 views
0

我正在創建一個C#WPF應用程序,並且我不是老實說最好的。根據以下情況,是否可以在應用程序啓動時添加彈出窗口?

我想知道是否有可能做到以下幾點。

當應用程序啓動時,我希望它自動檢查本地數據庫中的表,並且表爲空,創建一個彈出窗口,提示用戶插入填充這些行所需的值?

這是可能的還是我將不得不考慮替代設計?

目前,我有我的Window_loaded:

if (limit.UL == null && limit.LL == null) 
{ 
    limitWindow = new LimitWindow(); 
} 

其中limit.UL和limit.LL都在我的表中的列,但沒有任何運氣,由於它們不是對象。

有什麼建議嗎?

在此先感謝。

+0

確定這是可能的。在數據庫上激發一個select語句,讓窗口彈出或不彈出。 ; o)在你的文章的其餘部分,你必須提供更多的細節。數據如何加載a.s.o .... – DHN 2013-04-26 14:59:41

+0

@DHN最後的評論可能沒有意義。據我所知,它通過使用ADO.NET實體框架進行連接。 – user2278479 2013-04-26 15:53:15

回答

0

我建議你在你的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

希望這會有所幫助。

相關問題