2026-02-26 19:17:23 +01:00
|
|
|
using System;
|
2024-10-14 22:55:52 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Text;
|
2026-02-04 23:16:06 +01:00
|
|
|
using System.Threading;
|
2024-10-14 22:55:52 +02:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ImageCatalog_2
|
|
|
|
|
{
|
|
|
|
|
public class ViewModelBase : INotifyPropertyChanged
|
|
|
|
|
{
|
2026-02-04 23:16:06 +01:00
|
|
|
private readonly SynchronizationContext? _synchronizationContext;
|
|
|
|
|
|
|
|
|
|
protected ViewModelBase()
|
|
|
|
|
{
|
|
|
|
|
// Capture the synchronization context (UI thread context)
|
|
|
|
|
_synchronizationContext = SynchronizationContext.Current;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-14 22:55:52 +02:00
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
2026-05-09 14:04:21 +02:00
|
|
|
|
2024-10-14 22:55:52 +02:00
|
|
|
// 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 = "")
|
|
|
|
|
{
|
2026-02-04 23:16:06 +01:00
|
|
|
if (PropertyChanged == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2026-02-26 19:17:23 +01:00
|
|
|
if (_synchronizationContext != null && SynchronizationContext.Current != _synchronizationContext)
|
2026-02-04 23:16:06 +01:00
|
|
|
{
|
|
|
|
|
// We're on a different thread, marshal to the UI thread
|
|
|
|
|
_synchronizationContext.Send(_ =>
|
|
|
|
|
{
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}, null);
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-10-14 22:55:52 +02:00
|
|
|
{
|
2026-02-04 23:16:06 +01:00
|
|
|
// We're already on the UI thread or no sync context available
|
2024-10-14 22:55:52 +02:00
|
|
|
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|