Repository navigation
Expand file tree
/
Copy pathObservableAsPropertyAttributeExamples.cs
More file actions
81 lines (65 loc) · 2.97 KB
/
Copy pathObservableAsPropertyAttributeExamples.cs
File metadata and controls
81 lines (65 loc) · 2.97 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
// Copyright (c) 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 System.ComponentModel;
using ReactiveUI.Binding.Documentation.Todo;
using ReactiveUI.Primitives.Signals;
namespace ReactiveUI.Binding.Documentation.Properties;
/// <summary>Shows a partial property declared with <see cref="ObservableAsPropertyAttribute"/>.</summary>
public static partial class ObservableAsPropertyAttributeExamples
{
/// <summary>Constructs <see cref="SummaryViewModel"/> and reads both of its generated properties.</summary>
public static void DeclareAPropertyWithTheAttribute()
{
TodoItem item = new TodoItem { Title = "Renew car registration" };
SummaryViewModel summary = new SummaryViewModel(item);
Console.WriteLine(summary.Title);
Console.WriteLine(summary.IsDone);
item.IsDone = true;
Console.WriteLine(summary.IsDone);
}
/// <summary>Reads a property before and after its helper is assigned: until then it returns its initial value.</summary>
public static void StartFromAnInitialValue()
{
StatusViewModel waiting = new StatusViewModel(null);
StatusViewModel ready = new StatusViewModel(Signal.Return("Ready"));
Console.WriteLine(waiting.Status);
Console.WriteLine(ready.Status);
// Output:
// Loading
// Ready
}
/// <summary>Reads each replacement helper's value from its synchronous change notification.</summary>
public static void ReplaceAHelperWhileObserved()
{
using ReplaceableStatus? status = new();
string? observed = null;
status.PropertyChanged += (_, _) => observed = status.Status;
status.Connect(Signal.Return("Connected"));
Console.WriteLine(observed);
status.Connect(Signal.Return("Disconnected"));
Console.WriteLine(observed);
// Output:
// Connected
// Disconnected
}
/// <summary>Replaces its property's source when a new connection starts.</summary>
[System.Diagnostics.DebuggerDisplay("ReplaceableStatus: Status = {Status}")]
public sealed partial class ReplaceableStatus : INotifyPropertyChanged, IDisposable
{
/// <inheritdoc/>
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>Gets the latest value from the current connection.</summary>
[ObservableAsProperty]
public partial string? Status { get; }
/// <summary>Disposes the previous helper and follows a new source.</summary>
/// <param name="source">The connection's status values.</param>
public void Connect(IObservable<string?> source)
{
_statusHelper?.Dispose();
_statusHelper = source.ToProperty(this, static x => x.Status);
}
/// <inheritdoc/>
public void Dispose() => _statusHelper?.Dispose();
}
}