WPF和Blend:调整窗口大小不需要';行不通

WPF和Blend:调整窗口大小不需要';行不通,wpf,blend,Wpf,Blend,我已经设置了一个故事板来放大和缩小窗口。这些在混合编辑器中可以很好地工作,但在实际运行时,它们根本不工作 窗口应以与垂直方向相同的速率水平扩展/收缩 这有什么办法 这是我的XAML <Window x:Name="window" x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.co

我已经设置了一个故事板来放大和缩小窗口。这些在混合编辑器中可以很好地工作,但在实际运行时,它们根本不工作

窗口应以与垂直方向相同的速率水平扩展/收缩

这有什么办法

这是我的XAML

<Window x:Name="window" x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="32" Width="32" ResizeMode="NoResize" WindowStyle="None">
    <Window.Resources>
        <Storyboard x:Key="GrowFrame">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="window">
                <EasingDoubleKeyFrame KeyTime="0" Value="32"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="256">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <BackEase EasingMode="EaseOut" Amplitude="0.25"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="window">
                <EasingDoubleKeyFrame KeyTime="0" Value="32"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="256">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <BackEase EasingMode="EaseOut" Amplitude="0.25"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="window">
                <EasingDoubleKeyFrame KeyTime="0" Value="0.5"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <BackEase EasingMode="EaseOut" Amplitude="0.25"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="window">
                <EasingColorKeyFrame KeyTime="0" Value="Transparent"/>
                <EasingColorKeyFrame KeyTime="0:0:0.5" Value="White">
                    <EasingColorKeyFrame.EasingFunction>
                        <BackEase EasingMode="EaseOut"/>
                    </EasingColorKeyFrame.EasingFunction>
                </EasingColorKeyFrame>
            </ColorAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="ShrinkFrame">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="window">
                <EasingDoubleKeyFrame KeyTime="0" Value="256"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="32">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <CubicEase EasingMode="EaseInOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="window">
                <EasingDoubleKeyFrame KeyTime="0" Value="256"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="32">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <CubicEase EasingMode="EaseInOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="window">
                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0.5">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <CubicEase EasingMode="EaseInOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="window">
                <EasingColorKeyFrame KeyTime="0" Value="White"/>
                <EasingColorKeyFrame KeyTime="0:0:0.5" Value="Transparent">
                    <EasingColorKeyFrame.EasingFunction>
                        <CubicEase EasingMode="EaseInOut"/>
                    </EasingColorKeyFrame.EasingFunction>
                </EasingColorKeyFrame>
            </ColorAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="Mouse.MouseEnter">
            <BeginStoryboard Storyboard="{StaticResource GrowFrame}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="Mouse.MouseLeave">
            <BeginStoryboard x:Name="ShrinkFrame_BeginStoryboard" Storyboard="{StaticResource ShrinkFrame}"/>
        </EventTrigger>
    </Window.Triggers>
</Window>

正如其他人指出的,新框架版本中存在一个阻止同步动画的缺陷。然而,这里有一个对我来说非常有效的快速小解决方案。这不简单,所以我认为任何解释都不合适。它起作用了,这才是最重要的:

XAML:

<Window x:Class="App.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="32" Width="32" ResizeMode="NoResize" WindowStyle="None"
    MouseEnter="MouseEntered" MouseLeave="MouseLeft">
</Window>
using System.Windows;
using System.Windows.Input;

namespace App
{
    public partial class MainWindow : Window
    {
        private const int MAX_WINDOW_SIZE = 256;
        private const int MIN_WINDOW_SIZE = 32;
        private const int ANIMATION_SPEED = 10;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void MouseEntered(object sender, MouseEventArgs e)
        {
            while (this.Width <= MAX_WINDOW_SIZE)
            {
                this.Width += ANIMATION_SPEED;
                this.Height += ANIMATION_SPEED;
            }
        }

        private void MouseLeft(object sender, MouseEventArgs e)
        {
            while (this.Width >= MIN_WINDOW_SIZE)
            {
                this.Width -= ANIMATION_SPEED;
                this.Height -= ANIMATION_SPEED;
            }
        }
    }
}

代码隐藏:

<Window x:Class="App.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="32" Width="32" ResizeMode="NoResize" WindowStyle="None"
    MouseEnter="MouseEntered" MouseLeave="MouseLeft">
</Window>
using System.Windows;
using System.Windows.Input;

namespace App
{
    public partial class MainWindow : Window
    {
        private const int MAX_WINDOW_SIZE = 256;
        private const int MIN_WINDOW_SIZE = 32;
        private const int ANIMATION_SPEED = 10;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void MouseEntered(object sender, MouseEventArgs e)
        {
            while (this.Width <= MAX_WINDOW_SIZE)
            {
                this.Width += ANIMATION_SPEED;
                this.Height += ANIMATION_SPEED;
            }
        }

