Cross-platform: remove System.Drawing deps, add #if WINDOWS

Refactored image creation APIs to use byte[] for logo data instead of System.Drawing.Image, enabling cross-platform support. Wrapped all GDI+/Windows-specific code in #if WINDOWS and updated project files to conditionally include Windows-only dependencies. Defaulted to ImageSharp on non-Windows, and updated UI and settings to reflect platform capabilities. Application now builds and runs on Linux/macOS with Avalonia and ImageSharp, while retaining full Windows functionality.
This commit is contained in:
MaddoScientisto 2026-02-26 19:17:23 +01:00
commit 73597689ed
16 changed files with 115 additions and 90 deletions

View file

@ -1,9 +1,8 @@
using System.Drawing;
using System.Threading.Tasks;
namespace MaddoShared;
public interface IImageCreator
{
Task CreateImageAsync(ImageState imgState, Image logo);
Task CreateImageAsync(ImageState imgState, byte[]? logoData);
}

View file

@ -3,7 +3,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
@ -14,7 +13,6 @@ using Microsoft.Extensions.Logging;
namespace MaddoShared
{
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
public class ImageCreationService(
ILogger<ImageCreationService> logger,
PicSettings picSettings,
@ -72,19 +70,15 @@ namespace MaddoShared
// int threads = options.MaxThreads == 0 ? Environment.ProcessorCount * 2 : options.MaxThreads;
int threads = options.MaxThreads;
Bitmap logoBmp = null;
// Load Logo (short-circuit)
// Load logo once as raw bytes (cross-platform). byte[] is safe to share across threads.
byte[]? logoBytes = null;
if (picSettings.LogoAggiungi && File.Exists(picSettings.LogoNomeFile))
{
logoBmp = new Bitmap(picSettings.LogoNomeFile);
logoBytes = File.ReadAllBytes(picSettings.LogoNomeFile);
}
Func<FileData, Task> processFile = async fileData =>
{
Bitmap logoCopy = logoBmp is null
? null
: logoBmp.Clone(new Rectangle(0, 0, logoBmp.Width, logoBmp.Height), logoBmp.PixelFormat);
var imgState = new ImageState
{
WorkFile = fileData.File,
@ -93,8 +87,7 @@ namespace MaddoShared
try
{
// Ensure CreateImageAsync can accept a null logoCopy value.
await imageCreatorService.CreateImageAsync(imgState, logoCopy);
await imageCreatorService.CreateImageAsync(imgState, logoBytes);
results.Add(fileData.File.Name);
@ -110,8 +103,7 @@ namespace MaddoShared
}
finally
{
// Dispose the clone if it was created
logoCopy?.Dispose();
// nothing to dispose — byte[] is managed
}
};
@ -139,14 +131,6 @@ namespace MaddoShared
}
}
try
{
logoBmp?.Dispose();
}
catch (Exception e)
{
logger.LogError(e, "Error in disposing the logo");
}
}
private List<FileData> GetFilesToProcess(Options options)

View file

