Use compiled bindings insteaed of reflection bindings

This commit is contained in:
Maksym Katsydan 2022-11-12 02:34:54 -05:00
parent 1246a011c9
commit 8459f9bf3b
6 changed files with 80 additions and 61 deletions

View File

@ -1,5 +1,8 @@
<UserControl xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" <UserControl xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="AvaloniaCoreRTDemo.Controls.MainControl"> xmlns:viewModels="clr-namespace:AvaloniaCoreRTDemo.Controls.ViewModels"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="AvaloniaCoreRTDemo.Controls.MainControl"
x:CompileBindings="True"
x:DataType="viewModels:MainViewModel">
<Grid ColumnDefinitions="*,*" RowDefinitions="Auto,Auto,*" Margin="32"> <Grid ColumnDefinitions="*,*" RowDefinitions="Auto,Auto,*" Margin="32">
<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" TextAlignment="Center" Margin="0,6">Welcome to Avalonia UI + NativeAOT!</TextBlock> <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" TextAlignment="Center" Margin="0,6">Welcome to Avalonia UI + NativeAOT!</TextBlock>
<Image Grid.Row="1" Grid.Column="0" Stretch="None" Source="{Binding DotNetImage}"/> <Image Grid.Row="1" Grid.Column="0" Stretch="None" Source="{Binding DotNetImage}"/>

View File

@ -1,6 +1,9 @@
<Window xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" <Window xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:AvaloniaCoreRTDemo.Windows.ViewModels"
mc:Ignorable="d" x:Class="AvaloniaCoreRTDemo.Windows.AboutWindow" d:DesignWidth="640" d:DesignHeight="256" mc:Ignorable="d" x:Class="AvaloniaCoreRTDemo.Windows.AboutWindow" d:DesignWidth="640" d:DesignHeight="256"
WindowState="Normal" WindowStartupLocation="CenterOwner" MinHeight="256" MinWidth="640" Height="256" Width="640" MaxHeight="256" MaxWidth="640" Title="About" CanResize="false" ShowInTaskbar="False" Icon="avares://AvaloniaCoreRTDemo/Assets/about.ico"> WindowState="Normal" WindowStartupLocation="CenterOwner" MinHeight="256" MinWidth="640" Height="256" Width="640" MaxHeight="256" MaxWidth="640" Title="About" CanResize="false" ShowInTaskbar="False" Icon="avares://AvaloniaCoreRTDemo/Assets/about.ico"
x:CompileBindings="True"
x:DataType="viewModels:AboutViewModel">
<Grid VerticalAlignment="Top" HorizontalAlignment="Left" ColumnDefinitions="Auto,Auto" RowDefinitions="Auto"> <Grid VerticalAlignment="Top" HorizontalAlignment="Left" ColumnDefinitions="Auto,Auto" RowDefinitions="Auto">
<Image Margin="0, 60" Grid.Row="0" Grid.Column="0" Stretch="None" Source="{Binding ComputerImage}" VerticalAlignment="Top" HorizontalAlignment="Left"> <Image Margin="0, 60" Grid.Row="0" Grid.Column="0" Stretch="None" Source="{Binding ComputerImage}" VerticalAlignment="Top" HorizontalAlignment="Left">
</Image> </Image>

View File

