Skip to content

Commit c9d5bdf

Browse files
authored
Update readme file.
1 parent c8a911b commit c9d5bdf

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

README.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,38 @@ public class ItemViewModel : IBindingContext
386386

387387
The `ItemViewModel` can be serialized and deserialized without any issues.
388388

389+
To achieve the same result, but with minimal boilerplate code, you can automatically create an observable backing field using the `[WithObservableBackingField]` attribute from [UnityMvvmToolkit.Generator](https://github.com/LibraStack/UnityMvvmToolkit.Generator).
390+
391+
```csharp
392+
public partial class ItemViewModel : IBindingContext
393+
{
394+
[WithObservableBackingField]
395+
public string Name
396+
{
397+
get => _name.Value;
398+
set => _name.Value = value;
399+
}
400+
}
401+
```
402+
403+
<details><summary><b>Generated code</b></summary>
404+
<br />
405+
406+
`ItemViewModel.BackingFields.g.cs`
407+
408+
```csharp
409+
partial class ItemViewModel
410+
{
411+
[global::System.CodeDom.Compiler.GeneratedCode("UnityMvvmToolkit.Generator", "1.0.0.0")]
412+
[global::UnityMvvmToolkit.Core.Attributes.Observable(nameof(Name))]
413+
private readonly global::UnityMvvmToolkit.Core.Interfaces.IProperty<string> _name = new global::UnityMvvmToolkit.Core.Property<string>();
414+
}
415+
```
416+
417+
</details>
418+
419+
> **Note:** The [UnityMvvmToolkit.Generator](https://github.com/LibraStack/UnityMvvmToolkit.Generator) is available exclusively for my [patrons](https://patreon.com/DimaChebanov).
420+
389421
### Command & Command\<T\>
390422

391423
The `Command` and `Command<T>` are `ICommand` implementations that can expose a method or delegate to the view. These types act as a way to bind commands between the viewmodel and UI elements.
@@ -1131,9 +1163,9 @@ Now we can use the `CustomViewModelProvider` as follows.
11311163
In this example, `Label1` and `Label2` will display the text "Main Context", while `Label3` will display the text "Custom Context".
11321164

11331165
### Source code generator
1134-
1166+
11351167
The best way to speed up the creation of custom `VisualElement` is to use source code generators. With this powerful tool, you can achieve the same great results with minimal boilerplate code and focus on what really matters: programming!
1136-
1168+
11371169
Let's create the `BindableImage` control, but this time using source code generators.
11381170

11391171
For a visual element without bindings, we will use a [UnityUxmlGenerator](https://github.com/LibraStack/UnityUxmlGenerator).
@@ -1148,12 +1180,12 @@ public partial class Image : VisualElement
11481180
}
11491181
}
11501182
```
1151-
1183+
11521184
<details><summary><b>Generated code</b></summary>
11531185
<br />
11541186

11551187
`Image.UxmlFactory.g.cs`
1156-
1188+
11571189
```csharp
11581190
partial class Image
11591191
{
@@ -1163,9 +1195,9 @@ partial class Image
11631195
}
11641196
}
11651197
```
1166-
1198+
11671199
</details>
1168-
1200+
11691201
For a bindable visual element, we will use a [UnityMvvmToolkit.Generator](https://github.com/LibraStack/UnityMvvmToolkit.Generator).
11701202

11711203
```csharp
@@ -1191,12 +1223,12 @@ public partial class BindableImage : Image
11911223
}
11921224
}
11931225
```
1194-
1226+
11951227
<details><summary><b>Generated code</b></summary>
11961228
<br />
11971229

11981230
`BindableImage.Bindings.g.cs`
1199-
1231+
12001232
```csharp
12011233
partial class BindableImage : global::UnityMvvmToolkit.Core.Interfaces.IBindableElement
12021234
{
@@ -1298,7 +1330,7 @@ partial class BindableImage
12981330
}
12991331
}
13001332
```
1301-
1333+
13021334
</details>
13031335

13041336
As you can see, using [UnityUxmlGenerator](https://github.com/LibraStack/UnityUxmlGenerator) and [UnityMvvmToolkit.Generator](https://github.com/LibraStack/UnityMvvmToolkit.Generator) we can achieve the same results but with just a few lines of code.

0 commit comments

Comments
 (0)