Update Avalonia to 11.0

This commit is contained in:
Maksym Katsydan 2022-11-12 02:23:58 -05:00
parent dc2eefc43d
commit 1246a011c9
12 changed files with 83 additions and 184 deletions

View File

@ -1,18 +1,8 @@
<Application xmlns="https://github.com/avaloniaui" <Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaCoreRTDemo.App"> x:Class="AvaloniaCoreRTDemo.App">
<NativeMenu.Menu>
<NativeMenu>
<NativeMenuItem Header="About" Gesture="cmd+b" IsEnabled="{Binding AboutEnabled}" Command="{Binding HelpAboutMethod}"/>
</NativeMenu>
</NativeMenu.Menu>
<Application.Styles>
<StyleInclude Source="avares://Avalonia.Themes.Default/Accents/BaseDark.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Accents/BaseLight.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/DefaultTheme.xaml"/>
</Application.Styles>
<Application.Resources> <Application.Resources>
<FluentTheme x:Key="fluentLight" Mode="Light" /> <FluentTheme x:Key="fluentTheme" Mode="Light" />
<FluentTheme x:Key="fluentDark" Mode="Dark" /> <SimpleTheme x:Key="simpleTheme" Mode="Light" />
</Application.Resources> </Application.Resources>
</Application> </Application>

View File

