Support UI selection via --wpf command-line argument

Refactored Program.cs to accept command-line arguments and select between WinForms (default) and WPF UI based on the presence of --wpf. Removed old commented-out UI selection code. Updated launchSettings.json to provide separate profiles for WinForms and WPF launches. If WPF is requested but unavailable, the app falls back to WinForms.
This commit is contained in:
MaddoScientisto 2026-02-21 17:27:16 +01:00
commit d068b4b3e1
2 changed files with 27 additions and 26 deletions

View file

@ -57,7 +57,7 @@ static class Program
public static IServiceProvider ServiceProvider { get; private set; }
[STAThread]
static void Main()
static void Main(string[] args)
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
@ -74,21 +74,14 @@ static class Program
// Resolve WPF MainWindow when available, otherwise fall back to WinForms MainForm
var serviceProvider = ServiceProvider;
// NOTE: By default the app will attempt to run the WPF window when available and fall back
// to the WinForms MainForm. If you want to force the WinForms UI for testing, uncomment
// the block below and comment out the WPF branch.
// -----------------------------------------------------------------------------
// // Force WinForms UI (uncomment to enable)
//var mainForm = serviceProvider.GetRequiredService<MainForm>();
//// If you want to set the DataModel explicitly on the WinForms form use the lines below
//// var mainViewModel = serviceProvider.GetRequiredService<DataModel>();
//// mainForm.Model = mainViewModel;
//Application.Run(mainForm);
// -----------------------------------------------------------------------------
// Determine UI based on command line. Default: WinForms. Use --wpf to explicitly request WPF.
bool useWpf = args is not null && Array.Exists(args, a => string.Equals(a, "--wpf", StringComparison.OrdinalIgnoreCase));
// Create the WPF Application and merge MahApps resources BEFORE constructing the MainWindow
// so InitializeComponent sees the theme resources on first render.
var wpfApp = new System.Windows.Application();
if (useWpf)
{
// Create the WPF Application and merge MahApps resources BEFORE constructing the MainWindow
// so InitializeComponent sees the theme resources on first render.
var wpfApp = new System.Windows.Application();
try
{
wpfApp.Resources.MergedDictionaries.Add(new System.Windows.ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml") });
@ -111,15 +104,18 @@ static class Program
// If resources fail to load (package not present at runtime), continue silently
}
// Now resolve the WPF MainWindow so its constructor runs with the application resources available
var wpfMain = serviceProvider.GetService(typeof(ImageCatalog_2.MainWindow)) as ImageCatalog_2.MainWindow;
if (wpfMain is not null)
{
wpfApp.Run(wpfMain);
return;
// Now resolve the WPF MainWindow so its constructor runs with the application resources available
var wpfMain = serviceProvider.GetService(typeof(ImageCatalog_2.MainWindow)) as ImageCatalog_2.MainWindow;
if (wpfMain is not null)
{
wpfApp.Run(wpfMain);
return;
}
// If WPF was requested but not available, fall back to WinForms.
}
// Fallback to WinForms UI
// Default / fallback to WinForms UI
var mainForm = serviceProvider.GetRequiredService<MainForm>();
System.Windows.Forms.Application.Run(mainForm);
}

View file

@ -1,7 +1,12 @@
{
"profiles": {
"Profile 1": {
"commandName": "Project"
"ImageCatalog (WinForms)": {
"commandName": "Project",
"commandLineArgs": ""
},
"ImageCatalog (WPF)": {
"commandName": "Project",
"commandLineArgs": "--wpf"
}
}
}