mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
Add documentation
This commit is contained in:
35
ReactiveExample/README.md
Normal file
35
ReactiveExample/README.md
Normal file
@@ -0,0 +1,35 @@
|
||||
This is a sample app that shows how to use `System.Reactive` and `ReactiveUI` with `Terminal.Gui`. The app uses the MVVM architecture that may seem familiar to folks coming from WPF, Xamarin Forms, UWP, Avalonia, or Windows Forms. In this app, we implement the data bindings using ReactiveUI `WhenAnyValue` syntax and [Pharmacist](https://github.com/reactiveui/pharmacist) — a tool that converts all events in a NuGet package into observable wrappers.
|
||||
|
||||
<img src="https://user-images.githubusercontent.com/6759207/94748621-646a7280-038a-11eb-8ea0-34629dc799b3.gif" width="450">
|
||||
|
||||
### Data Bindings
|
||||
|
||||
If you wish to implement `OneWay` data binding, then use the `WhenAnyValue` [ReactiveUI extension method](https://www.reactiveui.net/docs/handbook/when-any/) that listens to `INotifyPropertyChanged` events of the specified property, and converts that events into `IObservable<TProperty>`:
|
||||
|
||||
```cs
|
||||
// 'usernameInput' is 'TextField'
|
||||
ViewModel
|
||||
.WhenAnyValue (x => x.Username)
|
||||
.BindTo (usernameInput, x => x.Text);
|
||||
```
|
||||
|
||||
Note that your view model should implement `INotifyPropertyChanged` or inherit from a `ReactiveObject`. If you wish to implement `OneWayToSource` data binding, then install [Pharmacist.MSBuild](https://github.com/reactiveui/pharmacist) into your project and listen to e.g. `TextChanged` event of a `TextField`:
|
||||
|
||||
```cs
|
||||
// 'usernameInput' is 'TextField'
|
||||
usernameInput
|
||||
.Events () // The Events() extension is generated by Pharmacist.
|
||||
.TextChanged
|
||||
.Select (old => usernameInput.Text)
|
||||
.DistinctUntilChanged ()
|
||||
.BindTo (ViewModel, x => x.Username);
|
||||
```
|
||||
|
||||
If you combine `OneWay` and `OneWayToSource` data bindings, you get `TwoWay` data binding. Also be sure to use the `ustring` type instead of the `string` type. Invoking commands should be as simple as this:
|
||||
```cs
|
||||
// 'clearButton' is 'Button'
|
||||
clearButton
|
||||
.Events ()
|
||||
.Clicked
|
||||
.InvokeCommand (ViewModel, x => x.Clear);
|
||||
```
|
||||
Reference in New Issue
Block a user