简体   繁体   中英

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

I am creating a C# WPF application and I'm not the best with it to be honest.

I'm wondering if it's possible to do the following.

When the application launches I want it to automatically check a table in a local database and the table is empty, create a pop up that prompts the user to insert the values it needs to fill those rows?

Is this possible or am I going to have to think of an alternative design?

At the moment I have in my Window_loaded:

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

where limit.UL and limit.LL are columns in my table, but not having any luck due to them not being objects.

Any advice?

Thanks in advance.

I would recommend to you to override the Application.OnStartup method in your App.xaml.cs file. In there, I would perform the check whether your data exists. If it does not, I would create your LimitWindow and show it as a dialog. After that the applications main window can be initialized and shown. The code would look somewhat like this:

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();
}

Keep in mind that you should remove the StartupUri attribute from the Application element in the App.xaml file.

The CheckLimit function would instantiate the DbContext or the ObjectContext of entity framework and perform the query, normally using LINQ:

private Limit CheckLimit()
{
    // Create the context and perform the query
    var dbContext = new ClassDerivedFromDbContext();
    var limit = dbContext.Limits.FirstOrDefault();
    dbContext.Close();
    return limit;
}

As I don't know how your entity framework classes look like, you would have to implement CheckLimit by yourself.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM