• Stars
    star
    322
  • Rank 130,398 (Top 3 %)
  • Language
    C#
  • Created almost 3 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Provides helpers for declarative ui in C#

Avalonia.Markup.Declarative

Write Avalonia UI with C#

Avalonia.Markup.Declarative is the set of base classes and extension methods over Avalonia's controls to define views markup with C# code instead of having separate XAML files

Installation

for now this project available as a source code. So you need to clone or download repository and Add Avalonia.Markup.Declarative project to your solution.

There will be Nuget package available later.

Usage

public class MainView : ViewBase<MainViewModel>
{
    public static IValueConverter InverseBooleanConverter { get; } 
        = new FuncValueConverter<bool, bool>(b => !b);

    //This method is executed before View building
    protected override void OnCreated()
    {
        ViewModel = new MainViewModel();
    }

    //Define markup in Build method
    protected override object Build(MainViewModel vm) =>
        new Grid()
            
            .Styles(
                new Style<Button>(s => s.Class(":pointerover")) //make button red when pointer is over using avalonia styles
                    .Background(Brushes.Red)
            )

            .Cols("Auto, 100, *") // equivalent of Grid.ColumnDefintions property
            .Background(Brushes.Green) // the same as grid.Background = Brushes.Green
            .Children(
                
                new TextBlock()
                    .Text( @vm.TextVal ), // use @ character before to Bind control's property to ViewModel's property

                new TextBlock()
                    .Col(1) //equivalent of Grid.SetColumn(textBlock, 1)
                    .IsVisible( @vm.HideGreeting, //bind TextBlock.IsVisible to MainViewModel.HideGreeting property
                                bindingMode: BindingMode.OneWay, // we can set Binding mode if necessery
                                converter: InverseBooleanConverter ), //Set value converter to invert value
                    .Text( "Hello Avalonia" ), 

                new Button()
                    .Col(2) //equivalent of Grid.SetColumn(textBlock, 1)
                    // we don't actually need binding here, 
                    // so just direct set to Command on view model
                    .Command(vm.ClickButtonCommand) 
                    .Content("Click me") // Content = "Click me"
                    .Padding(8, 0, 0, 0) //Set left padding to 8
                    .With(ButtonStyle) //Execute LabelStyle method over TextBlock control 
            );

    private void ButtonStyle(Button b) => b
        .VerticalAlignment(VerticalAlignment.Center)
        .FontSize(12);
}

Hot reload support

ViewBase class automagiacally supports .Net 6 hot reload feature.

  • make sure that your view classes are located in the Assembly that doesn't contain any Xaml files, otherwise hot reload feature will always throw "need to rebuild" message.

Properties support on custom controls

There are two source generators to add markup extensions on your own controls. If you downloaded source code or clonned this repo, you have to add them by referencing Avalonia.Markup.Declarative.SourceGenerator project in csproj file like this:

	<ItemGroup>
		<ProjectReference Include="..\..\AvaloniaMarkup.Declarative.SourceGenerator\Avalonia.Markup.Declarative.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
	</ItemGroup>
  • make sure, that path to source generator project is correct relative to your project

MVU Pattern implementation

Inspired by blazor component layout. So basic component will looks like:

public class Component : ComponentBase
{
//markup part
    protected override object Build() =>
        new StackPanel()
            .Children(
                new TextBlock()
                    .Ref(out _textBlock1)
                    .Text("Hello world"),
                new TextBlock()
                    .Text(Bind(CounterText)),
                new Button()
                    .Content("Click me")
                    .OnClick(OnButtonClick)
            );
            
//code part
    [Inject] SampleDataService DataService { get; set; } = null!; //service injection

    public int Counter { get; set; }
    public string CounterText => $"Counter: {Counter}";  //no need to implement AvaloniaProperty or OnPropertyChanged behaviors, since component has registry of all properties and emits ProperyChanged event after changing state of component.

    private void OnButtonClick(RoutedEventArgs e)
    {
        Counter++;
        _textBlock1.Text = DataService.GetData();
        StateHasChanged(); //for now we have to call this method manually. In future there will be some additional triggers like user input, that will rise this method automatically
    }
}

More Repositories

1

Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
C#
25,320
star
2

AvaloniaEdit

Avalonia-based text editor (port of AvalonEdit)
C#
716
star
3

Avalonia.Samples

Avalonia.Samples aims to provide some minimal samples focusing on a particular issue at a time. This should help getting new users started.
C#
570
star
4

Citrus.Avalonia

Modern styles for Avalonia controls.
C#
542
star
5

avalonia-dotnet-templates

Avalonia Templates for `dotnet new`
C#
478
star
6

AvaloniaVS

Visual Studio Extension for Avalonia
C#
414
star
7

Live.Avalonia

In-app live reload for Avalonia applications.
C#
402
star
8

Avalonia.Xaml.Behaviors

Port of Windows UWP Xaml Behaviors for Avalonia Xaml.
C#
376
star
9

Avalonia.Controls.TreeDataGrid

A combined TreeView/DataGrid for Avalonia.
C#
268
star
10

Avalonia.Labs

Experimental Controls for Avalonia
C#
167
star
11

XamlControlsGallery

C#
159
star
12

AvaloniaMauiHybrid

Brings templated controls, pixel perfect rendering, animations, styling and composition to Maui
C#
152
star
13

AvaloniaUI.QuickGuides

C#
136
star
14

Avalonia.HtmlRenderer

Avalonia front-end for the HtmlRenderer project.
C#
112
star
15

AvaloniaVSCode

Visual Studio Code Extension for Avalonia UI
TypeScript
97
star
16

Avalonia.GIF

GIF image renderer and player for Avalonia UI Framework
C#
91
star
17

Avalonia.NameGenerator

Generates x:Name refs to Avalonia XAML controls.
C#
85
star
18

AvaloniaAutoGrid

C#
78
star
19

Avalonia.Essentials

Essential cross platform APIs for your mobile apps.
C#
65
star
20

Solitaire

C#
64
star
21

Wordle-onia

C#
60
star
22

ControlCatalogStandalone

C#
57
star
23

XamlPlayground

Avalonia Xaml Playground
C#
56
star
24

avalonia-docs

JavaScript
55
star
25

Documentation

AvaloniaUI documentation page source code
53
star
26

Avalonia.VSCodeExtension

This is an experimental VSCode extension. Don't expect it to work.
C#
50
star
27

Avalonia.MusicStore

C#
47
star
28

Avalonia-VSCode-Extension

An awesome extension for Visual Studio Code.
44
star
29

Avalonia.Lottie

Lottie/Bodymovin renderer and player for Avalonia UI Framework.
C#
29
star
30

Avalonia.AxamlAutoCompletion

C#
15
star
31

skiabuild

Makefile
6
star
32

MusicStoreTutorial

C#
6
star
33

Avalonia.TagsAndIdentation

TypeScript
5
star
34

NodeEditor

CSS
4
star
35

Avalonia.WebCam

Experimental cross-platform webcam interface for Avalonia UI Framework.
C#
3
star
36

Avalonia.Toolkit

2
star
37

.github

2
star