6.5 Writing AXAML Code
- When generating AXAML code, use CustomControl with ControlTheme for Stand-Alone Control Style
- Purpose: Theme separation and minimizing style dependencies
6.5.1 AvaloniaUI Custom Control Library Project Structure
Recommended Project Structure:
YourAvaloniaSolution
āāā YourCustomControlProject1/
ā āāā Properties/
ā ā āāā AssemblyInfo.cs ā AssemblyInfo.cs definition
ā āāā Themes/
ā ā āāā Generic.axaml ā ControlTheme definition
ā ā āāā CustomButton1.axaml ā Individual control theme
ā ā āāā CustomTextBox1.axaml ā Individual control theme
ā āāā CustomButton1.cs
ā āāā CustomTextBox1.cs
āāā YourCustomControlProject2/
āāā Properties/
ā āāā AssemblyInfo.cs ā AssemblyInfo.cs definition
āāā Themes/
ā āāā Generic.axaml ā ControlTheme definition
ā āāā CustomButton2.axaml ā Individual control theme
ā āāā CustomTextBox2.axaml ā Individual control theme
āāā CustomButton2.cs
āāā CustomTextBox2.cs
6.6 ā ļø Distinguishing ResourceInclude vs MergeResourceInclude
- ResourceInclude: Used in regular ResourceDictionary files (Generic.axaml, Styles, etc.)
- MergeResourceInclude: Used only in Application.Resources (App.axaml)
Advantages:
- Complete separation of theme and logic based on ControlTheme
- Flexible style variations through CSS Classes
- State management via Pseudo Classes (:pointerover, :pressed, etc.)
- Theme modularization through ResourceInclude
- Work can be split by file for team collaboration
6.5.2 Key Differences Between WPF and AvaloniaUI
| Item |
WPF |
AvaloniaUI |
| File Extension |
.xaml |
.axaml |
| Style Definition |
Style + ControlTemplate |
ControlTheme |
| State Management |
Trigger, DataTrigger |
Pseudo Classes, Style Selector |
| CSS Support |
ā |
ā
(Classes attribute) |
| Resource Merging |
MergedDictionaries + ResourceDictionary |
MergedDictionaries + ResourceInclude |
| Dependency Props |
DependencyProperty |
StyledProperty, DirectProperty |