使用Avalonia桌面端开发-无边框窗口、窗口阴影

无边框窗口

方式1 样式配置

完全透明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Window.Styles>
<Style Selector="Window">
<Setter Property="SystemDecorations" Value="None">
</Setter>
<Setter Property="CanResize" Value="False">
</Setter>
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Opacity="0" Color="White">
</SolidColorBrush>
</Setter.Value>
</Setter>
</Style>
</Window.Styles>

带阴影

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<Window.Styles>
<Style Selector="Window">
<Setter Property="ExtendClientAreaToDecorationsHint" Value="True">
</Setter>
<Setter Property="ExtendClientAreaChromeHints" Value="NoChrome">
</Setter>
<Setter Property="ExtendClientAreaTitleBarHeightHint" Value="-1">
</Setter>
<Setter Property="CanResize" Value="False">
</Setter>
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Opacity="0" Color="White">
</SolidColorBrush>
</Setter.Value>
</Setter>
</Style>
</Window.Styles>

上面无边框窗口的样式中 设置背景透明色并不是直接使用Background=Transparent而是用了一个 SolidCorloBrush 设置的Opacity=0,这么做的原因是直接设置Background=Transparent 发布在Linux(银河麒麟v10) 上,无边框的样式会失效,使用SolidCorloBrush 才可以。

方式2 属性配置

窗口完全透明

1
2
SystemDecorations="None"
Background="Transparent"

窗口带边线和阴影

1
2
3
ExtendClientAreaToDecorationsHint="True"
ExtendClientAreaChromeHints="NoChrome"
ExtendClientAreaTitleBarHeightHint="-1"

这样设置后,窗口会有阴影和边框。

窗口拖动

需要拖动的地方添加PointerPressed事件

1
this.PointerPressed += MainWindow_PointerPressed;

事件

1
2
3
4
5
6
7
private void MainWindow_PointerPressed(object? sender, Avalonia.Input.PointerPressedEventArgs e)
{
if (e.Pointer.Type == PointerType.Mouse)
{
this.BeginMoveDrag(e);
}
}