Skip to content

Commit

Permalink
More features: Validation sample, MRU sample
Browse files Browse the repository at this point in the history
  • Loading branch information
arielbh committed Sep 22, 2012
1 parent 011f60b commit 84e5efb
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 1 deletion.
21 changes: 20 additions & 1 deletion ReactiveUI.Samples.Basics/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Window x:Class="ReactiveUI.Samples.Basics.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
Title="MainWindow" Height="600" Width="525">
<StackPanel>
<Expander Header="Simple Notification" IsExpanded="True">
<StackPanel>
Expand All @@ -13,5 +13,24 @@
<ProgressBar Value="{Binding SlowProgress2}" Height="50"/>
</StackPanel>
</Expander>
<Expander Header="Validation" IsExpanded="True">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Age: "/>
<TextBox Text="{Binding Person.Age, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Width="100" Height="30"/>
</StackPanel>
<CheckBox Content="Is Valid Age?" IsChecked="{Binding Person.IsValid}" IsEnabled="False"/>
</StackPanel>
</Expander>
<Expander Header="Memoizing MRU" IsExpanded="True">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Calculate (x) => (x * 10): "/>
<TextBox Text="{Binding Calculator.Number, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Width="100" Height="30"/>
</StackPanel>
<Button Content="Calculate" Command="{Binding Calculator.CalculateCommand}"/>
<TextBlock Text="{Binding Calculator.Result}"/>
</StackPanel>
</Expander>
</StackPanel>
</Window>
3 changes: 3 additions & 0 deletions ReactiveUI.Samples.Basics/ReactiveUI.Samples.Basics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<HintPath>..\packages\reactiveui-xaml.3.99.3-beta\lib\Net40\ReactiveUI.Xaml.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Data" />
<Reference Include="System.Reactive.Core">
<HintPath>..\packages\Rx-Core.2.0.20823\lib\Net40\System.Reactive.Core.dll</HintPath>
Expand Down Expand Up @@ -95,7 +96,9 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="ViewModels\CalculatorViewModel.cs" />
<Compile Include="ViewModels\MainViewModel.cs" />
<Compile Include="ViewModels\PersonViewModel.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
74 changes: 74 additions & 0 deletions ReactiveUI.Samples.Basics/ViewModels/CalculatorViewModel.cs
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); }
}
}
}
21 changes: 21 additions & 0 deletions ReactiveUI.Samples.Basics/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public MainViewModel()
SlowProgress2 = Progress;
}));

Person = new PersonViewModel();
Calculator = new CalculatorViewModel();
}

private int _Progress;
Expand All @@ -70,6 +73,24 @@ public int SlowProgress2
set { this.RaiseAndSetIfChanged(x => x.SlowProgress2, value); }
}

private PersonViewModel _Person;

public PersonViewModel Person
{
get { return _Person; }
set { this.RaiseAndSetIfChanged(x => x.Person, value); }
}

private CalculatorViewModel _Calculator;

public CalculatorViewModel Calculator
{
get { return _Calculator; }
set { this.RaiseAndSetIfChanged(x => x.Calculator, value); }
}




}
}
41 changes: 41 additions & 0 deletions ReactiveUI.Samples.Basics/ViewModels/PersonViewModel.cs
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); }
}

}
}

0 comments on commit 84e5efb

Please sign in to comment.