@ -34,7 +34,7 @@ public class ImageCreatorImageSharp : IImageCreator
_logger = logger;
}
public async Task CreateImageAsync(ImageState imgState, System.Drawing.Image logo)
public async Task CreateImageAsync(ImageState imgState, byte[]? logoData)
{
ArgumentNullException.ThrowIfNull(imgState);
@ -74,7 +74,7 @@ public class ImageCreatorImageSharp : IImageCreator
var fileNameBig = System.IO.Path.Combine(imgState.DestDir.FullName, imgState.NomeFileBig);
// Draw overlays (text/logo) onto big image using ImageSharp and save
await DrawAndSaveWithGdiAsync(imgBig, fileNameBig, imgState, logo, _picSettings.JpegQuality, isThumbnail: false).ConfigureAwait(false);
await DrawAndSaveWithGdiAsync(imgBig, fileNameBig, imgState, logoData, _picSettings.JpegQuality, isThumbnail: false).ConfigureAwait(false);
// Create thumbnail if requested
if (_picSettings.CreaMiniature)
@ -85,7 +85,7 @@ public class ImageCreatorImageSharp : IImageCreator
var fileNameSmall = System.IO.Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall);
// Draw overlays and save thumbnail via ImageSharp
await DrawAndSaveWithGdiAsync(imgSmall, fileNameSmall, imgState, logo, _picSettings.JpegQualityMin, isThumbnail: true).ConfigureAwait(false);
await DrawAndSaveWithGdiAsync(imgSmall, fileNameSmall, imgState, logoData, _picSettings.JpegQualityMin, isThumbnail: true).ConfigureAwait(false);
}
}
catch (Exception ex)
@ -110,7 +110,7 @@ public class ImageCreatorImageSharp : IImageCreator
};
}
private async Task DrawAndSaveWithGdiAsync(Image<Rgba32> imgSharp, string outputPath, ImageState imgState, System.Drawing.Image logo, long quality, bool isThumbnail)
private async Task DrawAndSaveWithGdiAsync(Image<Rgba32> imgSharp, string outputPath, ImageState imgState, byte[]? logoData, long quality, bool isThumbnail)
{
// Use ImageSharp drawing APIs to render text and logos and save using ImageSharp encoders.
// Clone editable image so we don't mutate the original reference unexpectedly.
@ -287,13 +287,12 @@ public class ImageCreatorImageSharp : IImageCreator
// Draw logo if provided. For compatibility with the original GDI implementation,
// do not draw the logo on thumbnails (ImageCreatorSharp only draws logos on big images).
if (logo != null && _picSettings.LogoAggiungi && !isThumbnail)
if (logoData != null && logoData.Length > 0 && _picSettings.LogoAggiungi && !isThumbnail)
{
try
{
Image<Rgba32> logoImg = null;
// Prefer configured file if present, otherwise use the provided System.Drawing.Image instance
if (!string.IsNullOrEmpty(_picSettings.LogoNomeFile) && File.Exists(_picSettings.LogoNomeFile))
{
using var logoStream = File.OpenRead(_picSettings.LogoNomeFile);
@ -301,10 +300,7 @@ public class ImageCreatorImageSharp : IImageCreator
}
else
{
// Convert System.Drawing.Image to ImageSharp by saving to PNG in-memory to preserve alpha
await using var ms = new MemoryStream();
logo.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
using var ms = new MemoryStream(logoData);
logoImg = await SixLabors.ImageSharp.Image.LoadAsync<Rgba32>(ms).ConfigureAwait(false);
}

View file

@ -7,6 +7,7 @@ namespace MaddoShared;
/// <summary>
/// Dynamically resolves the concrete IImageCreator implementation at call time
/// based on current PicSettings.ImageCreatorProvider.
/// On non-Windows platforms only ImageCreatorImageSharp is available.
/// </summary>
public class ImageCreatorMapper : IImageCreator
{
@ -21,29 +22,33 @@ public class ImageCreatorMapper : IImageCreator
_logger = logger;
}
public Task CreateImageAsync(ImageState imgState, System.Drawing.Image logo)
public Task CreateImageAsync(ImageState imgState, byte[]? logoData)
{
var provider = (_settings.ImageCreatorProvider ?? "Sharp").Trim();
_logger?.LogDebug("Resolving IImageCreator for provider '{Provider}'", provider);
#if WINDOWS
return provider.Equals("ALTERNATE", StringComparison.OrdinalIgnoreCase)
? ResolveAndCall<ImageCreatorImageSharp>(imgState, logo)
: ResolveAndCall<ImageCreatorGDI>(imgState, logo);
? ResolveAndCall<ImageCreatorImageSharp>(imgState, logoData)
: ResolveAndCall<ImageCreatorGDI>(imgState, logoData);
#else
// GDI is not available on non-Windows — always use ImageSharp
return ResolveAndCall<ImageCreatorImageSharp>(imgState, logoData);
#endif
}
private Task ResolveAndCall<T>(ImageState imgState, System.Drawing.Image logo) where T : IImageCreator
private Task ResolveAndCall<T>(ImageState imgState, byte[]? logoData) where T : IImageCreator
{
// Resolve the concrete implementation and forward the call
var impl = (IImageCreator)_sp.GetService(typeof(T));
var impl = (IImageCreator?)_sp.GetService(typeof(T));
if (impl is null)
{
_logger?.LogWarning("Requested image creator {Type} is not registered. Falling back to ImageCreatorGDI.", typeof(T).Name);
impl = (IImageCreator)_sp.GetService(typeof(ImageCreatorGDI));
_logger?.LogWarning("Requested image creator {Type} is not registered. Falling back to ImageCreatorImageSharp.", typeof(T).Name);
impl = (IImageCreator?)_sp.GetService(typeof(ImageCreatorImageSharp));
}
if (impl is null)
throw new InvalidOperationException("No IImageCreator implementation is registered.");
return impl.CreateImageAsync(imgState, logo);
return impl.CreateImageAsync(imgState, logoData);
}
}

View file

@ -1,4 +1,5 @@
using System;
#if WINDOWS
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
@ -17,7 +18,7 @@ namespace MaddoShared;
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
public class ImageCreatorGDI(PicSettings picSettings, ILogger<ImageCreatorGDI> logger) : IImageCreator
{
public async Task CreateImageAsync(ImageState imgState, Image logo)
public async Task CreateImageAsync(ImageState imgState, byte[]? logoData)
{
try
{
@ -51,7 +52,7 @@ public class ImageCreatorGDI(PicSettings picSettings, ILogger<ImageCreatorGDI> l
AddText(g, imgState, imgOutputBig);
AddLogo(imgOutputBig, logo);
AddLogo(imgOutputBig, logoData);
SavePhoto(imgOutputBig, imgState, thisFormat);
}).ConfigureAwait(false);
@ -480,13 +481,14 @@ public class ImageCreatorGDI(PicSettings picSettings, ILogger<ImageCreatorGDI> l
}
}
private void AddLogo(Bitmap imgOutputBig, Image logo)
private void AddLogo(Bitmap imgOutputBig, byte[]? logoData)
{
// Skip if no logo provided
if (logo is null) return;
// Skip if no logo bytes provided
if (logoData is null) return;
// Load check (use short-circuit &&)
if (!(picSettings.LogoAggiungi && File.Exists(picSettings.LogoNomeFile))) return;
if (!picSettings.LogoAggiungi) return;
using var logo = Image.FromStream(new System.IO.MemoryStream(logoData));
// Decide whether to apply a color-key transparency remap or rely on existing image alpha.
// If UseTransparentColor is true, parse the configured TransparentColor and remap it to fully transparent.
@ -861,3 +863,4 @@ public class ImageCreatorGDI(PicSettings picSettings, ILogger<ImageCreatorGDI> l
: Environment.NewLine + formatted;
}
}
#endif // WINDOWS

View file

@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0-windows</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<OutputType>Library</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<UseWindowsForms>true</UseWindowsForms>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<PlatformTarget>x64</PlatformTarget>
<!-- WINDOWS preprocessor symbol mirrors the -windows TFM suffix so #if WINDOWS guards work -->
<DefineConstants Condition="$([MSBuild]::IsOsPlatform('Windows'))">$(DefineConstants);WINDOWS</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AsyncEnumerator" Version="4.0.2" />
@ -29,6 +29,6 @@
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.4.421302">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="10.0.3" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="10.0.3" Condition="$([MSBuild]::IsOsPlatform('Windows'))" />
</ItemGroup>
</Project>

View file

@ -1,7 +1,6 @@
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace MaddoShared;

View file

@ -73,7 +73,7 @@
<TextBlock Text="Libreria Immagini" FontWeight="Bold" Margin="0,12,0,0" />
<StackPanel Orientation="Horizontal" Margin="0,6,0,0">
<RadioButton Content="System.Graphics" IsChecked="{Binding UseSystemGraphics}" GroupName="Lib" />
<RadioButton Content="System.Graphics" IsChecked="{Binding UseSystemGraphics}" GroupName="Lib" IsVisible="{Binding IsRunningOnWindows}" />
<RadioButton Content="ImageSharp" IsChecked="{Binding UseImageSharp}" GroupName="Lib" Margin="8,0,0,0" />
</StackPanel>
</StackPanel>

View file

@ -5,12 +5,17 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
#if WINDOWS
using System.Drawing.Text;
#endif
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
#if WINDOWS
using System.Windows.Forms;
#endif
using System.Windows.Input;
using AutoMapper;
using MaddoShared;
@ -258,12 +263,16 @@ namespace ImageCatalog_2
private List<string> LoadAvailableFonts()
{
#if WINDOWS
var fonts = new List<string>();
using (var installedFonts = new InstalledFontCollection())
{
fonts.AddRange(installedFonts.Families.Select(f => f.Name));
}
return fonts;
#else
return new List<string>();
#endif
}
private CancellationTokenSource? _mainToken;
@ -604,7 +613,12 @@ namespace ImageCatalog_2
}
// Image library selection (UI radio buttons bind to the boolean helpers)
private string _imageLibrary = "System.Graphics";
private string _imageLibrary = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "System.Graphics" : "ImageSharp";
/// <summary>
/// Whether the application is running on Windows. Used by cross-platform UIs to show/hide Windows-only options.
/// </summary>
public bool IsRunningOnWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
/// <summary>
/// The selected image processing library. Possible values: "System.Graphics" or "ImageSharp".
@ -624,6 +638,7 @@ namespace ImageCatalog_2
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(UseSystemGraphics));
NotifyPropertyChanged(nameof(UseImageSharp));
NotifyPropertyChanged(nameof(IsRunningOnWindows));
}
}
@ -1506,7 +1521,11 @@ namespace ImageCatalog_2
public event EventHandler<string> LoadSettingsRequested;
public event EventHandler SelectColorRequested;
// Request that the View shows a message to the user (message, caption, icon)
#if WINDOWS
public event EventHandler<Tuple<string, string, MessageBoxIcon>> ShowMessageRequested;
#else
public event EventHandler<Tuple<string, string, int>> ShowMessageRequested;
#endif
public event EventHandler SelectTransparentColorRequested;
private void SelectSourceFolder(object parameter)

