11 changed files with 293 additions and 0 deletions
@ -0,0 +1,7 @@ |
|||
<Application xmlns="https://github.com/avaloniaui" |
|||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
|||
x:Class="Generators.Sandbox.App"> |
|||
<Application.Styles> |
|||
<FluentTheme /> |
|||
</Application.Styles> |
|||
</Application> |
|||
@ -0,0 +1,20 @@ |
|||
using Avalonia; |
|||
using Avalonia.Markup.Xaml; |
|||
using Generators.Sandbox.ViewModels; |
|||
|
|||
namespace Generators.Sandbox; |
|||
|
|||
public class App : Application |
|||
{ |
|||
public override void Initialize() => AvaloniaXamlLoader.Load(this); |
|||
|
|||
public override void OnFrameworkInitializationCompleted() |
|||
{ |
|||
var view = new Views.SignUpView |
|||
{ |
|||
ViewModel = new SignUpViewModel() |
|||
}; |
|||
view.Show(); |
|||
base.OnFrameworkInitializationCompleted(); |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
using System; |
|||
using Avalonia.Controls; |
|||
using Avalonia.Styling; |
|||
|
|||
namespace Generators.Sandbox.Controls; |
|||
|
|||
public class CustomTextBox : TextBox, IStyleable |
|||
{ |
|||
Type IStyleable.StyleKey => typeof(TextBox); |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
<UserControl xmlns="https://github.com/avaloniaui" |
|||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
|||
xmlns:controls="using:Generators.Sandbox.Controls" |
|||
x:Class="Generators.Sandbox.Controls.SignUpView"> |
|||
<StackPanel> |
|||
<controls:CustomTextBox Margin="0 10 0 0" |
|||
x:Name="UserNameTextBox" |
|||
Watermark="Please, enter user name..." |
|||
UseFloatingWatermark="True" /> |
|||
<TextBlock x:Name="UserNameValidation" |
|||
Foreground="Red" |
|||
FontSize="12" /> |
|||
<TextBox Margin="0 10 0 0" |
|||
x:Name="PasswordTextBox" |
|||
Watermark="Please, enter your password..." |
|||
UseFloatingWatermark="True" |
|||
PasswordChar="*" /> |
|||
<TextBlock x:Name="PasswordValidation" |
|||
Foreground="Red" |
|||
FontSize="12" /> |
|||
<TextBox Margin="0 10 0 0" |
|||
x:Name="ConfirmPasswordTextBox" |
|||
Watermark="Please, confirm the password..." |
|||
UseFloatingWatermark="True" |
|||
PasswordChar="*" /> |
|||
<TextBlock x:Name="ConfirmPasswordValidation" |
|||
TextWrapping="Wrap" |
|||
Foreground="Red" |
|||
FontSize="12" /> |
|||
<TextBlock> |
|||
<TextBlock.Inlines> |
|||
<InlineCollection> |
|||
<Run x:Name="SignUpButtonDescription" /> |
|||
</InlineCollection> |
|||
</TextBlock.Inlines> |
|||
</TextBlock> |
|||
<Button Margin="0 10 0 5" |
|||
Content="Sign up" |
|||
x:Name="SignUpButton" /> |
|||
<TextBlock x:Name="CompoundValidation" |
|||
TextWrapping="Wrap" |
|||
Foreground="Red" |
|||
FontSize="12" /> |
|||
</StackPanel> |
|||
</UserControl> |
|||
@ -0,0 +1,54 @@ |
|||
using System; |
|||
using System.Reactive.Disposables; |
|||
using Avalonia.ReactiveUI; |
|||
using Generators.Sandbox.ViewModels; |
|||
using ReactiveUI; |
|||
using ReactiveUI.Validation.Extensions; |
|||
using ReactiveUI.Validation.Formatters; |
|||
|
|||
namespace Generators.Sandbox.Controls; |
|||
|
|||
/// <summary>
|
|||
/// This is a sample view class with typed x:Name references generated using
|
|||
/// .NET 5 source generators. The class has to be partial because x:Name
|
|||
/// references are living in a separate partial class file. See also:
|
|||
/// https://devblogs.microsoft.com/dotnet/new-c-source-generator-samples/
|
|||
/// </summary>
|
|||
public partial class SignUpView : ReactiveUserControl<SignUpViewModel> |
|||
{ |
|||
public SignUpView() |
|||
{ |
|||
// The InitializeComponent method is also generated automatically
|
|||
// and lives in the autogenerated part of the partial class.
|
|||
InitializeComponent(); |
|||
this.WhenActivated(disposables => |
|||
{ |
|||
this.Bind(ViewModel, x => x.UserName, x => x.UserNameTextBox.Text) |
|||
.DisposeWith(disposables); |
|||
this.Bind(ViewModel, x => x.Password, x => x.PasswordTextBox.Text) |
|||
.DisposeWith(disposables); |
|||
this.Bind(ViewModel, x => x.ConfirmPassword, x => x.ConfirmPasswordTextBox.Text) |
|||
.DisposeWith(disposables); |
|||
this.BindCommand(ViewModel, x => x.SignUp, x => x.SignUpButton) |
|||
.DisposeWith(disposables); |
|||
|
|||
this.BindValidation(ViewModel, x => x.UserName, x => x.UserNameValidation.Text) |
|||
.DisposeWith(disposables); |
|||
this.BindValidation(ViewModel, x => x.Password, x => x.PasswordValidation.Text) |
|||
.DisposeWith(disposables); |
|||
this.BindValidation(ViewModel, x => x.ConfirmPassword, x => x.ConfirmPasswordValidation.Text) |
|||
.DisposeWith(disposables); |
|||
|
|||
var newLineFormatter = new SingleLineFormatter(Environment.NewLine); |
|||
this.BindValidation(ViewModel, x => x.CompoundValidation.Text, newLineFormatter) |
|||
.DisposeWith(disposables); |
|||
|
|||
// The references to text boxes below are also auto generated.
|
|||
// Use Ctrl+Click in order to view the generated sources.
|
|||
UserNameTextBox.Text = "Joseph!"; |
|||
PasswordTextBox.Text = "1234"; |
|||
ConfirmPasswordTextBox.Text = "1234"; |
|||
SignUpButtonDescription.Text = "Press the button below to sign up."; |
|||
}); |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
<PropertyGroup> |
|||
<OutputType>Exe</OutputType> |
|||
<TargetFramework>net6.0</TargetFramework> |
|||
<IncludeAvaloniaGenerators>true</IncludeAvaloniaGenerators> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<AvaloniaResource Include="**\*.xaml"/> |
|||
<!-- Note this AdditionalFiles directive. --> |
|||
<AdditionalFiles Include="**\*.xaml"/> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="ReactiveUI.Validation" Version="3.0.22"/> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\packages\Avalonia\Avalonia.csproj"/> |
|||
<ProjectReference Include="..\..\src\Avalonia.Desktop\Avalonia.Desktop.csproj"/> |
|||
<ProjectReference Include="..\..\src\Avalonia.ReactiveUI\Avalonia.ReactiveUI.csproj"/> |
|||
<ProjectReference Include="..\..\src\Avalonia.Themes.Fluent\Avalonia.Themes.Fluent.csproj"/> |
|||
<ProjectReference Include="..\..\src\Avalonia.Fonts.Inter\Avalonia.Fonts.Inter.csproj"/> |
|||
</ItemGroup> |
|||
|
|||
<Import Project="..\..\build\BuildTargets.targets"/> |
|||
<Import Project="..\..\build\SourceGenerators.props"/> |
|||
</Project> |
|||
@ -0,0 +1,15 @@ |
|||
using Avalonia; |
|||
using Avalonia.ReactiveUI; |
|||
|
|||
namespace Generators.Sandbox; |
|||
|
|||
internal static class Program |
|||
{ |
|||
public static void Main(string[] args) => BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); |
|||
|
|||
private static AppBuilder BuildAvaloniaApp() |
|||
=> AppBuilder.Configure<App>() |
|||
.UseReactiveUI() |
|||
.UsePlatformDetect() |
|||
.LogToTrace(); |
|||
} |
|||
@ -0,0 +1,70 @@ |
|||
using System.Reactive; |
|||
using ReactiveUI; |
|||
using ReactiveUI.Validation.Extensions; |
|||
using ReactiveUI.Validation.Helpers; |
|||
|
|||
namespace Generators.Sandbox.ViewModels; |
|||
|
|||
public class SignUpViewModel : ReactiveValidationObject |
|||
{ |
|||
private string _userName = string.Empty; |
|||
private string _password = string.Empty; |
|||
private string _confirmPassword = string.Empty; |
|||
|
|||
public SignUpViewModel() |
|||
{ |
|||
this.ValidationRule( |
|||
vm => vm.UserName, |
|||
name => !string.IsNullOrWhiteSpace(name), |
|||
"UserName is required."); |
|||
|
|||
this.ValidationRule( |
|||
vm => vm.Password, |
|||
password => !string.IsNullOrWhiteSpace(password), |
|||
"Password is required."); |
|||
|
|||
this.ValidationRule( |
|||
vm => vm.Password, |
|||
password => password?.Length > 2, |
|||
password => $"Password should be longer, current length: {password.Length}"); |
|||
|
|||
this.ValidationRule( |
|||
vm => vm.ConfirmPassword, |
|||
confirmation => !string.IsNullOrWhiteSpace(confirmation), |
|||
"Confirm password field is required."); |
|||
|
|||
var passwordsObservable = |
|||
this.WhenAnyValue( |
|||
x => x.Password, |
|||
x => x.ConfirmPassword, |
|||
(password, confirmation) => |
|||
password == confirmation); |
|||
|
|||
this.ValidationRule( |
|||
vm => vm.ConfirmPassword, |
|||
passwordsObservable, |
|||
"Passwords must match."); |
|||
|
|||
SignUp = ReactiveCommand.Create(() => {}, this.IsValid()); |
|||
} |
|||
|
|||
public ReactiveCommand<Unit, Unit> SignUp { get; } |
|||
|
|||
public string UserName |
|||
{ |
|||
get => _userName; |
|||
set => this.RaiseAndSetIfChanged(ref _userName, value); |
|||
} |
|||
|
|||
public string Password |
|||
{ |
|||
get => _password; |
|||
set => this.RaiseAndSetIfChanged(ref _password, value); |
|||
} |
|||
|
|||
public string ConfirmPassword |
|||
{ |
|||
get => _confirmPassword; |
|||
set => this.RaiseAndSetIfChanged(ref _confirmPassword, value); |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
<Window xmlns="https://github.com/avaloniaui" |
|||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
|||
xmlns:controls="using:Generators.Sandbox.Controls" |
|||
x:Class="Generators.Sandbox.Views.SignUpView"> |
|||
<StackPanel Margin="10"> |
|||
<TextBlock Text="Sign Up" /> |
|||
<controls:SignUpView x:Name="SignUpControl" /> |
|||
</StackPanel> |
|||
</Window> |
|||
@ -0,0 +1,28 @@ |
|||
using System.Reactive.Disposables; |
|||
using Avalonia.ReactiveUI; |
|||
using Generators.Sandbox.ViewModels; |
|||
using ReactiveUI; |
|||
|
|||
namespace Generators.Sandbox.Views; |
|||
|
|||
/// <summary>
|
|||
/// This is a sample view class with typed x:Name references generated using
|
|||
/// .NET 5 source generators. The class has to be partial because x:Name
|
|||
/// references are living in a separate partial class file. See also:
|
|||
/// https://devblogs.microsoft.com/dotnet/new-c-source-generator-samples/
|
|||
/// </summary>
|
|||
public partial class SignUpView : ReactiveWindow<SignUpViewModel> |
|||
{ |
|||
public SignUpView() |
|||
{ |
|||
// The InitializeComponent method is also generated automatically
|
|||
// and lives in the autogenerated part of the partial class.
|
|||
InitializeComponent(); |
|||
this.WhenActivated(disposables => |
|||
{ |
|||
this.WhenAnyValue(view => view.ViewModel) |
|||
.BindTo(this, view => view.SignUpControl.ViewModel) |
|||
.DisposeWith(disposables); |
|||
}); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue