Skip to content

Instantly share code, notes, and snippets.

@worldbeater
Last active August 1, 2023 21:50
Show Gist options
  • Save worldbeater/9504b98f1c86606c9c05a00689588d7d to your computer and use it in GitHub Desktop.
Save worldbeater/9504b98f1c86606c9c05a00689588d7d to your computer and use it in GitHub Desktop.
Observables. Shorter Getters And Setters. ReactiveUI. See: https://medium.com/@worldbeater/reactive-mvvm-for-net-platform-175dc69cfc82
public class ReactiveViewModel : ReactiveObject
{
public ReactiveViewModel()
{
Clear = ReactiveCommand.Create(() => { Name = string.Empty; });
this.WhenAnyValue(x => x.Name)
.Select(name => $"Hello, {name}!")
.ToProperty(this, x => x.Greeting, out greeting);
}
public ReactiveCommand<Unit, Unit> Clear { get; }
private readonly ObservableAsPropertyHelper<string> greeting;
public string Greeting => greeting.Value;
private string name = string.Empty;
public string Name
{
get => name;
set => this.RaiseAndSetIfChanged(ref name, value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment