Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a WPF app from loading?

Tags:

c#

.net

wpf

startup

I want that the WPF application starts only in certain conditions. I tried the following with no success:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        if (ConditionIsMet) // pseudo-code
        {
            base.OnStartup(e);
        }
    }
}

But the app runs normally even when the condition is not met

like image 288
Jader Dias Avatar asked Sep 15 '10 16:09

Jader Dias


People also ask

Can WPF application run IOS?

NET 5 WPF application support in IOS and Android. . NET 5 is s unified platform to write code once and run anywhere.

Can WPF applications be built without using XML?

But WPF without XAML is possible. You don't even need Visual Studio installed for this, just the . NET CLI and the Developer Command Prompt. Drop these two files in a folder and run msbuild and then you can run the file in the Bin directory.

Can WPF be targeted to Web Browser Yes or no?

WPF only runs on windows. You can make a type of wpf application called xbap which runs in a browser.


2 Answers

Try this:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    if (MyCondition)
    {
        ShowSomeDialog("Hey, I Can't start because...");
        this.Shutdown();
    }
}
like image 197
Muad'Dib Avatar answered Oct 04 '22 20:10

Muad'Dib


Another solution

Designer

<Application x:Class="SingleInstanceWPF.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Startup="Application_Startup">
</Application>

Code behind

public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        if (ConditionIsMet)
        {
            var window = new MainWindow();
            window.Show();
        }
        else
        {
            this.Shutdown();
        }
    }
}
like image 26
Jader Dias Avatar answered Oct 04 '22 19:10

Jader Dias