diff --git a/imagecatalog/Program.cs b/imagecatalog/Program.cs index b32c760..ba559a4 100644 --- a/imagecatalog/Program.cs +++ b/imagecatalog/Program.cs @@ -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(); - //// If you want to set the DataModel explicitly on the WinForms form use the lines below - //// var mainViewModel = serviceProvider.GetRequiredService(); - //// 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(); System.Windows.Forms.Application.Run(mainForm); } diff --git a/imagecatalog/Properties/launchSettings.json b/imagecatalog/Properties/launchSettings.json index 641af8b..12da6a8 100644 --- a/imagecatalog/Properties/launchSettings.json +++ b/imagecatalog/Properties/launchSettings.json @@ -1,7 +1,12 @@ { "profiles": { - "Profile 1": { - "commandName": "Project" + "ImageCatalog (WinForms)": { + "commandName": "Project", + "commandLineArgs": "" + }, + "ImageCatalog (WPF)": { + "commandName": "Project", + "commandLineArgs": "--wpf" } } -} \ No newline at end of file +}