Observable helpers with short-and-easy and UI-safe property implementations. Can be used for any MVVM software architectural applications.
$ dotnet add package ObservableHelpersObservable helpers with source generators and UI-safe property implementations. Can be used for any MVVM software architectural applications.
NuGets
| Name | Info |
|---|---|
| ObservableHelpers |
// Install release version
Install-Package ObservableHelpers
// Install pre-release version
Install-Package ObservableHelpers -pre
.NET Standard 2.0 and above - see https://github.com/dotnet/standard/blob/master/docs/versions.md for compatibility matrix
All observable events are executed on thread that was used to create the object instance. To use in UI safe updates, create the object instances at the UI thread or manually configure the ISyncObject.SyncOperation to use UI thread.
using ObservableHelpers;
namespace YourNamespace
{
[ObservableObject]
public partial class Dinosaur
{
[ObservableProperty]
string? name;
[ObservableProperty(Access = Access.PublicWithPrivateSetter)]
string? family;
[ObservableProperty]
int height;
}
}
using ObservableHelpers;
namespace YourNamespace
{
public partial class Dinosaur
{
public string? Name
{
get => name;
set
{
if (!EqualityComparer<string?>.Default.Equals(name, value))
{
OnNameChanging(value);
OnPropertyChanging(nameof(Name));
name = value;
OnNameChanged(value);
OnPropertyChanged(nameof(Name));
}
}
}
public string? Family
{
get => family;
private set
{
if (!EqualityComparer<string?>.Default.Equals(family, value))
{
OnFamilyChanging(value);
OnPropertyChanging(nameof(Family));
family = value;
OnFamilyChanged(value);
OnPropertyChanged(nameof(Family));
}
}
}
public int Height
{
get => height;
set
{
if (!EqualityComparer<int>.Default.Equals(height, value))
{
OnHeightChanging(value);
OnPropertyChanging(nameof(Height));
height = value;
OnHeightChanged(value);
OnPropertyChanged(nameof(Height));
}
}
}
}
}
using ObservableHelpers;
namespace YourNamespace
{
public class Program
{
private Dinosaur dinosaur;
public void UIThread()
{
dinosaur = new Dinosaur(); // Must be created on UI thread to synchronize events
dinosaur.SynchronizeChangedEvent = true;
}
public void BackgroundThread()
{
dinosaur.PropertyChanged += (s, e) =>
{
// Executes on UI thread if dinosaur.SynchronizeChangedEvent is true (default false)
}
dinosaur.SynchronizedPropertyChanged += (s, e) =>
{
// Executed on UI thread
}
dinosaur.UnsynchronizedPropertyChanged += (s, e) =>
{
// Executed on current thread
}
dinosaur.Name = "Megalosaurus";
}
}
}
Code & Inspiration from the following:
All I have ever asked is to be active by submitting bugs, features, and sending those pull requests down!.