View file

@ -1,19 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net10.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>
<ProduceReferenceAssembly>False</ProduceReferenceAssembly>
<!-- Default assembly name for regular builds -->
<AssemblyName>ImageCatalog</AssemblyName>
<LangVersion>default</LangVersion>
</PropertyGroup>
<!-- Windows: net10.0-windows TFM auto-defines WINDOWS preprocessor symbol -->
<PropertyGroup Condition="$([MSBuild]::IsOsPlatform('Windows'))">
<TargetFramework>net10.0-windows</TargetFramework>
<OutputType>WinExe</OutputType>
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<ApplicationIcon>Logo.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PropertyGroup Condition="!$([MSBuild]::IsOsPlatform('Windows'))">
<TargetFramework>net10.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<!-- Keep MinVer package enabled but do NOT let it overwrite AssemblyVersion/FileVersion used at build-time.
@ -42,11 +47,11 @@
<ItemGroup>
<PackageReference Include="AIFotoONLUS.Core" Version="0.1.1" />
<PackageReference Include="AutoMapper" Version="16.0.0" />
<PackageReference Include="MahApps.Metro" Version="2.4.11" />
<PackageReference Include="MahApps.Metro" Version="2.4.11" Condition="$([MSBuild]::IsOsPlatform('Windows'))" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="10.0.3" />
<PackageReference Include="MahApps.Metro.IconPacks" Version="6.2.1" />
<PackageReference Include="MahApps.Metro.IconPacks" Version="6.2.1" Condition="$([MSBuild]::IsOsPlatform('Windows'))" />
<PackageReference Include="MinVer" Version="7.0.0" PrivateAssets="all" />
<PackageReference Include="Avalonia" Version="11.3.0" />
<PackageReference Include="Avalonia.Desktop" Version="11.3.0" />

