更多的内置行为正在计划当中。如果对此你有任何需求,欢迎在这里告诉我们。
dotnet add package Nlnet.Avalonia.Css.Behaviors --version 1.0.0-beta.4
dotnet add package Nlnet.Avalonia.Css.CompileGenerator --version 1.0.0-beta.4
public partial class CustomA : AvaloniaObject, IBehaviorDeclarer
{
}
/// <summary>
/// Use customA behavior feature for default css context.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static AppBuilder UseCustomABehaviorForDefaultContext(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;
}
[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();
}
}
}
private static AppBuilder BuildAvaloniaApp()
{
return AppBuilder.Configure<App>()
.UsePlatformDetect()
// Use avalonia css stuff.
.UseAcssDefaultBuilder()
// [Optional] Use acss behavior.
.UseAcssBehaviorForDefaultBuilder();
// [Optional] Use customA behavior.
.UseCustomABehaviorForDefaultBulder();
}
ComboBoxPage ComboBox#C1{
.acss:combobox.popup.align;
}
NtWindow#RootWindow{
.acss:window.esc.close;
}