-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More features: Validation sample, MRU sample
- Loading branch information
Showing
5 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
ReactiveUI.Samples.Basics/ViewModels/CalculatorViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Windows.Input; | ||
using ReactiveUI.Xaml; | ||
|
||
namespace ReactiveUI.Samples.Basics.ViewModels | ||
{ | ||
public class CalculatorViewModel : ReactiveValidatedObject | ||
{ | ||
private MemoizingMRUCache<int, int> _cache; | ||
|
||
public CalculatorViewModel() | ||
{ | ||
_cache = new MemoizingMRUCache<int, int>((x, ctx) => | ||
{ | ||
Thread.Sleep(1000); | ||
// Pretend this calculation isn’t cheap | ||
return x*10; | ||
}, 5); | ||
|
||
|
||
CalculateCommand = new ReactiveAsyncCommand(); | ||
(CalculateCommand as ReactiveAsyncCommand).RegisterAsyncTask<object>(o => | ||
{ | ||
return Task.Factory.StartNew(() => | ||
{ | ||
int top; | ||
bool cached = _cache.TryGet( Number, out top); | ||
if (cached) | ||
{ | ||
Result = 0; | ||
Thread.Sleep(1000); | ||
Result = top; | ||
} | ||
else | ||
{ | ||
top = _cache.Get(Number); | ||
for (int i = 0; i <= top; i++) | ||
{ | ||
Result = i; | ||
Thread.Sleep(100); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
} | ||
|
||
private int _Number; | ||
|
||
[Required] | ||
public int Number | ||
{ | ||
get { return _Number; } | ||
set { this.RaiseAndSetIfChanged(x => x.Number, value); } | ||
} | ||
|
||
public ICommand CalculateCommand { get; set; } | ||
|
||
private int _Result; | ||
|
||
public int Result | ||
{ | ||
get { return _Result; } | ||
set { this.RaiseAndSetIfChanged(x => x.Result, value); } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Reactive; | ||
using ReactiveUI; | ||
|
||
namespace ReactiveUI.Samples.Basics.ViewModels | ||
{ | ||
public class PersonViewModel : ReactiveValidatedObject | ||
{ | ||
|
||
public PersonViewModel() | ||
{ | ||
ValidationObservable.Subscribe(Observer.Create<IObservedChange<object, bool>>(x => | ||
{ | ||
IsValid = this.IsObjectValid(); | ||
})); | ||
} | ||
private int _Age; | ||
|
||
[ValidatesViaMethod(AllowBlanks = false, AllowNull = false, Name = "IsAgeValid", ErrorMessage = "Please enter a valid age 0..120")] | ||
public int Age | ||
{ | ||
get { return _Age; } | ||
set { this.RaiseAndSetIfChanged(x => x.Age, value); } | ||
} | ||
|
||
public bool IsAgeValid(int age) | ||
{ | ||
return ((age >= 0) && (age <= 120)); | ||
} | ||
|
||
private bool _IsValid; | ||
|
||
public bool IsValid | ||
{ | ||
get { return _IsValid; } | ||
set { this.RaiseAndSetIfChanged(x => x.IsValid, value); } | ||
} | ||
|
||
} | ||
} |