View file

@ -1,4 +1,5 @@
using System;
#if WINDOWS
using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.CompilerServices;
@ -2241,4 +2242,5 @@ namespace ImageCatalog
}
}
}
}
}
#endif

View file

@ -1,4 +1,5 @@
using System;
#if WINDOWS
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
@ -837,4 +838,5 @@ public class PicInfo
DirDestStart = Dir_DestStart;
NomeImmagine = Nome_Immagine;
}
}
}
#endif

View file

@ -1,3 +1,4 @@
#if WINDOWS
using System.Windows;
using MahApps.Metro.Controls;
using ControlzEx.Theming;
@ -378,3 +379,5 @@ namespace ImageCatalog_2
}
}
#endif

View file

@ -195,7 +195,7 @@ namespace ImageCatalog_2.Models
// Selected image processing library (e.g., "System.Graphics" or "ImageSharp")
[JsonPropertyName("ImageLibrary")]
[XmlElement("ImageLibrary")]
public string ImageLibrary { get; set; } = "System.Graphics";
public string ImageLibrary { get; set; } = "ImageSharp";
// Options
[JsonPropertyName("ForceJpeg")]

View file

@ -1,4 +1,4 @@
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using ImageCatalog;
using ImageCatalog_2.Services;
using MaddoShared;
@ -16,6 +16,7 @@ namespace ImageCatalog_2;
static class Program
{
#if WINDOWS
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AllocConsole();
@ -56,6 +57,7 @@ static class Program
Console.SetOut(standardOutput);
Console.SetError(standardOutput);
}
#endif
public static IServiceProvider ServiceProvider { get; private set; }
@ -67,19 +69,20 @@ static class Program
[STAThread]
static void Main(string[] args)
{
System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.SystemAware);
#if WINDOWS
System.Windows.Forms.Application.SetHighDpiMode(System.Windows.Forms.HighDpiMode.SystemAware);
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
AllocConsole();
RedirectConsoleOutput();
#endif
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
ServiceProvider = serviceCollection.BuildServiceProvider();
// Resolve WPF MainWindow when available, otherwise fall back to WinForms MainForm
var serviceProvider = ServiceProvider;
// Determine UI based on command line. Default: WinForms. Use --wpf for WPF, --avalonia for Avalonia.
@ -92,19 +95,16 @@ static class Program
return;
}
#if WINDOWS
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") });
wpfApp.Resources.MergedDictionaries.Add(new System.Windows.ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml") });
// Default Light theme (can be replaced at runtime)
wpfApp.Resources.MergedDictionaries.Add(new System.Windows.ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml") });
// Also notify ThemeManager about initial theme so chrome and MahApps brushes are applied
try
{
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(wpfApp, "Light.Blue");
@ -116,10 +116,9 @@ static class Program
}
catch
{
// If resources fail to load (package not present at runtime), continue silently
// If resources fail to load, 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)
{
@ -127,26 +126,27 @@ static class Program
return;
}
// If WPF was requested but not available, fall back to WinForms.
// If WPF was requested but not available, fall through to WinForms.
}
// Default / fallback to WinForms UI
var mainForm = serviceProvider.GetRequiredService<MainForm>();
System.Windows.Forms.Application.Run(mainForm);
#else
// On non-Windows, Avalonia is the only available UI
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args ?? Array.Empty<string>());
#endif
}
private static void ConfigureServices(ServiceCollection services)
{
// Register AutoMapper (new AddAutoMapper overload — provide config and marker types)
services.AddAutoMapper(cfg => { }, typeof(Program));
// Register your services here
services.AddTransient<ITestService, TestService>();
services.AddTransient<ISettingsService, SettingsService>();
services.AddTransient<DataModel>(sp =>
{
// Resolve optional version provider and pass to DataModel
var testService = sp.GetRequiredService<ITestService>();
var settingsService = sp.GetRequiredService<ISettingsService>();
var imageCreation = sp.GetRequiredService<ImageCreationService>();
@ -159,25 +159,24 @@ static class Program
});
services.AddTransient<ImageCreationService>();
#if WINDOWS
services.AddTransient<ImageCreatorGDI>();
#endif
services.AddTransient<ImageCreatorImageSharp>();
services.AddTransient<ImageCreatorMapper>();
// Register IImageCreator to be resolved via ImageCreatorMapper which selects concrete implementation at call time
services.AddTransient<IImageCreator>(sp => sp.GetRequiredService<ImageCreatorMapper>());
// Register a ParametriSetup singleton that persists user preferences in LocalApplicationData
var userPrefsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"ImageCatalog", "userprefs.xml");
services.AddSingleton(new ParametriSetup(userPrefsPath));
services.AddSingleton<PicSettings>();
// Register your forms
#if WINDOWS
services.AddTransient<MainForm>();
// Register WPF MainWindow so it can be resolved with the existing DataModel
services.AddTransient<ImageCatalog_2.MainWindow>();
#endif
// Version provider for UI and logging
services.AddSingleton<MaddoShared.IVersionProvider, MaddoShared.VersionProvider>();
services.AddLogging(configure =>

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
@ -6,14 +6,18 @@ using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
#if WINDOWS
using System.Windows.Forms;
#endif
namespace ImageCatalog_2
{
public class ViewModelBase : INotifyPropertyChanged
{
private readonly SynchronizationContext? _synchronizationContext;
#if WINDOWS
private Control? _control;
#endif
protected ViewModelBase()
{
@ -25,10 +29,12 @@ namespace ImageCatalog_2
/// Set a Control to use for thread marshalling in WinForms applications.
/// This is required for proper cross-thread handling with data binding.
/// </summary>
#if WINDOWS
public void SetControl(Control control)
{
_control = control;
}
#endif
public event PropertyChangedEventHandler? PropertyChanged;
@ -40,6 +46,7 @@ namespace ImageCatalog_2
if (PropertyChanged == null)
return;
#if WINDOWS
// If we have a Control reference (WinForms), use Control.Invoke for proper marshalling
if (_control != null)
{
@ -53,7 +60,9 @@ namespace ImageCatalog_2
}
}
// Fallback to SynchronizationContext if available
else if (_synchronizationContext != null && SynchronizationContext.Current != _synchronizationContext)
else
#endif
if (_synchronizationContext != null && SynchronizationContext.Current != _synchronizationContext)
{
// We're on a different thread, marshal to the UI thread
_synchronizationContext.Send(_ =>