@ -4,7 +4,7 @@ using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using Avalonia.Styling; using Avalonia.Styling;
using Avalonia.Themes.Fluent; using Avalonia.Themes.Fluent;
using Avalonia.Themes.Simple;
using AvaloniaCoreRTDemo.Interfaces; using AvaloniaCoreRTDemo.Interfaces;
using AvaloniaCoreRTDemo.Windows; using AvaloniaCoreRTDemo.Windows;
@ -12,11 +12,8 @@ namespace AvaloniaCoreRTDemo
{ {
public sealed class App : Application, IThemeSwitch public sealed class App : Application, IThemeSwitch
{ {
private IStyle _baseLight; private FluentTheme _fluentTheme;
private IStyle _baseDark; private SimpleTheme _simpleTheme;
private IStyle _fluentLight;
private IStyle _fluentDark;
private ApplicationTheme _currentTheme; private ApplicationTheme _currentTheme;
@ -31,7 +28,7 @@ namespace AvaloniaCoreRTDemo
this.InitializeThemes(); this.InitializeThemes();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{ {
desktop.MainWindow = !Utilities.IsOSX ? new MainWindow() : new MainWindowMacOS(); desktop.MainWindow = new MainWindow();
this.DataContext = desktop.MainWindow.DataContext; this.DataContext = desktop.MainWindow.DataContext;
} }
base.OnFrameworkInitializationCompleted(); base.OnFrameworkInitializationCompleted();
@ -39,15 +36,11 @@ namespace AvaloniaCoreRTDemo
private void InitializeThemes() private void InitializeThemes()
{ {
this._baseDark = this.Styles[0]; this._fluentTheme = (FluentTheme)this.Resources["fluentTheme"]!;
this._baseLight = this.Styles[1]; this._simpleTheme = (SimpleTheme)this.Resources["simpleTheme"]!;
Styles.Add(_fluentTheme);
this.Styles.Remove(this._baseDark); this._currentTheme = ApplicationTheme.FluentLight;
this._fluentLight = (FluentTheme)this.Resources["fluentLight"]!;
this._fluentDark = (FluentTheme)this.Resources["fluentDark"]!;
this._currentTheme = ApplicationTheme.DefaultLight;
} }
ApplicationTheme IThemeSwitch.Current => this._currentTheme; ApplicationTheme IThemeSwitch.Current => this._currentTheme;
@ -57,25 +50,21 @@ namespace AvaloniaCoreRTDemo
this._currentTheme = theme; this._currentTheme = theme;
switch (theme) switch (theme)
{ {
case ApplicationTheme.DefaultLight: case ApplicationTheme.SimpleLight:
this.Styles[0] = this._baseLight; this._simpleTheme.Mode = SimpleThemeMode.Light;
this.Styles.Remove(this._baseDark); this.Styles[0] = this._simpleTheme;
break; break;
case ApplicationTheme.DefaultDark: case ApplicationTheme.SimpleDark:
this.Styles[0] = this._baseDark; this._simpleTheme.Mode = SimpleThemeMode.Dark;
this.Styles.Remove(this._baseLight); this.Styles[0] = this._simpleTheme;
break; break;
case ApplicationTheme.FluentLight: case ApplicationTheme.FluentLight:
this.Styles[0] = this._fluentLight; this._fluentTheme.Mode = FluentThemeMode.Light;
this.Styles.Remove(this._fluentDark); this.Styles[0] = this._fluentTheme;
if (!this.Styles.Contains(this._baseLight))
this.Styles.Add(this._baseLight);
break; break;
case ApplicationTheme.FluentDark: case ApplicationTheme.FluentDark:
this.Styles[0] = this._fluentDark; this._fluentTheme.Mode = FluentThemeMode.Dark;
this.Styles.Remove(this._baseLight); this.Styles[0] = this._fluentTheme;
if (!this.Styles.Contains(this._baseDark))
this.Styles.Add(this._baseDark);
break; break;
} }
} }

View File

@ -4,8 +4,8 @@ namespace AvaloniaCoreRTDemo
{ {
public enum ApplicationTheme : Byte public enum ApplicationTheme : Byte
{ {
DefaultLight = 0, SimpleLight = 0,
DefaultDark = 1, SimpleDark = 1,
FluentLight = 2, FluentLight = 2,
FluentDark = 3, FluentDark = 3,
} }

View File

@ -9,10 +9,10 @@
<InvariantGlobalization>true</InvariantGlobalization> <InvariantGlobalization>true</InvariantGlobalization>
<!--https://docs.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options--> <!--https://docs.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options-->
<TrimmerDefaultAction>link</TrimmerDefaultAction> <TrimmerDefaultAction>link</TrimmerDefaultAction>
<IsWindows Condition="$([MSBuild]::IsOSPlatform('Windows'))">true</IsWindows> <IsWindows Condition="$([MSBuild]::IsOSPlatform('Windows'))">true</IsWindows>
<IsLinux Condition="$([MSBuild]::IsOSPlatform('Linux'))">true</IsLinux> <IsLinux Condition="$([MSBuild]::IsOSPlatform('Linux'))">true</IsLinux>
<IsOSX Condition="$([MSBuild]::IsOSPlatform('OSX'))">true</IsOSX> <IsOSX Condition="$([MSBuild]::IsOSPlatform('OSX'))">true</IsOSX>
<DefineConstants Condition="'$(IsOSX)'=='true'">OSX</DefineConstants>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
@ -30,31 +30,34 @@
<ItemGroup> <ItemGroup>
<AvaloniaResource Include="Assets/**" /> <AvaloniaResource Include="Assets/**" />
<EmbeddedResource Include="Images/**" /> <AvaloniaResource Include="Images/**" />
<None Update="avalonia.png" CopyToPublishDirectory="PreserveNewest">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="dotnet.png" CopyToPublishDirectory="PreserveNewest">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets/app.icns" CopyToPublishDirectory="PreserveNewest" Condition="$(RuntimeIdentifier.StartsWith('osx'))"> <None Update="Assets/app.icns" CopyToPublishDirectory="PreserveNewest" Condition="$(RuntimeIdentifier.StartsWith('osx'))">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="dotnet.png">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="avalonia.png">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.16" /> <PackageReference Include="Avalonia" Version="11.0.0-preview4" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.16" /> <PackageReference Include="Avalonia.Desktop" Version="11.0.0-preview4" />
<PackageReference Include="Avalonia.Native" Version="0.10.16" /> <PackageReference Include="Avalonia.ReactiveUI" Version="11.0.0-preview4" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.16" /> <PackageReference Include="Avalonia.Themes.Simple" Version="11.0.0-preview4" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.0-preview4" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.--> <!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.16" /> <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.0-preview4" />
<!--Condition below is needed to generate macOS App only.--> <!--Condition below is needed to generate macOS App only.-->
<PackageReference Include="Dotnet.Bundle" Version="*" Condition="$(RuntimeIdentifier.StartsWith('osx'))" /> <PackageReference Include="Dotnet.Bundle" Version="*" Condition="$(RuntimeIdentifier.StartsWith('osx'))" />
</ItemGroup> </ItemGroup>
<!-- Information for Dotnet.Bundle to Generate macOS app--> <!-- Information for Dotnet.Bundle to Generate macOS app-->
<PropertyGroup Condition="'$(IsOSX)'=='true'"> <PropertyGroup>
<CFBundleName>$(AssemblyName)</CFBundleName> <CFBundleName>$(AssemblyName)</CFBundleName>
<CFBundleDisplayName>$(AssemblyName)</CFBundleDisplayName> <CFBundleDisplayName>$(AssemblyName)</CFBundleDisplayName>
<CFBundleIdentifier>com.$(username).$(AssemblyName)</CFBundleIdentifier> <CFBundleIdentifier>com.$(username).$(AssemblyName)</CFBundleIdentifier>
@ -77,32 +80,4 @@
<RuntimeHostConfigurationOption Include="Switch.System.Reflection.Assembly.SimulatedCallingAssembly" Value="true" /> <RuntimeHostConfigurationOption Include="Switch.System.Reflection.Assembly.SimulatedCallingAssembly" Value="true" />
</ItemGroup> </ItemGroup>
<!--App axaml-->
<ItemGroup>
<Compile Update="App.axaml.cs">
<DependentUpon>App.axaml</DependentUpon>
</Compile>
</ItemGroup>
<!--Cross platform xaml-->
<ItemGroup>
<Compile Update="Controls/MainControl.axaml.cs">
<DependentUpon>MainControl.axaml</DependentUpon>
</Compile>
<Compile Update="Windows/AboutWindow.axaml.cs">
<DependentUpon>AboutWindow.axaml</DependentUpon>
</Compile>
</ItemGroup>
<!--No-OSX only xaml-->
<ItemGroup Condition="'$(IsOSX)'!='true'">
<Compile Update="Windows/MainWindow.axaml.cs">
<DependentUpon>MainWindow.axaml</DependentUpon>
</Compile>
</ItemGroup>
<!--OSX only xaml-->
<ItemGroup Condition="'$(IsOSX)'=='true'">
<Compile Update="Windows/MainWindowMacOS.axaml.cs">
<DependentUpon>MainWindowMacOS.axaml</DependentUpon>
</Compile>
</ItemGroup>
</Project> </Project>

View File

@ -18,13 +18,10 @@ namespace AvaloniaCoreRTDemo.Controls.ViewModels
public MainViewModel() public MainViewModel()
{ {
this._dotNetImage = Utilities.GetImageFromFile(GetImageFullPath("dotnet.png")); this._dotNetImage = Utilities.GetImageFromFile("dotnet.png");
this._avaloniaImage = Utilities.GetImageFromFile(GetImageFullPath("avalonia.png")); this._avaloniaImage = Utilities.GetImageFromFile("avalonia.png");
} }
private static String GetImageFullPath(String fileName)
=> Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
~MainViewModel() ~MainViewModel()
{ {
this._dotNetImage.Dispose(); this._dotNetImage.Dispose();

View File

@ -12,10 +12,6 @@ namespace AvaloniaCoreRTDemo
// Avalonia configuration, don't remove; also used by visual designer. // Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp() public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>().UsePlatformDetect() => AppBuilder.Configure<App>().UsePlatformDetect()
.UseAvaloniaNativeOSX()
.LogToTrace(); .LogToTrace();
private static AppBuilder UseAvaloniaNativeOSX(this AppBuilder appBuilder)
=> Utilities.IsOSX ? appBuilder.UseAvaloniaNative() : appBuilder;
} }
} }

View File

@ -1,10 +1,9 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Avalonia;
using Avalonia.Media.Imaging; using Avalonia.Media.Imaging;
using Avalonia.Platform;
namespace AvaloniaCoreRTDemo namespace AvaloniaCoreRTDemo
{ {
@ -15,25 +14,24 @@ namespace AvaloniaCoreRTDemo
public static Bitmap GetImageFromResources(String fileName) public static Bitmap GetImageFromResources(String fileName)
{ {
Assembly asm = Assembly.GetExecutingAssembly(); var assetLoader = AvaloniaLocator.Current.GetRequiredService<IAssetLoader>();
String resourceName = asm.GetManifestResourceNames().FirstOrDefault(str => str.EndsWith(fileName)); using var assetStream = assetLoader.Open(new Uri($"avares://AvaloniaCoreRTDemo/Images/{fileName}"));
if (resourceName != null) return new Bitmap(assetStream);
using (Stream bitmapStream = asm.GetManifestResourceStream(resourceName))
return new Bitmap(bitmapStream);
else
return default;
} }
public static Bitmap GetImageFromFile(String path) public static Bitmap GetImageFromFile(String path)
{ {
try try
{ {
return new Bitmap(path); return new Bitmap(GetImageFullPath(path));
} }
catch (Exception) catch (Exception)
{ {
return GetImageFromResources("broken-link.png"); return GetImageFromResources("broken-link.png");
} }
} }
private static String GetImageFullPath(String fileName)
=> Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
} }
} }

