0

我正在尝试创建 Sticky WPF 窗口。

如果窗口更靠近左侧,则贴在左侧或右侧,如果靠近右侧,则在顶部

在我正在使用的代码下方,

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                this.DragMove();
            }

    private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {                
                Point currentPoints = PointToScreen(Mouse.GetPosition(this));

//Place left
                if (currentPoints.X < (300))
                {                 
                    DoubleAnimation moveAnimation = new DoubleAnimation(-190, TimeSpan.FromSeconds(1));
                    _MyWindow.BeginAnimation(Window.LeftProperty, moveAnimation);
                }
//Place Right
                else if (currentPoints.X > (Screen.PrimaryScreen.WorkingArea.Width - 300))
                {
                    DoubleAnimation moveAnimation = new DoubleAnimation(Screen.PrimaryScreen.WorkingArea.Width + 190, TimeSpan.FromSeconds(1));
                    _MyWindow.BeginAnimation(Window.LeftProperty, moveAnimation);              
                }
//Place top
                else
                {
                    DoubleAnimation TopAnimation = new DoubleAnimation(-190, TimeSpan.FromSeconds(1));
                    _MyWindow.BeginAnimation(Window.TopProperty, TopAnimation, HandoffBehavior.Compose);
                }
            }

上面的代码只移动一次窗口。

在 MSDN 上,如何:使用情节提要对其进行动画处理后设置属性

要再次运行动画,请将 BeginAnimation 属性设置为 NULL,

我尝试在动画之前将属性设置为 NULL。现在代码可以正常工作了。

现在代码看起来像

private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Point currentPoints = PointToScreen(Mouse.GetPosition(this));

            if (currentPoints.X < (300))
            {
                if (_MyWindow.HasAnimatedProperties)
                    _MyWindow.BeginAnimation(Window.LeftProperty, null);
                DoubleAnimation moveAnimation = new DoubleAnimation(-190, TimeSpan.FromSeconds(1));
                _MyWindow.BeginAnimation(Window.LeftProperty, moveAnimation);
            }
            else if (currentPoints.X > (Screen.PrimaryScreen.WorkingArea.Width - 300))
            {
                if (_MyWindow.HasAnimatedProperties)
                    _MyWindow.BeginAnimation(Window.LeftProperty, null);
                DoubleAnimation moveAnimation = new DoubleAnimation(Screen.PrimaryScreen.WorkingArea.Width + 190, TimeSpan.FromSeconds(1));
                _MyWindow.BeginAnimation(Window.LeftProperty, moveAnimation);
            }
            else
            {
                if (_MyWindow.HasAnimatedProperties)
                    _MyWindow.BeginAnimation(Window.TopProperty, null);
                DoubleAnimation TopAnimation = new DoubleAnimation(-190, TimeSpan.FromSeconds(1));
                _MyWindow.BeginAnimation(Window.TopProperty, TopAnimation, HandoffBehavior.Compose);
            }
        }

如果注意到,我将窗口放置在左侧和顶部的 -190 位置以隐藏它的某些部分。

但是使用下面的属性,它将窗口位置重置为0。我不希望它重置位置

_MyWindow.BeginAnimation(Window.LeftProperty, null);

有人可以建议如何在不重置现有位置的情况下制作多个动画吗?

  • Ashish Sapkale
4

1 回答 1

0

我认为您将 LeftProperty 设置为 null 的想法是正确的,但您应该在动画完成后这样做。

因此,为您的动画添加一个委托并将该属性设置为 null 并查看它是否有效。

例子

TopAnimation.Completed += (s,e) =>
{
   _MyWindow.BeginAnimation(Window.TopProperty, null);
};

您的其他选择是使用 ThicknessAnimation 或 TranslateTransform 来移动您的窗口。让我知道您是否仍有问题

于 2015-05-21T09:31:22.777 回答