Nlnet.Avalonia.Css.Behaviors 1.0.0-beta.3

This is a prerelease version of Nlnet.Avalonia.Css.Behaviors.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Nlnet.Avalonia.Css.Behaviors --version 1.0.0-beta.3                
NuGet\Install-Package Nlnet.Avalonia.Css.Behaviors -Version 1.0.0-beta.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nlnet.Avalonia.Css.Behaviors" Version="1.0.0-beta.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Nlnet.Avalonia.Css.Behaviors --version 1.0.0-beta.3                
#r "nuget: Nlnet.Avalonia.Css.Behaviors, 1.0.0-beta.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Nlnet.Avalonia.Css.Behaviors as a Cake Addin
#addin nuget:?package=Nlnet.Avalonia.Css.Behaviors&version=1.0.0-beta.3&prerelease

// Install Nlnet.Avalonia.Css.Behaviors as a Cake Tool
#tool nuget:?package=Nlnet.Avalonia.Css.Behaviors&version=1.0.0-beta.3&prerelease                

Nlnet.Avalonia.Css.Behaviors

This library provides extended behaviors for Acss. We have provided two behaviors, those belong to Acss, for instruction. We will continue to provide common behaviors for Acss. Any request for common behaviors could be sent to us at here.

  • combobox.popup.align. This behavior will align the selected item of the drop down list to the ComboBox.
  • window.esc.close. This behavior let the window accept esc key to close it.

Note that we haven't used the Avalonia.Xaml.Interactivity component. You can still independently use Acss Behavior and Avalonia.Xaml.Interactivity without worrying about any mutual impact or conflicts.

Custom Behaviors

Acss supports custom behaviors. Nlnet.Avalonia.Css.CompileGenerator package could be used for that.

  1. Add package Nlnet.Avalonia.Css.CompileGenerator.

  2. Create behavior declarer class that derived from AvaloniaObject and implement the IBehaviorDeclarer interface.

public partial class CustomA : AvaloniaObject, IBehaviorDeclarer
{
        
}
  1. Provide a extension method for users to use CustomA.
    /// <summary>
    /// Use customA behavior feature for default css builder.
    /// </summary>
    /// <param name="builder"></param>
    /// <returns></returns>
    public static AppBuilder UseCustomABehaviorForDefaultBuilder(this AppBuilder builder)
    {
        AcssBuilder.Default.BehaviorResolverManager.LoadResolver(new GenericBehaviorResolver<CustomA>());
        AcssBuilder.Default.BehaviorDeclarerManager.RegisterDeclarer<CustomA>(nameof(CustomA).ToLower());
        AcssBuilder.Default.BehaviorDeclarerManager.RegisterDeclarer<CustomA>(nameof(CustomA));

        return builder;
    }

    /// <summary>
    /// Use customA behavior feature.
    /// </summary>
    /// <param name="builder"></param>
    /// <param name="cssBuilder"></param>
    /// <returns></returns>
    public static AppBuilder UseCustomABehavior(this AppBuilder builder, IAcssBuilder cssBuilder)
    {
        cssBuilder.BehaviorResolverManager.LoadResolver(new GenericBehaviorResolver<CustomA>());
        cssBuilder.BehaviorDeclarerManager.RegisterDeclarer<CustomA>(nameof(CustomA).ToLower());
        cssBuilder.BehaviorDeclarerManager.RegisterDeclarer<CustomA>(nameof(CustomA));

        return builder;
    }
  1. Create behavior class derived from AcssBehavior<> and add BehaviorAttribute to it. The name 'window.esc.close' will be the name that used in acss file.
[Behavior("window.esc.close", typeof(CustomA))]
public class WindowEscCloseBehavior: AcssBehavior<WindowEscCloseBehavior>
{
    protected override void OnAttached(AvaloniaObject target)
    {
        if (target is not Window window)
        {
            return;
        }

        window.KeyDown -= WindowOnKeyDown;
        window.KeyDown += WindowOnKeyDown;
    }
    
    protected override void OnDetached(AvaloniaObject target)
    {
        if (target is not Window window)
        {
            return;
        }

        window.KeyDown -= WindowOnKeyDown;
    }
    
    private static void WindowOnKeyDown(object? sender, KeyEventArgs e)
    {
        if (sender is not Window window)
        {
            return;
        }
        if (e.Key == Key.Escape)
        {
            window.Close();
        }
    }
}

Usage

  • Use the acss behaviors for default builder in BuildAvaloniaApp(). Also you can use the behaviors in your own AcssBuilder with method AcssBehaviorsExtensions.UseAcssBehavior().
private static AppBuilder BuildAvaloniaApp()
{
    return AppBuilder.Configure<App>()
        .UsePlatformDetect()
        .LogToTrace()
        .UseReactiveUI()
        .LogToLocalFile()

        // Use avalonia css stuff.
        .UseAcssDefaultBuilder()

        // [Optional] Use acss behavior.
        .UseAcssBehaviorForDefaultBuilder();
    	
    	// [Optional] Use customA behavior.
    	.UseCustomABehaviorForDefaultBulder();
}
  • Use the behaviors in acss.
ComboBoxPage ComboBox#C1{
    .acss:combobox.popup.align;
}

NtWindow#RootWindow{
    .acss:window.esc.close;
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Nlnet.Avalonia.Css.Behaviors:

Repository Stars
liwuqingxin/Avalonia.Css
Enable css for avalonia.
Version Downloads Last updated
1.0.2 147 3/16/2024
1.0.0 380 11/6/2023
1.0.0-beta.3 90 8/27/2023
1.0.0-beta.2 78 8/23/2023
1.0.0-beta.1 129 8/16/2023