@@ -332,24 +332,20 @@ The following shows how to set up a simple command.
332
332
using UnityMvvmToolkit .Core ;
333
333
using UnityMvvmToolkit .Core .Interfaces ;
334
334
335
- public class CounterViewModel : ViewModel
335
+ public class CounterViewModel : IBindingContext
336
336
{
337
- private int _count ;
338
-
339
337
public CounterViewModel ()
340
338
{
339
+ Count = new Property <int >();
340
+
341
341
IncrementCommand = new Command (IncrementCount );
342
342
}
343
343
344
- public int Count
345
- {
346
- get => _count ;
347
- set => Set (ref _count , value );
348
- }
344
+ public IProperty <int > Count { get ; }
349
345
350
346
public ICommand IncrementCommand { get ; }
351
347
352
- private void IncrementCount () => Count ++ ;
348
+ private void IncrementCount () => Count . Value ++ ;
353
349
}
354
350
```
355
351
@@ -378,28 +374,27 @@ Key functionality:
378
374
Let's say we want to download an image from the web and display it as soon as it downloads.
379
375
380
376
``` csharp
381
- public class ImageViewerViewModel : ViewModel
377
+ public class ImageViewerViewModel : IBindingContext
382
378
{
379
+ [Observable (nameof (Image ))]
380
+ private readonly IProperty <Texture2D > _image ;
383
381
private readonly IImageDownloader _imageDownloader ;
384
- private Texture2D _texture ;
385
382
386
383
public ImageViewerViewModel (IImageDownloader imageDownloader )
387
384
{
385
+ _image = new Property <Texture2D >();
388
386
_imageDownloader = imageDownloader ;
387
+
389
388
DownloadImageCommand = new AsyncCommand (DownloadImageAsync );
390
389
}
391
390
392
- public Texture2D Image
393
- {
394
- get => _texture ;
395
- private set => Set (ref _texture , value );
396
- }
391
+ public Texture2D Image => _image .Value ;
397
392
398
393
public IAsyncCommand DownloadImageCommand { get ; }
399
394
400
395
private async UniTask DownloadImageAsync (CancellationToken cancellationToken )
401
396
{
402
- Image = await _imageDownloader .DownloadRandomImageAsync (cancellationToken );
397
+ _image . Value = await _imageDownloader .DownloadRandomImageAsync (cancellationToken );
403
398
}
404
399
}
405
400
```
@@ -420,7 +415,7 @@ With the related UI code.
420
415
To disable the ` BindableButton ` while an async operation is running, simply set the ` DisableOnExecution ` property of the ` AsyncCommand ` to ` true ` .
421
416
422
417
``` csharp
423
- public class ImageViewerViewModel : ViewModel
418
+ public class ImageViewerViewModel : IBindingContext
424
419
{
425
420
public ImageViewerViewModel (IImageDownloader imageDownloader )
426
421
{
@@ -433,7 +428,7 @@ public class ImageViewerViewModel : ViewModel
433
428
If you want to create an async command that supports cancellation, use the ` WithCancellation ` extension method.
434
429
435
430
``` csharp
436
- public class MyViewModel : ViewModel
431
+ public class MyViewModel : IBindingContext
437
432
{
438
433
public MyViewModel ()
439
434
{
@@ -532,23 +527,22 @@ Then you can use the `ThemeModeToBoolConverter` as in the following example.
532
527
Parameter value converter allows to convert a command parameter.
533
528
534
529
Built-in parameter value converters:
535
- - ParameterToStrConverter
536
530
- ParameterToIntConverter
537
531
- ParameterToFloatConverter
538
532
539
- By default, the converter is not needed if your command has a ` ReadOnlyMemory<char> ` parameter type.
533
+ By default, the converter is not needed if your command has a ` string ` parameter type.
540
534
541
535
``` csharp
542
- public class MyViewModel : ViewModel
536
+ public class MyViewModel : IBindingContext
543
537
{
544
538
public MyViewModel ()
545
539
{
546
- PrintParameterCommand = new Command <ReadOnlyMemory < char > >(PrintParameter );
540
+ PrintParameterCommand = new Command <string >(PrintParameter );
547
541
}
548
542
549
- public ICommand <ReadOnlyMemory < char > > PrintParameterCommand { get ; }
543
+ public ICommand <string > PrintParameterCommand { get ; }
550
544
551
- private void PrintParameter (ReadOnlyMemory < char > parameter )
545
+ private void PrintParameter (string parameter )
552
546
{
553
547
Debug .Log (parameter );
554
548
}
@@ -568,9 +562,9 @@ If you want to create your own parameter value converter, create a class that in
568
562
``` csharp
569
563
public class ParameterToIntConverter : ParameterValueConverter <int >
570
564
{
571
- public override int Convert (ReadOnlyMemory < char > parameter )
565
+ public override int Convert (string parameter )
572
566
{
573
- return int .Parse (parameter . Span );
567
+ return int .Parse (parameter );
574
568
}
575
569
}
576
570
```
@@ -590,7 +584,7 @@ public class MyView : DocumentView<MyViewModel>
590
584
Then you can use the ` ParameterToIntConverter ` as in the following example.
591
585
592
586
``` csharp
593
- public class MyViewModel : ViewModel
587
+ public class MyViewModel : IBindingContext
594
588
{
595
589
public MyViewModel ()
596
590
{
@@ -718,8 +712,8 @@ public class LabelViewModel : IBindingContext
718
712
{
719
713
public LabelViewModel ()
720
714
{
721
- IntValue = new ReadOnlyProperty <int >(55 );
722
- StrValue = new ReadOnlyProperty <string >(" 69" );
715
+ IntValue = new Property <int >(55 );
716
+ StrValue = new Property <string >(" 69" );
723
717
}
724
718
725
719
public IReadOnlyProperty <int > IntValue { get ; }
@@ -1036,10 +1030,14 @@ public abstract class BaseView<TBindingContext> : DocumentView<TBindingContext>
1036
1030
protected override IObjectProvider GetObjectProvider ()
1037
1031
{
1038
1032
return new BindingContextObjectProvider (new IValueConverter [] { new IntToStrConverter () })
1039
- // Finds and warmups all classes that implement IBindingContext.
1033
+ // Finds and warmups all classes from calling assembly that implement IBindingContext.
1040
1034
.WarmupAssemblyViewModels ()
1035
+ // Finds and warmups all classes from certain assembly that implement IBindingContext.
1036
+ .WarmupAssemblyViewModels (Assembly .GetExecutingAssembly ())
1041
1037
// Warmups a certain class.
1042
1038
.WarmupViewModel <CounterViewModel >()
1039
+ // Warmups a certain class.
1040
+ .WarmupViewModel (typeof (CounterViewModel ))
1043
1041
// Creates 5 instances to rent 'IProperty<string>' without any allocations.
1044
1042
.WarmupValueConverter <IntToStrConverter >(5 );
1045
1043
}
0 commit comments