文章

Avalonia UI常用C#实现

使用C#控制元素

Avalonia UI常用C#实现

Avalonia UI常用C#实现

寻找控件

需在xaml中为相应控件设置x:Name

1
_myButton = this.FindControl<Button>("MyButton");

Margin

1
_myButton.Margin = new Thickness(0, 10, 0, 0);

应用变形(Transfrom)

制作动画

1
2
var scaleTransform = new ScaleTransform { ScaleX = 0.9, ScaleY = 0.9 };
_myButton.RenderTransform = scaleTransform;

应用动画 (Animation)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var DropAnimation = new Animation
{
  Duration = TimeSpan.FromSeconds(0.25),
  Easing = new ExponentialEaseOut(),
  Children =
  {
      new KeyFrame
      {
          Cue = new Cue(0),
          Setters =
          {
              new Setter(UserControl.MarginProperty, new Thickness(0,100,0,0)),
              new Setter(UserControl.OpacityProperty, 0.0)
          }
      },
      new KeyFrame
      {
          Cue = new Cue(1),
          Setters =
          {
              new Setter(UserControl.MarginProperty, new Thickness(0)),
              new Setter(UserControl.OpacityProperty, 1.0)
          }
      }
  }
};
await DropAnimation.RunAsync(ContentPanel1);

在ResourceDict中寻找Geometry

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/// <summary>
/// Get Geometry svg from resource xaml
/// </summary>
/// <param name="resource_name">key for StreamGeometry you want</param>
/// <returns></returns>
private Geometry? TryGetGeometry(string resource_name)
{
    var is_res_exist = Application.Current.Resources.TryGetResource(resource_name, null, out var res);
    if (is_res_exist && res is Geometry geom)
    {
        return geom;
    }
    else
    {
        return null;
    }
}

在ResourceDict中寻找Color

1
2
3
4
5
var CBH_backgound = Application.Current.Resources.TryGetResource("CatalogBaseHighColor", null, out var Hresource);
if (CBH_backgound && Hresource is Color Backgound)
{
    SomeControl.Background = Backgound;
}

设置PathIcon内容为Geometry

1
2
3
Geometry? svg = TryGetGeometry("resource_name");
PathIcon icon = new PathIcon();
icon.Data = svg;

在Resource Dict中查找任意内容并强制转换

1
2
// to StreamGrometry
PathIcon.Data = (StreamGeometry)this.FindResource("res_name");

设置子控件的Dockpanel.Dock

1
2
3
DockPanel dockPanel = new DockPanel();
Label label = new Label();
DockPanel.SetDock(label, Dock.Left);
本文由作者按照 CC BY 4.0 进行授权