Nlnet.Avalonia.Css.Behaviors
1.0.0-beta.2
See the version list below for details.
dotnet add package Nlnet.Avalonia.Css.Behaviors --version 1.0.0-beta.2
NuGet\Install-Package Nlnet.Avalonia.Css.Behaviors -Version 1.0.0-beta.2
<PackageReference Include="Nlnet.Avalonia.Css.Behaviors" Version="1.0.0-beta.2" />
paket add Nlnet.Avalonia.Css.Behaviors --version 1.0.0-beta.2
#r "nuget: Nlnet.Avalonia.Css.Behaviors, 1.0.0-beta.2"
// Install Nlnet.Avalonia.Css.Behaviors as a Cake Addin #addin nuget:?package=Nlnet.Avalonia.Css.Behaviors&version=1.0.0-beta.2&prerelease // Install Nlnet.Avalonia.Css.Behaviors as a Cake Tool #tool nuget:?package=Nlnet.Avalonia.Css.Behaviors&version=1.0.0-beta.2&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.
Add package Nlnet.Avalonia.Css.CompileGenerator.
Create behavior declarer class that derived from
AvaloniaObject
and implement theIBehaviorDeclarer
interface.
public partial class CustomA : AvaloniaObject, IBehaviorDeclarer
{
}
- 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;
}
- 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 methodAcssBehaviorsExtensions.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 | Versions 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. |
-
net6.0
- Avalonia (>= 11.0.0)
- Avalonia.ReactiveUI (>= 11.0.0)
- Nlnet.Avalonia.Css (>= 1.0.0-beta.2)
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 |