前提:
Avalonia版本11.0.0
1.配置文件
1.1 添加配置文件
1.2 读取配置文件
添加System.Configuration.ConfigurationManager
using Avalonia.Controls;
using System.Configuration;namespace AvaloniaApplication7.Views
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();// 读取配置项var DBOption = ConfigurationManager.AppSettings["DBOption"];}}
}
2.样式
2.1 添加样式文件至Assets文件夹下
<Styles xmlns="https://github.com/avaloniaui"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Design.PreviewWith></Design.PreviewWith><Style Selector="TextBlock"><Setter Property="Foreground" Value="White" /><Setter Property="FontSize" Value="16" /><Setter Property="FontWeight" Value="Normal"/></Style>
</Styles>
2.2 引用样式
打开App.axaml文件,添加<StyleInclude Source="你要用的样式文件.axaml"/>,添加后该样式在整个应用生效。
<Application.Styles><FluentTheme /><StyleInclude Source="/Assets/Styles/CommonStyles.axaml"/></Application.Styles>
3.资源文件
3.1 添加资源文件
<ResourceDictionary xmlns="https://github.com/avaloniaui"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><SolidColorBrush x:Key="WindowDefaultBackground">#336699</SolidColorBrush><!--Disabled颜色--><Color x:Key="DisabledColor">#545454</Color><Brush x:Key="EditWordColor">#FFFFFA</Brush><Brush x:Key="ComboBoxBackground">#003366</Brush><Brush x:Key="ComboBoxPopupBackground">#003366</Brush><SolidColorBrush x:Key="GridColor1">#003e62</SolidColorBrush><!--表头背景色--><SolidColorBrush x:Key="ColumnHeaderBackground">#666699</SolidColorBrush><!--行背景色--><SolidColorBrush x:Key="RowBackground">#111B59</SolidColorBrush><!--Grid背景色--><Brush x:Key="GridBackground">#06173E</Brush><!--Grid背景色--><Brush x:Key="GridBackground1">#111B59</Brush><SolidColorBrush x:Key="SystemControlBackgroundAltHighBrush">#1f497d</SolidColorBrush><!--Memu的下级Popup背景色--><SolidColorBrush x:Key="MenuFlyoutBackground">#052350</SolidColorBrush><!--默认字体--><FontFamily x:Key="DefaultFontFamily">Microsoft YaHei</FontFamily>
</ResourceDictionary>
3.2 引用资源文件
打开App.axaml文件,添加<MergeResourceInclude Source="你要用的样式文件.axaml"/>,添加后该样式在整个应用生效。
<Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><MergeResourceInclude Source="/Assets/CommonResource.axaml" /></ResourceDictionary.MergedDictionaries></ResourceDictionary>
</Application.Resources>
3.3 使用资源
<Style Selector="TextBox"><Setter Property="Margin" Value="0 -10 0 10" /><Setter Property="FontFamily" Value="{StaticResource DefaultFontFamily}" />
</Style>
4. 项目启动页面
打开App.axaml.cs文件