View File

@ -2,20 +2,28 @@
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" 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"> Width="640" Height="480" WindowStartupLocation="CenterScreen" Title="AvaloniaCoreRTDemo" Icon="avares://AvaloniaCoreRTDemo/Assets/app.ico" MinWidth="400" MinHeight="350">
<DockPanel> <DockPanel>
<Menu DockPanel.Dock="Top"> <NativeMenuBar DockPanel.Dock="Top" />
<MenuItem Header="_File">
<MenuItem Header="_Exit" Command="{Binding FileExitCommand}"/>
</MenuItem>
<MenuItem Header="_Theme">
<MenuItem Header="Default Light" IsEnabled="{Binding DefaultLightEnabled}" Command="{Binding DefaultLightMethod}"/>
<MenuItem Header="Default Dark" IsEnabled="{Binding DefaultDarkEnabled}" Command="{Binding DefaultDarkMethod}"/>
<MenuItem Header="Fluent Light" IsEnabled="{Binding FluentLightEnabled}" Command="{Binding FluentLightMethod}"/>
<MenuItem Header="Fluent Dark" IsEnabled="{Binding FluentDarkEnabled}" Command="{Binding FluentDarkMethod}"/>
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_About" IsEnabled="{Binding AboutEnabled}" Command="{Binding HelpAboutMethod}"/>
</MenuItem>
</Menu>
<controls:MainControl/> <controls:MainControl/>
</DockPanel> </DockPanel>
<NativeMenu.Menu>
<NativeMenu>
<NativeMenuItem Header="File">
<NativeMenuItem.Menu>
<NativeMenu>
<NativeMenuItem Header="Exit" Gesture="cmd+e" Command="{Binding FileExitCommand}"/>
</NativeMenu>
</NativeMenuItem.Menu>
</NativeMenuItem>
<NativeMenuItem Header="Theme">
<NativeMenuItem.Menu>
<NativeMenu>
<NativeMenuItem Header="Default Light" IsEnabled="{Binding DefaultLightEnabled}" Command="{Binding DefaultLightMethod}"/>
<NativeMenuItem Header="Default Dark" IsEnabled="{Binding DefaultDarkEnabled}" Command="{Binding DefaultDarkMethod}"/>
<NativeMenuItem Header="Fluent Light" IsEnabled="{Binding FluentLightEnabled}" Command="{Binding FluentLightMethod}"/>
<NativeMenuItem Header="Fluent Dark" IsEnabled="{Binding FluentDarkEnabled}" Command="{Binding FluentDarkMethod}"/>
</NativeMenu>
</NativeMenuItem.Menu>
</NativeMenuItem>
</NativeMenu>
</NativeMenu.Menu>
</Window> </Window>

