Mahapps 中的 StickWindow

StickWindow in Mahapps

我正在尝试实现吸附到边缘。我找到了一个答案 here 并实现了,但是它破坏了 Mahapps MetroWindow 的无边框 window。

我已经调整了解决方案以使用 Behaviors,所以我将只分享 Behavior snnipet(您可以找到 StickyWindow 代码 here):

用法:

<metro:MetroWindow
x:Class="Communicator.Main.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:metro="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:cal="http://www.caliburnproject.org"
xmlns:phoneView="clr-namespace:Communicator.Softphone.Views;assembly=Communicator.Softphone"
xmlns:converters="clr-namespace:Communicator.ControlLibrary.Converters;assembly=Communicator.ControlLibrary"
xmlns:local="clr-namespace:Communicator.Main"
Title="Comunicador" Height="600" Width="450"
WindowStartupLocation="CenterScreen" ResizeMode="NoResize" TitlebarHeight="40"
EnableDWMDropShadow="True" WindowTransitionsEnabled="False"
cal:Message.Attach="[Event KeyDown] = [Action KeyDown($executionContext)]; [Event Activated] = [Action ViewActivated]; [Event Deactivated] = [Action ViewDeactivated]">
<i:Interaction.Behaviors>
    <local:StickyWindowBehavior />
</i:Interaction.Behaviors>

StickWindowBehavior:

public class StickyWindowBehavior : Behavior<Window>
{
    private StickyWindow stickWindow = null;

    protected override void OnAttached()
    {
        base.OnAttached();
        stickWindow = new StickyWindow(AssociatedObject)
        {
            StickToScreen = true,
            StickOnResize = true,
            StickOnMove = true
        };
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        if (stickWindow != null)
            stickWindow.ReleaseHandle();
    }
}

我的 MetroWindow 发生了什么:

如何在不丢失 MetroWindow 无边框 window 的情况下添加摇杆行为?

您的问题与 的原因相同:Mahapps.Metro 将其行为设置为 window 的样式,所以实际上你正在覆盖 XAML.

中的那些声明

您必须设置标准行为以及您自己的行为:

<i:Interaction.Behaviors>
    <local:StickyWindowBehavior />
    <Behaviours:BorderlessWindowBehavior />
    <Behaviours:WindowsSettingBehaviour />
    <Behaviours:GlowWindowBehavior />
</i:Interaction.Behaviors>