@ -1,6 +1,10 @@
<Window xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" <Window xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:AvaloniaCoreRTDemo.Controls" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="AvaloniaCoreRTDemo.Windows.MainWindow" xmlns:controls="clr-namespace:AvaloniaCoreRTDemo.Controls"
Width="640" Height="480" WindowStartupLocation="CenterScreen" Title="AvaloniaCoreRTDemo" Icon="avares://AvaloniaCoreRTDemo/Assets/app.ico" MinWidth="400" MinHeight="350"> xmlns:viewModels1="clr-namespace:AvaloniaCoreRTDemo.Windows.ViewModels"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="AvaloniaCoreRTDemo.Windows.MainWindow"
Width="640" Height="480" WindowStartupLocation="CenterScreen" Title="AvaloniaCoreRTDemo" Icon="avares://AvaloniaCoreRTDemo/Assets/app.ico" MinWidth="400" MinHeight="350"
x:CompileBindings="True"
x:DataType="viewModels1:MainViewModelBase">
<DockPanel> <DockPanel>
<NativeMenuBar DockPanel.Dock="Top" /> <NativeMenuBar DockPanel.Dock="Top" />
<controls:MainControl/> <controls:MainControl/>
@ -11,6 +15,7 @@
<NativeMenuItem.Menu> <NativeMenuItem.Menu>
<NativeMenu> <NativeMenu>
<NativeMenuItem Header="Exit" Gesture="cmd+e" Command="{Binding FileExitCommand}"/> <NativeMenuItem Header="Exit" Gesture="cmd+e" Command="{Binding FileExitCommand}"/>
<NativeMenuItem Header="About" Gesture="cmd+b" IsEnabled="{Binding AboutEnabled}" Command="{Binding HelpAboutMethod}"/>
</NativeMenu> </NativeMenu>
</NativeMenuItem.Menu> </NativeMenuItem.Menu>
</NativeMenuItem> </NativeMenuItem>

View File

