Avalonia如何更改全局背景色
1.项目下载地址:https://gitee.com/confusedkitten/avalonia-demo
2.UI库Semi.Avalonia,项目地址 https://github.com/irihitech/Semi.Avalonia
3.ColorView,使用Semi.Avalonia.ColorPicker,Nuget获取就行
4.样式预览
以下是测试专用方法,动态改变默认背景色,实际上你只要在你的资源里设置以下 <SolidColorBrush x:Key="WindowDefaultBackground">#336699</SolidColorBrush>,就可以了,这个资源文件在App.axaml中引用以下就生效了
5.WindowBackground.axaml
<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"xmlns:colorPicker="clr-namespace:Semi.Avalonia.ColorPicker;assembly=Semi.Avalonia.ColorPicker"xmlns:controls="using:Avalonia.Controls"mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"x:Class="AvaloniaDemo.Pages.WindowBackground"><StackPanel Spacing="20"><StackPanelVerticalAlignment="Top"Orientation="Horizontal"Spacing="20"><ColorView ColorSpectrumShape="Ring" x:Name="ColorV" Loaded="ColorVLoaded"/></StackPanel></StackPanel>
</UserControl>
6.WindowBackground.axaml.cs
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using System;namespace AvaloniaDemo.Pages;public partial class WindowBackground : UserControl
{public EventHandler<ColorChangedEventArgs>? ColorChangedHandler;public WindowBackground(){InitializeComponent();}private void ColorVLoaded(object? sender, RoutedEventArgs e){if (sender is ColorView colorView)colorView.ColorChanged += ColorChanged;}private void ColorChanged(object? sender, ColorChangedEventArgs e){ColorChangedHandler?.Invoke(this, e);}
}
7.MainWindow.axaml.cs
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Themes.Fluent;
using AvaloniaDemo.Pages;namespace AvaloniaDemo.Views
{public partial class MainWindow : Window{public MainWindow(){this.InitializeComponent();}private void TabLoaded(object? sender, Avalonia.Interactivity.RoutedEventArgs e){if (sender is WindowBackground windowBackground)windowBackground.ColorChangedHandler += ColorChanged;}private void ColorChanged(object? sender, ColorChangedEventArgs e){Resources["WindowDefaultBackground"] = new SolidColorBrush(e.NewColor);}private void InitializeComponent(){AvaloniaXamlLoader.Load(this);}}
}
8. 标题栏颜色问题
参考 https://github.com/AvaloniaUI/Avalonia/issues/186
解决方案是,设置 SystemDecorations="None",就是不要标题栏了,自己画一个:类似这个样子
<Window xmlns="https://github.com/avaloniaui"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:vm="using:AvaloniaDemo.ViewModels"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:pages="using:AvaloniaDemo.Pages"mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"x:Class="AvaloniaDemo.Views.MainWindow"x:DataType="vm:MainWindowViewModel"Icon="/Assets/avalonia-logo.ico"SystemDecorations="None"Title="AvaloniaDemo"><Design.DataContext><!-- This only sets the DataContext for the previewer in an IDE,to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) --><vm:MainWindowViewModel/></Design.DataContext><Grid RowDefinitions="40,*"><StackPanel Background="Pink" Orientation="Horizontal" VerticalAlignment="Center"><TextBlock Text="画一个标题栏"/><Button Content="×" FontSize="25" Margin="1010 0 0 0"/></StackPanel><TabControl Name="tab"Grid.Row="1"Margin="8"Padding="20,0,0,0"HorizontalAlignment="Stretch"TabStripPlacement="Left"><TabItem Header="DataGrid"><pages:DataGrid /></TabItem></TabControl></Grid>
</Window>