HandyControl自定义窗口问题
前段时间写XX工具的时候,用到了HandyControl组件库,在开发的时候想着自定义一个窗口,这样看着更协调一些,但是按照官方的Example实现的窗口并不好,一个是窗口是重写的,导致只能通过点击窗口上的按钮去关闭,另外,这个窗口在最大化和最小化的时候有些不协调,还有周围还有一点线条,看着有点奇怪,那么该如何自定义窗口而不引入这些问题呢?
HandyControl的官方文档中压根没有这个解法,后来还是看到一个第三方文档,里面是通过继承HandyControl的Window,并且重写NonClientAreaContent去实现,以下是一个可用的自定义窗口
XML
当然,在使用之前需要安装HandyControl的nuget包,并且配置一下才能使用 <hc:Window
FocusManager.FocusedElement="{Binding RelativeSource={x:Static RelativeSource.Self}, Mode=OneTime}"
Height="450"
MinHeight="450"
MinWidth="800"
ResizeMode="CanResize"
Title="测试窗口"
Width="800"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<hc:Window.NonClientAreaContent>
<StackPanel
HorizontalAlignment="Right"
Margin="10,0"
Orientation="Horizontal"
VerticalAlignment="Stretch">
<TextBlock Text="深色模式" VerticalAlignment="Center" />
<ToggleButton
IsChecked="{Binding DarkTheme, Mode=TwoWay}"
Margin="10,0"
Style="{StaticResource ToggleButtonSwitch}"
hc:VisualElement.HighlightBrush="{DynamicResource InfoBrush}" />
<Button
Click="AboutButtonClick"
Content="关于"
hc:IconElement.Geometry="{StaticResource InfoGeometry}" />
</StackPanel>
</hc:Window.NonClientAreaContent>
</hc:Window>
另外,在使用期间,发现一个比较奇怪的问题,如果隐藏了最大化按钮,会导致程序最小化后无法显示界面,这个问题暂时弄不清楚具体原因,可能是这个窗口实现的时候有点问题吧,目前就去掉了这个限制
吐槽
如果你在使用HandyControl的时候,不仅要留意官方文档,还得多看看我提到的这个第三方文档。个人认为HandyControl文档写的有些乱,有些用法都没有写,比如TagContainer,官方文档还停在很久之前的版本,第三方文档给出的代码有问题,只能看源码去解决一些问题,比如通过MVVM添加、删除等。对于这些问题还没有给出Demo完整演示,用起来太麻烦。不过相比有些组件库还算好一点,能找到的资料稍微多一点