簡體   English   中英

是否可以根據以下內容在應用程序啟動時添加彈出窗口?

[英]Is it possible to add a popup on application launch depending on the following?

我正在創建一個C#WPF應用程序,說實話,我並不是最好的應用程序。

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

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

這可能嗎?還是我不得不考慮一種替代設計?

目前,我在Window_loaded中:

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

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

有什么建議嗎?

提前致謝。

我建議您重寫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();
}

請記住,您應該從App.xaml文件中的Application元素中刪除StartupUri屬性。

CheckLimit函數將實例化實體框架的DbContextObjectContext並執行查詢,通常使用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

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM