Skip to content

Commit 9b236d0

Browse files
authored
Update readme file.
1 parent 2d25588 commit 9b236d0

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,6 @@ public class UserViewModel : IBindingContext
398398
</ui:UXML>
399399
```
400400

401-
The `ItemViewModel` can be serialized and deserialized without any issues.
402-
403401
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).
404402

405403
```csharp
@@ -464,6 +462,68 @@ public partial class UserViewModel : IBindingContext
464462

465463
> **Note:** The [UnityMvvmToolkit.Generator](https://github.com/LibraStack/UnityMvvmToolkit.Generator) is available exclusively for my [patrons](https://patreon.com/DimaChebanov).
466464
465+
#### Serializable ViewModel
466+
467+
A common scenario, for instance, when working with collection items, is to create a "bindable" item that can be serialized.
468+
469+
```csharp
470+
public class ItemViewModel : ICollectionItem
471+
{
472+
[Observable(nameof(Name))]
473+
private readonly IProperty<string> _name = new Property<string>();
474+
475+
public int Id { get; set; }
476+
477+
public string Name
478+
{
479+
get => _name.Value;
480+
set => _name.Value = value;
481+
}
482+
}
483+
```
484+
485+
```xml
486+
<ui:UXML xmlns:uitk="UnityMvvmToolkit.UITK.BindableUIElements" ...>
487+
<uitk:BindableLabel binding-text-path="Name" />
488+
</ui:UXML>
489+
```
490+
491+
The `ItemViewModel` can be serialized and deserialized without any issues.
492+
493+
The same result, but using the `[WithObservableBackingField]` attribute from [UnityMvvmToolkit.Generator](https://github.com/LibraStack/UnityMvvmToolkit.Generator).
494+
495+
```csharp
496+
public partial class ItemViewModel : ICollectionItem
497+
{
498+
public int Id { get; set; }
499+
500+
[WithObservableBackingField]
501+
public string Name
502+
{
503+
get => _name.Value;
504+
set => _name.Value = value;
505+
}
506+
}
507+
```
508+
509+
<details><summary><b>Generated code</b></summary>
510+
<br />
511+
512+
`ItemViewModel.BackingFields.g.cs`
513+
514+
```csharp
515+
partial class ItemViewModel
516+
{
517+
[global::System.CodeDom.Compiler.GeneratedCode("UnityMvvmToolkit.Generator", "1.0.0.0")]
518+
[global::UnityMvvmToolkit.Core.Attributes.Observable(nameof(Name))]
519+
private readonly global::UnityMvvmToolkit.Core.Interfaces.IProperty<string> _name = new global::UnityMvvmToolkit.Core.Property<string>();
520+
}
521+
```
522+
523+
</details>
524+
525+
> **Note:** The [UnityMvvmToolkit.Generator](https://github.com/LibraStack/UnityMvvmToolkit.Generator) is available exclusively for my [patrons](https://patreon.com/DimaChebanov).
526+
467527
### Command & Command\<T\>
468528

469529
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.

0 commit comments

Comments
 (0)