-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTestViewModel.cs
More file actions
149 lines (128 loc) · 5.02 KB
/
TestViewModel.cs
File metadata and controls
149 lines (128 loc) · 5.02 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System.Reactive;
using System.Reactive.Linq;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using ReactiveUI;
using ReactiveUI.SourceGenerators;
namespace SGReactiveUI.SourceGenerators.Test;
/// <summary>
/// TestClass.
/// </summary>
[DataContract]
public partial class TestViewModel : ReactiveObject
{
[JsonInclude]
[DataMember]
[ObservableAsProperty]
private double _test2Property;
[JsonInclude]
[Reactive]
[DataMember]
private int _test1Property;
/// <summary>
/// Initializes a new instance of the <see cref="TestViewModel"/> class.
/// </summary>
public TestViewModel()
{
Console.Out.WriteLine(Test1Command);
Console.Out.WriteLine(Test2Command);
Console.Out.WriteLine(Test3AsyncCommand);
Console.Out.WriteLine(Test4AsyncCommand);
Console.Out.WriteLine(Test5StringToIntCommand);
Console.Out.WriteLine(Test6ArgOnlyCommand);
Console.Out.WriteLine(Test7ObservableCommand);
Console.Out.WriteLine(Test8ObservableCommand);
Console.Out.WriteLine(Test9AsyncCommand);
Console.Out.WriteLine(Test10AsyncCommand);
Test1Command?.Execute().Subscribe();
Test2Command?.Execute().Subscribe(r => Console.Out.WriteLine(r));
Test3AsyncCommand?.Execute().Subscribe();
Test4AsyncCommand?.Execute().Subscribe(r => Console.Out.WriteLine(r));
Test5StringToIntCommand?.Execute("100").Subscribe(Console.Out.WriteLine);
Test6ArgOnlyCommand?.Execute("Hello World").Subscribe();
Test7ObservableCommand?.Execute().Subscribe();
_test2PropertyHelper = Test8ObservableCommand!.ToProperty(this, x => x.Test2Property);
Test8ObservableCommand?.Execute(100).Subscribe(Console.Out.WriteLine);
Console.Out.WriteLine($"Test2Property Value: {Test2}");
Console.Out.WriteLine($"Test2Property underlying Value: {_test2Property}");
Test9AsyncCommand?.ThrownExceptions.Subscribe(Console.Out.WriteLine);
var cancel = Test9AsyncCommand?.Execute().Subscribe();
Task.Delay(1000).Wait();
cancel?.Dispose();
Test10AsyncCommand?.Execute(200).Subscribe(r => Console.Out.WriteLine(r));
}
/// <summary>
/// Gets the instance.
/// </summary>
/// <value>
/// The instance.
/// </value>
public static TestViewModel Instance { get; } = new();
/// <summary>
/// Gets the can execute test1.
/// </summary>
/// <value>
/// The can execute test1.
/// </value>
#pragma warning disable CA1822 // Mark members as static
public IObservable<bool> CanExecuteTest1 => Observable.Return(true);
#pragma warning restore CA1822 // Mark members as static
/// <summary>
/// Test1s this instance.
/// </summary>
[ReactiveCommand(CanExecute = nameof(CanExecuteTest1))]
[property: JsonInclude]
private void Test1() => Console.Out.WriteLine("Test1");
/// <summary>
/// Test2s this instance.
/// </summary>
/// <returns>Rectangle.</returns>
[ReactiveCommand]
private Point Test2() => default;
/// <summary>
/// Test3s the asynchronous.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[ReactiveCommand]
private async Task Test3Async() => await Task.Delay(0);
/// <summary>
/// Test4s the asynchronous.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[ReactiveCommand]
private async Task<Point> Test4Async() => await Task.FromResult(new Point(100, 100));
/// <summary>
/// Test5s the string to int.
/// </summary>
/// <param name="str">The string.</param>
/// <returns>int.</returns>
[ReactiveCommand]
private int Test5StringToInt(string str) => int.Parse(str);
/// <summary>
/// Test6s the argument only.
/// </summary>
/// <param name="str">The string.</param>
[ReactiveCommand]
private void Test6ArgOnly(string str) => Console.Out.WriteLine($">>> {str}");
/// <summary>
/// Test7s the observable.
/// </summary>
/// <returns>An Observable of Unit.</returns>
[ReactiveCommand]
private IObservable<Unit> Test7Observable() => Observable.Return(Unit.Default);
/// <summary>
/// Test8s the observable.
/// </summary>
/// <param name="i">The i.</param>
/// <returns>An Observable of int.</returns>
[ReactiveCommand]
private IObservable<double> Test8Observable(int i) => Observable.Return(i + 10.0);
[ReactiveCommand]
private async Task Test9Async(CancellationToken ct) => await Task.Delay(2000, ct);
[ReactiveCommand]
private async Task<Point> Test10Async(int size, CancellationToken ct) => await Task.FromResult(new Point(size, size));
}