Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2febaa9

Browse files
Merge branch 'main' into develop
2 parents 3d3c8fe + 1631574 commit 2febaa9

File tree

1 file changed

+111
-14
lines changed

1 file changed

+111
-14
lines changed

‎README.md

Lines changed: 111 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -362,18 +362,32 @@ public class MyViewModel : IBindingContext
362362

363363
#### Wrapping a non-observable model
364364

365-
A common scenario, for instance, when working with collection items, is to create a wrapping "bindable" item model that relays properties of the collection item model, and raises the property value changed notifications when needed.
365+
A common scenario, for instance, when working with database items, is to create a wrapping "bindable" model that relays properties of the database model, and raises the property changed notifications when needed.
366366

367367
```csharp
368-
public class ItemViewModel : IBindingContext
368+
public class UserViewModel : IBindingContext
369369
{
370+
private readonly User _user;
371+
370372
[Observable(nameof(Name))]
371373
private readonly IProperty<string> _name = new Property<string>();
372374

375+
public UserViewModel(User user)
376+
{
377+
_user = user;
378+
_name.Value = user.Name;
379+
}
380+
373381
public string Name
374382
{
375-
get => _name.Value;
376-
set => _name.Value = value;
383+
get => _user.Name;
384+
set
385+
{
386+
if (_name.TrySetValue(value))
387+
{
388+
_user.Name = value;
389+
}
390+
}
377391
}
378392
}
379393
```
@@ -384,29 +398,41 @@ public class ItemViewModel : IBindingContext
384398
</ui:UXML>
385399
```
386400

387-
The `ItemViewModel` can be serialized and deserialized without any issues.
388-
389401
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).
390402

391403
```csharp
392-
public partial class ItemViewModel : IBindingContext
404+
public partial class UserViewModel : IBindingContext
393405
{
406+
private readonly User _user;
407+
408+
public UserViewModel(User user)
409+
{
410+
_user = user;
411+
_name.Value = user.Name;
412+
}
413+
394414
[WithObservableBackingField]
395415
public string Name
396416
{
397-
get => _name.Value;
398-
set => _name.Value = value;
417+
get => _user.Name;
418+
set
419+
{
420+
if (_name.TrySetValue(value))
421+
{
422+
_user.Name = value;
423+
}
424+
}
399425
}
400426
}
401427
```
402428

403429
<details><summary><b>Generated code</b></summary>
404430
<br />
405431

406-
`ItemViewModel.BackingFields.g.cs`
432+
`UserViewModel.BackingFields.g.cs`
407433

408434
```csharp
409-
partial class ItemViewModel
435+
partial class UserViewModel
410436
{
411437
[global::System.CodeDom.Compiler.GeneratedCode("UnityMvvmToolkit.Generator", "1.0.0.0")]
412438
[global::UnityMvvmToolkit.Core.Attributes.Observable(nameof(Name))]
@@ -419,15 +445,85 @@ partial class ItemViewModel
419445
Waiting for the [partial properties](https://github.com/dotnet/csharplang/issues/6420) support to make it even shorter.
420446

421447
```csharp
422-
public partial class ItemViewModel : IBindingContext
448+
public partial class UserViewModel : IBindingContext
423449
{
450+
private readonly User _user;
451+
452+
public UserViewModel(User user)
453+
{
454+
_user = user;
455+
_name.Value = user.Name;
456+
}
457+
424458
[WithObservableBackingField]
425459
public partial string Name { get; set; }
426460
}
427461
```
428462

429463
> **Note:** The [UnityMvvmToolkit.Generator](https://github.com/LibraStack/UnityMvvmToolkit.Generator) is available exclusively for my [patrons](https://patreon.com/DimaChebanov).
430464
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+
431527
### Command & Command\<T\>
432528

433529
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.
@@ -731,6 +827,7 @@ Once the `UnityMVVMToolkit` is installed, create a class `MyFirstViewModel` that
731827

732828
```csharp
733829
using UnityMvvmToolkit.Core;
830+
using UnityMvvmToolkit.Core.Interfaces;
734831

735832
public class MyFirstViewModel : IBindingContext
736833
{
@@ -870,7 +967,7 @@ public class TextFieldViewModel : IBindingContext
870967

871968
```xml
872969
<ui:UXML xmlns:uitk="UnityMvvmToolkit.UITK.BindableUIElements" ...>
873-
<uitk:BindableTextField binding-text-path="TextValue" />
970+
<uitk:BindableTextField binding-value-path="TextValue" />
874971
</ui:UXML>
875972
```
876973

@@ -900,7 +997,7 @@ public class DropdownFieldViewModel : IBindingContext
900997
};
901998

902999
Items = new ReadOnlyProperty<ObservableCollection<string>>(items);
903-
SelectedItem = new Property<string>("Value 1");
1000+
SelectedItem = new Property<string>(items[0]);
9041001
}
9051002

9061003
public IReadOnlyProperty<ObservableCollection<string>> Items { get; }

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /