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 2d25588

Browse files
Update readme file.
1 parent 3fda705 commit 2d25588

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

‎README.md

Lines changed: 44 additions & 8 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
{
375383
get => _name.Value;
376-
set => _name.Value = value;
384+
set
385+
{
386+
if (_name.TrySetValue(value))
387+
{
388+
_user.Name = value;
389+
}
390+
}
377391
}
378392
}
379393
```
@@ -389,24 +403,38 @@ The `ItemViewModel` can be serialized and deserialized without any issues.
389403
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).
390404

391405
```csharp
392-
public partial class ItemViewModel : IBindingContext
406+
public partial class UserViewModel : IBindingContext
393407
{
408+
private readonly User _user;
409+
410+
public UserViewModel(User user)
411+
{
412+
_user = user;
413+
_name.Value = user.Name;
414+
}
415+
394416
[WithObservableBackingField]
395417
public string Name
396418
{
397419
get => _name.Value;
398-
set => _name.Value = value;
420+
set
421+
{
422+
if (_name.TrySetValue(value))
423+
{
424+
_user.Name = value;
425+
}
426+
}
399427
}
400428
}
401429
```
402430

403431
<details><summary><b>Generated code</b></summary>
404432
<br />
405433

406-
`ItemViewModel.BackingFields.g.cs`
434+
`UserViewModel.BackingFields.g.cs`
407435

408436
```csharp
409-
partial class ItemViewModel
437+
partial class UserViewModel
410438
{
411439
[global::System.CodeDom.Compiler.GeneratedCode("UnityMvvmToolkit.Generator", "1.0.0.0")]
412440
[global::UnityMvvmToolkit.Core.Attributes.Observable(nameof(Name))]
@@ -419,8 +447,16 @@ partial class ItemViewModel
419447
Waiting for the [partial properties](https://github.com/dotnet/csharplang/issues/6420) support to make it even shorter.
420448

421449
```csharp
422-
public partial class ItemViewModel : IBindingContext
450+
public partial class UserViewModel : IBindingContext
423451
{
452+
private readonly User _user;
453+
454+
public UserViewModel(User user)
455+
{
456+
_user = user;
457+
_name.Value = user.Name;
458+
}
459+
424460
[WithObservableBackingField]
425461
public partial string Name { get; set; }
426462
}

0 commit comments

Comments
(0)

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