        private void MouseLeft(object sender, MouseEventArgs e)
        {
            while (this.Width >= MIN_WINDOW_SIZE)
            {
                this.Width -= ANIMATION_SPEED;
                this.Height -= ANIMATION_SPEED;
            }
        }
    }
}
使用System.Windows;
使用System.Windows.Input;
命名空间应用程序
{
公共部分类主窗口:窗口
{
私有常量int MAX_WINDOW_SIZE=256;
私有const int MIN_WINDOW_SIZE=32;
私有常量int动画速度=10;
公共主窗口()
{
初始化组件();
}
私有void mouseenterneted(对象发送方,MouseEventArgs e)
{
while(this.Width=最小窗口大小)
{
此.Width-=动画速度;
此.Height-=动画速度;
}
}
}
}

WPF支持同步动画(如您所尝试的)和重叠动画,其中您提供了切换行为。您可以尝试一下,看看它是否可以作为一种解决方法。然而,我认为这是一个已知的bug()。我很震惊它现在还没有被修复。

或者,使用一个固定大小的透明窗口作为解决方案:

   <Window x:Name="window" x:Class="RectangleWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="RectangleWindow" Height="300" Width="300"  ResizeMode="NoResize" WindowStyle="None" 
            Background="Transparent" AllowsTransparency="True">
        <Window.Resources>
            <Storyboard x:Key="GrowFrame">
                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="insideRectangle">
                    <EasingDoubleKeyFrame KeyTime="0" Value="32"/>
                    <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="256">
                        <EasingDoubleKeyFrame.EasingFunction>
                            <BackEase EasingMode="EaseOut" Amplitude="0.25"/>
                        </EasingDoubleKeyFrame.EasingFunction>
                    </EasingDoubleKeyFrame>
                </DoubleAnimationUsingKeyFrames>
                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="insideRectangle">
                    <EasingDoubleKeyFrame KeyTime="0" Value="32"/>
                    <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="256">
                        <EasingDoubleKeyFrame.EasingFunction>
                            <BackEase EasingMode="EaseOut" Amplitude="0.25"/>
                        </EasingDoubleKeyFrame.EasingFunction>
                    </EasingDoubleKeyFrame>
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>
            <Storyboard x:Key="ShrinkFrame">
                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="insideRectangle">
                    <EasingDoubleKeyFrame KeyTime="0" Value="256"/>
                    <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="32">
                        <EasingDoubleKeyFrame.EasingFunction>
                            <CubicEase EasingMode="EaseInOut"/>
                        </EasingDoubleKeyFrame.EasingFunction>
                    </EasingDoubleKeyFrame>
                </DoubleAnimationUsingKeyFrames>
                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="insideRectangle">
                    <EasingDoubleKeyFrame KeyTime="0" Value="256"/>
                    <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="32">
                        <EasingDoubleKeyFrame.EasingFunction>
                            <CubicEase EasingMode="EaseInOut"/>
                        </EasingDoubleKeyFrame.EasingFunction>
                    </EasingDoubleKeyFrame>
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>
        </Window.Resources>
        <Window.Triggers>
            <EventTrigger RoutedEvent="Mouse.MouseEnter">
                <BeginStoryboard Storyboard="{StaticResource GrowFrame}"/>
            </EventTrigger>
            <EventTrigger RoutedEvent="Mouse.MouseLeave">
                <BeginStoryboard x:Name="ShrinkFrame_BeginStoryboard" Storyboard="{StaticResource ShrinkFrame}"/>
            </EventTrigger>
        </Window.Triggers>

        <Rectangle x:Name="insideRectangle" Fill="White" 
          Width="50" Height="50"
          HorizontalAlignment="Left"
          VerticalAlignment="Top">
            <Rectangle.BitmapEffect>
                <DropShadowBitmapEffect/>
            </Rectangle.BitmapEffect>      
        </Rectangle>
    </Window>


发布您迄今为止尝试过的代码……当您设置属性时,Blender会生成一些称为xaml的标记。如果您能识别它并更新问题,我们可以尝试找出您设置的任何属性是否错误。@sankarann好了。当您直接从Blend运行时,故事板是否工作?由于您已将窗口大小调整设置为无大小调整…@Sankarann“无大小调整”意味着用户无法通过拖动边框来调整窗体大小。谢谢您的回答,它确实有效。不过,在进行了一点搜索之后,我发现在.NET3.5中对窗口的宽度和高度设置动画是可行的。。。为什么不在4.5.1中?好像是个错误。@SuperDisk嗯,不确定。我做了大量的研究,找不到解决这个问题的方法。我提供的解决方案是我自己一直在使用的——有时简单的方法是最好的方法。这让人放心:“非常感谢您的反馈。现在我们不会在产品的这一领域进行任何更改。谢谢!Wpf团队”是的,考虑到它已经存在了多久,他们信守诺言。我认为他们不理解其中的含义。