2014-02-15 46 views
2

我設置了一個放大和縮小窗口的故事板。這些在Blend編輯器中工作良好,但在實際運行時,它們根本不起作用。WPF和Blend:調整窗口大小不起作用

該窗口應該以與垂直方向水平相同的速率展開/收縮。

這是什麼修復?

enter image description here

這是我的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> 
+0

後你試過到目前爲止代碼... – Sankarann

+0

攪拌機產生一些標記稱爲XAML當您設置的屬性。如果您可以識別它並更新問題,我們可以嘗試確定您設置的任何屬性是否有誤。 –

+0

@sankarann我們走了。 –

回答

0

正如其他人指出的那樣,新框架版本中存在一個阻止同步動畫的錯誤。但是,這裏有一個快速的小解決方案,對我來說效果很好。它超越了簡單,所以我認爲沒有任何解釋是爲了順序。它的工作原理,這是最重要的:

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; 
      } 
     } 
    } 
} 
+0

感謝您的答案,它確實有效。雖然,經過多一點搜索,我發現動畫窗口的寬度和高度在.NET 3.5中工作...爲什麼不在4.5.1中?看起來像一個錯誤。 –

+0

@SuperDisk嗯,不知道。我做了很多研究,找不到能解決這個問題的解決方案。我提供的解決方案就是我自己一直在使用的解決方案 - 有時候簡單的方法是最好的方法。 –

2

WPF不支持同步動畫(如你未遂)和重疊您提供HandoffBehavior的動畫。您可以嘗試查看它是否可以解決問題。不過,我認爲這是一個已知的錯誤(https://connect.microsoft.com/VisualStudio/feedback/details/715415/window-width-height-animation-in-wpf-got-broken-on-net-framework-4-0)。我感到震驚,現在還沒有解決。

+1

這是令人放心的:「非常感謝您的反饋,在這一點上我們不會對產品的這個領域做任何改變,謝謝!Wpf團隊」 –

+1

好吧,考慮到這個時間已經過去了多長時間,他們堅持他們的話。我不認爲他們理解這些影響。 – user2023266

0

或者,只用一個固定大小的透明窗作爲一種解決方法:

<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>