View File

@ -1,26 +0,0 @@
<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.MainWindowMacOS"
Width="640" Height="480" WindowStartupLocation="CenterScreen" Title="AvaloniaCoreRTDemo" Icon="avares://AvaloniaCoreRTDemo/Assets/app.ico" MinWidth="400" MinHeight="350">
<controls:MainControl/>
<NativeMenu.Menu>
<NativeMenu>
<NativeMenuItem Header="File">
<NativeMenuItem.Menu>
<NativeMenu>
<NativeMenuItem Header="Exit" Gesture="cmd+e" Command="{Binding FileExitCommand}"/>
</NativeMenu>
</NativeMenuItem.Menu>
</NativeMenuItem>
<NativeMenuItem Header="Theme">
<NativeMenuItem.Menu>
<NativeMenu>
<NativeMenuItem Header="Default Light" IsEnabled="{Binding DefaultLightEnabled}" Command="{Binding DefaultLightMethod}"/>
<NativeMenuItem Header="Default Dark" IsEnabled="{Binding DefaultDarkEnabled}" Command="{Binding DefaultDarkMethod}"/>
<NativeMenuItem Header="Fluent Light" IsEnabled="{Binding FluentLightEnabled}" Command="{Binding FluentLightMethod}"/>
<NativeMenuItem Header="Fluent Dark" IsEnabled="{Binding FluentDarkEnabled}" Command="{Binding FluentDarkMethod}"/>
</NativeMenu>
</NativeMenuItem.Menu>
</NativeMenuItem>
</NativeMenu>
</NativeMenu.Menu>
</Window>

