Repository navigation
Expand file tree
/
Copy pathSourceGeneratorsExamples.cs
More file actions
96 lines (71 loc) · 3.21 KB
/
Copy pathSourceGeneratorsExamples.cs
File metadata and controls
96 lines (71 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2019-2026 ReactiveUI and Contributors. All rights reserved.
// ReactiveUI and Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using ReactiveUI.Primitives;
namespace ReactiveUI.Binding.Documentation.SourceGenerators;
/// <summary>
/// Shows bindings over the members ReactiveUI.SourceGenerators writes. That generator's output is not visible to this
/// one, so the binding generator reads its attributes and follows its rules for what it writes.
/// </summary>
public static class SourceGeneratorsExamples
{
/// <summary>Observes the property <c>[Reactive]</c> writes from a field.</summary>
public static void ObserveAPropertyWrittenFromAField()
{
ProfileViewModel profile = new ProfileViewModel();
using IDisposable subscription = profile.WhenAnyValue(static x => x.DisplayName).Subscribe(Console.WriteLine);
profile.DisplayName = "Grace";
// Output:
// Ada
// Grace
}
/// <summary>Observes a class marked <c>[IReactiveObject]</c>, which raises its notifications through the generated interface.</summary>
public static void ObserveAClassMarkedIReactiveObject()
{
ProfileCard card = new ProfileCard();
using IDisposable subscription = card.WhenAnyValue(static x => x.Title).Subscribe(Console.WriteLine);
card.Title = "Lead engineer";
// Output:
// Engineer
// Lead engineer
}
/// <summary>Binds a screen's name box to the property <c>[Reactive]</c> writes, in both directions.</summary>
public static void BindAPropertyWrittenFromAField()
{
ProfileViewModel profile = new ProfileViewModel();
ProfileView view = new ProfileView { ViewModel = profile };
using IDisposable binding = view.Bind(profile, static x => x.DisplayName, static v => v.NameText);
Console.WriteLine(view.NameText);
view.NameText = "Linus";
Console.WriteLine(profile.DisplayName);
// Output:
// Ada
// Linus
}
/// <summary>
/// Observes a property the way code another source generator writes does: with <see cref="ObservedProperty"/>,
/// which needs no generated binding. Hand-written code calls <c>WhenAnyValue</c> instead.
/// </summary>
public static void ObserveFromGeneratedCode()
{
ProfileViewModel profile = new ProfileViewModel();
using IDisposable subscription = ObservedProperty
.Create(profile, static x => x.DisplayName, static x => x.DisplayName)
.Subscribe(Console.WriteLine);
profile.DisplayName = "Grace";
// Output:
// Ada
// Grace
}
/// <summary>Binds a button to the command <c>[ReactiveCommand]</c> writes from a method.</summary>
public static void BindAButtonToAGeneratedCommand()
{
ProfileViewModel profile = new ProfileViewModel();
ProfileView view = new ProfileView { ViewModel = profile };
using IDisposable binding = view.BindCommand(profile, static x => x.SaveCommand, static v => v.Save);
view.Save.Press();
Console.WriteLine(profile.Saves);
// Output:
// 1
}
}