Repository navigation
Expand file tree
/
Copy pathBindCommandExamples.cs
More file actions
372 lines (279 loc) · 15.7 KB
/
Copy pathBindCommandExamples.cs
File metadata and controls
372 lines (279 loc) · 15.7 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// 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 ReactiveUI.Binding.Documentation.Banking;
using ReactiveUI.Binding.Documentation.CloudStorage;
using ReactiveUI.Binding.Documentation.Education;
using ReactiveUI.Binding.Documentation.GitHub;
using ReactiveUI.Binding.Documentation.Todo;
using ReactiveUI.Primitives;
using ReactiveUI.Primitives.Signals;
namespace ReactiveUI.Binding.Documentation.Bindings;
/// <summary>Shows <c>BindCommand</c>: a button runs a view model command, follows its <c>CanExecute</c> and can pass it a parameter.</summary>
public static class BindCommandExamples
{
/// <summary>The name of the event a MAUI button raises when the user presses it.</summary>
private const string ClickedEventName = "Clicked";
/// <summary>The title of the to-do item the add button creates.</summary>
private const string NewTodoTitle = "Book electrician";
/// <summary>The amount the customer sends to the utility.</summary>
private const decimal UtilityAmount = 250M;
/// <summary>The position of Linear Algebra in the list of courses.</summary>
private const int MathCourseIndex = 1;
/// <summary>The position of Diego Alvarez in the list of students who could join Linear Algebra.</summary>
private const int DiegoCandidateIndex = 2;
/// <summary>The name of the file the user picks in the upload dialog.</summary>
private const string SpringCampaignFileName = "spring-campaign.png";
/// <summary>The size in bytes of the picked file.</summary>
private const long SpringCampaignSize = 3_145_728;
/// <summary>The position of the uploaded file among the objects of the photos folder.</summary>
private const int SpringCampaignPosition = 1;
/// <summary>Binds the add button of the to-do screen to the view model's add command; the button follows the command's <c>CanExecute</c>.</summary>
/// <returns>A task that completes when the item is stored.</returns>
public static async Task BindAddButton()
{
TodoListViewModel viewModel = new(InMemoryTodoStore.CreateSeeded());
await viewModel.LoadAsync();
TodoView view = new() { ViewModel = viewModel };
using IDisposable binding = view.BindCommand(viewModel, static x => x.AddCommand, static v => v.AddButton);
Console.WriteLine(view.AddButton.IsEnabled);
viewModel.NewTitle = NewTodoTitle;
Console.WriteLine(view.AddButton.IsEnabled);
// The command starts the work and returns, so start listening for the new selection before the click.
Task<TodoItem?> added = viewModel.WhenChanged(static x => x.SelectedItem).Where(static item => item is not null).FirstAsync();
((IButtonController)view.AddButton).SendClicked();
await added;
Console.WriteLine(viewModel.Items.Count);
Console.WriteLine(viewModel.SelectedItem!.Title);
Console.WriteLine(view.AddButton.IsEnabled);
// Output:
// False
// True
// 5
// Book electrician
// False
}
/// <summary>Binds the complete button through its <c>Clicked</c> event; the binding runs the command on a click and leaves the button's enabled state alone.</summary>
/// <returns>A task that completes when the item is finished.</returns>
public static async Task BindCompleteButtonToNamedEvent()
{
TodoListViewModel viewModel = new(InMemoryTodoStore.CreateSeeded());
await viewModel.LoadAsync();
TodoView view = new() { ViewModel = viewModel };
using IDisposable binding = view.BindCommand(viewModel, static x => x.CompleteCommand, static v => v.CompleteButton, toEvent: ClickedEventName);
// No item is selected, so the command refuses the click.
((IButtonController)view.CompleteButton).SendClicked();
Console.WriteLine(viewModel.RemainingCount);
TodoItem item = viewModel.Items[0];
viewModel.SelectedItem = item;
Task<bool> completed = item.WhenChanged(static x => x.IsDone).Where(static done => done).FirstAsync();
((IButtonController)view.CompleteButton).SendClicked();
await completed;
Console.WriteLine(viewModel.RemainingCount);
Console.WriteLine(view.CompleteButton.IsEnabled);
// Output:
// 3
// 2
// True
}
/// <summary>
/// Binds one button to one command twice, with and without <c>toEvent</c>. Each call is its own binding: the plain one
/// attaches the command to the button and follows its <c>CanExecute</c>, and the event one runs the command on each click.
/// </summary>
public static void BindExportButtonWithAndWithoutEvent()
{
StatementExportViewModel viewModel = new() { HasStatement = true };
StatementExportView view = new() { ViewModel = viewModel };
IDisposable attached = view.BindCommand(viewModel, static x => x.ExportCommand, static v => v.ExportButton);
IDisposable clicked = view.BindCommand(viewModel, static x => x.ExportCommand, static v => v.ExportButton, toEvent: ClickedEventName);
Console.WriteLine(ReferenceEquals(viewModel.ExportCommand, view.ExportButton.Command));
// One press reaches the command through both bindings.
((IButtonController)view.ExportButton).SendClicked();
Console.WriteLine(viewModel.ExportCount);
clicked.Dispose();
((IButtonController)view.ExportButton).SendClicked();
Console.WriteLine(viewModel.ExportCount);
// Only the plain binding follows CanExecute, so the button goes dark while no statement is chosen.
viewModel.HasStatement = false;
Console.WriteLine(view.ExportButton.IsEnabled);
attached.Dispose();
// Output:
// True
// 2
// 3
// False
}
/// <summary>Binds the sign-in button; it stays disabled until the user has typed an access token.</summary>
/// <returns>A task that completes when the sign-in has finished.</returns>
public static async Task BindSignInButton()
{
IssueBoardViewModel viewModel = new(InMemoryGitHubServer.CreateSeeded());
IssueBoardView view = new() { ViewModel = viewModel };
using IDisposable binding = view.BindCommand(viewModel, static x => x.SignInCommand, static v => v.SignInButton);
Console.WriteLine(view.SignInButton.IsEnabled);
viewModel.Token = InMemoryGitHubServer.PriyaToken;
Console.WriteLine(view.SignInButton.IsEnabled);
Task<IReadOnlyList<Repository>> loaded = viewModel.WhenChanged(static x => x.Repositories).Where(static repositories => repositories.Count > 0).FirstAsync();
((IButtonController)view.SignInButton).SendClicked();
await loaded;
Console.WriteLine(viewModel.CurrentUser!.Login);
Console.WriteLine(view.SignInButton.IsEnabled);
// Output:
// False
// True
// priya-nair
// False
}
/// <summary>Binds the close-issue button; the view model asks a confirmation handler before it closes the issue.</summary>
/// <returns>A task that completes when the issue is closed.</returns>
public static async Task BindCloseIssueButton()
{
IssueBoardViewModel viewModel = await OpenWebshopIssuesAsync();
IssueBoardView view = new() { ViewModel = viewModel };
using IDisposable confirmation = viewModel.ConfirmClose.RegisterHandler(static context => context.SetOutput(true));
using IDisposable binding = view.BindCommand(viewModel, static x => x.CloseIssueCommand, static v => v.CloseIssueButton);
Console.WriteLine(view.CloseIssueButton.IsEnabled);
Issue issue = viewModel.Issues[0];
viewModel.SelectedIssue = issue;
Console.WriteLine(view.CloseIssueButton.IsEnabled);
Task<int> requestFinished = viewModel.WhenChanged(static x => x.RateLimitRemaining).Skip(1).FirstAsync();
((IButtonController)view.CloseIssueButton).SendClicked();
await requestFinished;
Console.WriteLine(issue.State);
Console.WriteLine(view.CloseIssueButton.IsEnabled);
// Output:
// False
// True
// Closed
// False
}
/// <summary>Binds the transfer button; it is disabled until the draft breaks none of the view model's rules.</summary>
/// <returns>A task that completes when the transfer has been sent.</returns>
public static async Task BindTransferButton()
{
TransferViewModel viewModel = new(new InMemoryBankingBackend());
await viewModel.LoadAsync();
using IDisposable confirmation = viewModel.ConfirmTransfer.RegisterHandler(static context => context.SetOutput(true));
TransferView view = new() { ViewModel = viewModel };
using IDisposable binding = view.BindCommand(viewModel, static x => x.TransferCommand, static v => v.TransferButton);
Console.WriteLine(view.TransferButton.IsEnabled);
viewModel.Draft.Source = viewModel.Accounts[0];
viewModel.Draft.Payee = viewModel.Payees[0];
Console.WriteLine(view.TransferButton.IsEnabled);
viewModel.Draft.Amount = UtilityAmount;
Console.WriteLine(view.TransferButton.IsEnabled);
// The transfer resets the amount once the bank has answered.
Task<decimal> sent = viewModel.Draft.WhenChanged(static x => x.Amount).Where(static amount => amount == 0M).FirstAsync();
((IButtonController)view.TransferButton).SendClicked();
await sent;
Console.WriteLine(viewModel.LastReceipt!.NewBalance);
Console.WriteLine(view.TransferButton.IsEnabled);
// Output:
// False
// False
// True
// 2200.75
// False
}
/// <summary>Binds the enrol button with an observable command parameter: the student selected in the candidate list.</summary>
/// <returns>A task that completes when the student is on the roster.</returns>
public static async Task BindEnrolButtonWithObservableParameter()
{
GradebookViewModel viewModel = new(InMemoryStudentRecords.CreateSeeded());
await viewModel.LoadCoursesAsync();
viewModel.SelectedCourse = viewModel.Courses[MathCourseIndex];
await viewModel.OpenCourseAsync();
GradebookView view = new() { ViewModel = viewModel };
view.CandidateList.ItemsSource = viewModel.Candidates.ToList();
using IDisposable binding = view.BindCommand(viewModel, static x => x.EnrolCommand, static v => v.EnrolButton, view.CandidateList.WhenChanged(static x => x.SelectedItem));
Console.WriteLine(view.EnrolButton.IsEnabled);
view.CandidateList.SelectedItem = viewModel.Candidates[DiegoCandidateIndex];
Console.WriteLine(view.EnrolButton.IsEnabled);
Task<IReadOnlyList<Student>> rosterLoaded = viewModel.WhenChanged(static x => x.Roster).Skip(1).FirstAsync();
((IButtonController)view.EnrolButton).SendClicked();
await rosterLoaded;
Console.WriteLine(viewModel.Roster.Count);
// Output:
// False
// True
// 3
}
/// <summary>Binds the upload button of the browser through its <c>Clicked</c> event with an observable command parameter: each file the user picks.</summary>
/// <returns>A task that completes when the file is stored.</returns>
public static async Task BindUploadButtonWithObservableParameterOnNamedEvent()
{
StorageBrowserViewModel browser = await OpenPhotosFolderAsync();
StorageBrowserView view = new() { ViewModel = browser };
Signal<UploadRequest> pickedFiles = new();
using IDisposable binding = view.BindCommand(browser, static x => x.UploadCommand, static v => v.UploadButton, pickedFiles, toEvent: ClickedEventName);
pickedFiles.OnNext(SpringCampaign());
Task<bool> uploaded = browser.WhenChanged(static x => x.IsUploading).Skip(1).Where(static uploading => !uploading).FirstAsync();
((IButtonController)view.UploadButton).SendClicked();
await uploaded;
Console.WriteLine(browser.Objects.Count);
// Output:
// 3
}
/// <summary>Binds the upload button through its <c>Clicked</c> event with a parameter expression.</summary>
/// <returns>A task that completes when the file is stored.</returns>
public static async Task BindUploadButtonWithParameterExpressionOnNamedEvent()
{
StorageBrowserViewModel browser = await OpenPhotosFolderAsync();
UploadPanelViewModel viewModel = new(browser);
UploadPanelView view = new() { ViewModel = viewModel };
using IDisposable binding = view.BindCommand(viewModel, static panel => panel.UploadCommand, static v => v.UploadButton, static panel => panel.PendingUpload, toEvent: ClickedEventName);
viewModel.PendingUpload = SpringCampaign();
Task<bool> uploaded = browser.WhenChanged(static x => x.IsUploading).Skip(1).Where(static uploading => !uploading).FirstAsync();
((IButtonController)view.UploadButton).SendClicked();
await uploaded;
Console.WriteLine(browser.Objects.Count);
// Output:
// 3
}
/// <summary>Binds the upload button with a parameter expression: the file the user picked, read from the view model.</summary>
/// <returns>A task that completes when the file is stored.</returns>
public static async Task BindUploadButtonWithParameterExpression()
{
StorageBrowserViewModel browser = await OpenPhotosFolderAsync();
UploadPanelViewModel viewModel = new(browser);
UploadPanelView view = new() { ViewModel = viewModel };
using IDisposable binding = view.BindCommand(viewModel, static x => x.UploadCommand, static v => v.UploadButton, static x => x.PendingUpload);
Console.WriteLine(view.UploadButton.IsEnabled);
viewModel.PendingUpload = SpringCampaign();
Console.WriteLine(view.UploadButton.IsEnabled);
Task<bool> uploaded = browser.WhenChanged(static x => x.IsUploading).Skip(1).Where(static uploading => !uploading).FirstAsync();
((IButtonController)view.UploadButton).SendClicked();
await uploaded;
Console.WriteLine(browser.Objects.Count);
Console.WriteLine(browser.Objects[SpringCampaignPosition].Key);
// Output:
// False
// True
// 3
// photos/2026/spring-campaign.png
}
/// <summary>Creates the file the user picks in the upload dialog.</summary>
/// <returns>The upload request.</returns>
private static UploadRequest SpringCampaign() => new(SpringCampaignFileName, SpringCampaignSize, "image/png");
/// <summary>Signs in as Priya Nair and loads the open issues of the webshop repository.</summary>
/// <returns>A task that completes with the view model once the issues are loaded.</returns>
private static async Task<IssueBoardViewModel> OpenWebshopIssuesAsync()
{
IssueBoardViewModel viewModel = new(InMemoryGitHubServer.CreateSeeded()) { Token = InMemoryGitHubServer.PriyaToken };
await viewModel.SignInAsync();
viewModel.SelectedRepository = viewModel.Repositories[0];
await viewModel.LoadIssuesAsync();
return viewModel;
}
/// <summary>Opens the 2026 photos folder of the media bucket.</summary>
/// <returns>A task that completes with the browser once the folder is listed.</returns>
private static async Task<StorageBrowserViewModel> OpenPhotosFolderAsync()
{
StorageBrowserViewModel browser = new(InMemoryObjectStorage.CreateSeeded());
await browser.LoadBucketsAsync();
browser.SelectedBucket = browser.Buckets[0];
browser.CurrentPrefix = "photos/2026/";
await browser.LoadObjectsAsync();
return browser;
}
}