View File

@ -1,28 +0,0 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using AvaloniaCoreRTDemo.Interfaces;
using AvaloniaCoreRTDemo.Windows.ViewModels;
namespace AvaloniaCoreRTDemo.Windows
{
public partial class MainWindowMacOS : Window, IMainWindow
{
public MainWindowMacOS()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
this.DataContext = new MainViewModel<MainWindowMacOS>(this);
}
IThemeSwitch IMainWindow.ThemeSwitch => App.Current as IThemeSwitch;
}
}

View File

@ -51,8 +51,8 @@ namespace AvaloniaCoreRTDemo.Windows.ViewModels
this.ChangeTheme(window.ThemeSwitch.Current); this.ChangeTheme(window.ThemeSwitch.Current);
} }
public void DefaultLightMethod() => this.ChangeTheme(ApplicationTheme.DefaultLight); public void DefaultLightMethod() => this.ChangeTheme(ApplicationTheme.SimpleLight);
public void DefaultDarkMethod() => this.ChangeTheme(ApplicationTheme.DefaultDark); public void DefaultDarkMethod() => this.ChangeTheme(ApplicationTheme.SimpleDark);
public void FluentLightMethod() => this.ChangeTheme(ApplicationTheme.FluentLight); public void FluentLightMethod() => this.ChangeTheme(ApplicationTheme.FluentLight);
public void FluentDarkMethod() => this.ChangeTheme(ApplicationTheme.FluentDark); public void FluentDarkMethod() => this.ChangeTheme(ApplicationTheme.FluentDark);
public override void HelpAboutMethod() => base.RunHelpAbout(this._window); public override void HelpAboutMethod() => base.RunHelpAbout(this._window);
@ -62,8 +62,8 @@ namespace AvaloniaCoreRTDemo.Windows.ViewModels
private void ChangeTheme(ApplicationTheme theme) private void ChangeTheme(ApplicationTheme theme)
{ {
this.DefaultLightEnabled = theme != ApplicationTheme.DefaultLight && theme != ApplicationTheme.FluentLight; this.DefaultLightEnabled = theme != ApplicationTheme.SimpleLight && theme != ApplicationTheme.FluentLight;
this.DefaultDarkEnabled = theme != ApplicationTheme.DefaultDark && theme != ApplicationTheme.FluentDark; this.DefaultDarkEnabled = theme != ApplicationTheme.SimpleDark && theme != ApplicationTheme.FluentDark;
this.FluentLightEnabled = theme != ApplicationTheme.FluentLight; this.FluentLightEnabled = theme != ApplicationTheme.FluentLight;
this.FluentDarkEnabled = theme != ApplicationTheme.FluentDark; this.FluentDarkEnabled = theme != ApplicationTheme.FluentDark;
this._window.ThemeSwitch?.ChangeTheme(theme); this._window.ThemeSwitch?.ChangeTheme(theme);

View File

@ -41,7 +41,7 @@ namespace AvaloniaCoreRTDemo.Windows.ViewModels
private static Boolean IsDarkTheme(ApplicationTheme? theme) private static Boolean IsDarkTheme(ApplicationTheme? theme)
=> theme switch => theme switch
{ {
ApplicationTheme.DefaultDark => true, ApplicationTheme.SimpleDark => true,
ApplicationTheme.FluentDark => true, ApplicationTheme.FluentDark => true,
_ => false, _ => false,
}; };