@ -13,19 +13,19 @@ namespace AvaloniaCoreRTDemo.Windows.ViewModels
private readonly Boolean _darkTheme; private readonly Boolean _darkTheme;
public IBitmap ComputerImage => _computerImage; public IBitmap ComputerImage => _computerImage;
public static String NCores => Environment.ProcessorCount.ToString(); public String NCores => Environment.ProcessorCount.ToString();
public static String OS => RuntimeInformation.OSDescription; public String OS => RuntimeInformation.OSDescription;
public static String OSArch => RuntimeInformation.OSArchitecture.ToString(); public String OSArch => RuntimeInformation.OSArchitecture.ToString();
public static String OSVersion => Environment.OSVersion.ToString(); public String OSVersion => Environment.OSVersion.ToString();
public static String ComputerName => Environment.MachineName; public String ComputerName => Environment.MachineName;
public static String UserName => Environment.UserName; public String UserName => Environment.UserName;
public static String SystemPath => Environment.SystemDirectory; public String SystemPath => Environment.SystemDirectory;
public static String CurrentPath => Environment.CurrentDirectory; public String CurrentPath => Environment.CurrentDirectory;
public static String ProcessArch => RuntimeInformation.ProcessArchitecture.ToString(); public String ProcessArch => RuntimeInformation.ProcessArchitecture.ToString();
public static String RuntimeName => RuntimeInformation.FrameworkDescription; public String RuntimeName => RuntimeInformation.FrameworkDescription;
public static String RuntimePath => RuntimeEnvironment.GetRuntimeDirectory(); public String RuntimePath => RuntimeEnvironment.GetRuntimeDirectory();
public static String RuntimeVersion => RuntimeEnvironment.GetSystemVersion(); public String RuntimeVersion => RuntimeEnvironment.GetSystemVersion();
public static String FrameworkVersion => Environment.Version.ToString(); public String FrameworkVersion => Environment.Version.ToString();
private String ComputerImageName private String ComputerImageName
{ {

View File

@ -12,53 +12,20 @@ namespace AvaloniaCoreRTDemo.Windows.ViewModels
{ {
private readonly TWindow _window; private readonly TWindow _window;
private Boolean _defaultLightEnable = true;
private Boolean _defaultDarkEnable = true;
private Boolean _fluentLightEnable = true;
private Boolean _fluentDarkEnable = true;
public Boolean DefaultLightEnabled
{
get => this._defaultLightEnable;
set => this.RaiseAndSetIfChanged(ref this._defaultLightEnable, value);
}
public Boolean DefaultDarkEnabled
{
get => this._defaultDarkEnable;
set => this.RaiseAndSetIfChanged(ref this._defaultDarkEnable, value);
}
public Boolean FluentLightEnabled
{
get => this._fluentLightEnable;
set => this.RaiseAndSetIfChanged(ref this._fluentLightEnable, value);
}
public Boolean FluentDarkEnabled
{
get => this._fluentDarkEnable;
set => this.RaiseAndSetIfChanged(ref this._fluentDarkEnable, value);
}
public ReactiveCommand<Unit, Unit> FileExitCommand { get; }
public MainViewModel(TWindow window) public MainViewModel(TWindow window)
: base(window.ThemeSwitch) : base(window.ThemeSwitch)
{ {
this._window = window; this._window = window;
this.FileExitCommand = ReactiveCommand.Create(RunFileExit);
this.ChangeTheme(window.ThemeSwitch.Current); this.ChangeTheme(window.ThemeSwitch.Current);
} }
public void DefaultLightMethod() => this.ChangeTheme(ApplicationTheme.SimpleLight);
public void DefaultDarkMethod() => this.ChangeTheme(ApplicationTheme.SimpleDark);
public void FluentLightMethod() => this.ChangeTheme(ApplicationTheme.FluentLight);
public void FluentDarkMethod() => this.ChangeTheme(ApplicationTheme.FluentDark);
public override void HelpAboutMethod() => base.RunHelpAbout(this._window); public override void HelpAboutMethod() => base.RunHelpAbout(this._window);
private void RunFileExit() public override void DefaultLightMethod() => this.ChangeTheme(ApplicationTheme.SimpleLight);
=> Environment.Exit(0); public override void DefaultDarkMethod() => this.ChangeTheme(ApplicationTheme.SimpleDark);
public override void FluentLightMethod() => this.ChangeTheme(ApplicationTheme.FluentLight);
public override void FluentDarkMethod() => this.ChangeTheme(ApplicationTheme.FluentDark);
private void ChangeTheme(ApplicationTheme theme) private void ChangeTheme(ApplicationTheme theme)
{ {

View File

@ -1,5 +1,5 @@
using System; using System;
using System.Reactive;
using Avalonia.Controls; using Avalonia.Controls;
using AvaloniaCoreRTDemo.Interfaces; using AvaloniaCoreRTDemo.Interfaces;
@ -12,6 +12,10 @@ namespace AvaloniaCoreRTDemo.Windows.ViewModels
{ {
private readonly IThemeSwitch _themeSwitch; private readonly IThemeSwitch _themeSwitch;
private Boolean _aboutEnable = true; private Boolean _aboutEnable = true;
private Boolean _defaultLightEnable = true;
private Boolean _defaultDarkEnable = true;
private Boolean _fluentLightEnable = true;
private Boolean _fluentDarkEnable = true;
public Boolean AboutEnabled public Boolean AboutEnabled
{ {
@ -20,10 +24,47 @@ namespace AvaloniaCoreRTDemo.Windows.ViewModels
} }
public MainViewModelBase(IThemeSwitch window) public MainViewModelBase(IThemeSwitch window)
=> this._themeSwitch = window; {
this._themeSwitch = window;
this.FileExitCommand = ReactiveCommand.Create(RunFileExit);
}
public Boolean DefaultLightEnabled
{
get => this._defaultLightEnable;
set => this.RaiseAndSetIfChanged(ref this._defaultLightEnable, value);
}
public Boolean DefaultDarkEnabled
{
get => this._defaultDarkEnable;
set => this.RaiseAndSetIfChanged(ref this._defaultDarkEnable, value);
}
public Boolean FluentLightEnabled
{
get => this._fluentLightEnable;
set => this.RaiseAndSetIfChanged(ref this._fluentLightEnable, value);
}
public Boolean FluentDarkEnabled
{
get => this._fluentDarkEnable;
set => this.RaiseAndSetIfChanged(ref this._fluentDarkEnable, value);
}
public ReactiveCommand<Unit, Unit> FileExitCommand { get; }
public abstract void HelpAboutMethod(); public abstract void HelpAboutMethod();
public abstract void DefaultLightMethod();
public abstract void DefaultDarkMethod();
public abstract void FluentLightMethod();
public abstract void FluentDarkMethod();
private void RunFileExit()
=> Environment.Exit(0);
protected async void RunHelpAbout(Window currentWindow) protected async void RunHelpAbout(Window currentWindow)
{ {
if (this.AboutEnabled) if (this.AboutEnabled)