Catalog/imagecatalog/ViewModelBase.cs
MaddoScientisto 73597689ed 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.
2026-02-26 19:17:23 +01:00

81 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
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()
{
// Capture the synchronization context (UI thread context)
_synchronizationContext = SynchronizationContext.Current;
}
/// <summary>
/// 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;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
protected void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged == null)
return;
#if WINDOWS
// If we have a Control reference (WinForms), use Control.Invoke for proper marshalling
if (_control != null)
{
if (_control.InvokeRequired)
{
_control.Invoke(() => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)));
}
else
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
// Fallback to SynchronizationContext if available
else
#endif
if (_synchronizationContext != null && SynchronizationContext.Current != _synchronizationContext)
{
// We're on a different thread, marshal to the UI thread
_synchronizationContext.Send(_ =>
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}, null);
}
else
{
// We're already on the UI thread or no sync context available
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}