【问题标题】:How to animate a Popup when it hides?隐藏时如何为弹出窗口设置动画?
【发布时间】:2014-02-02 08:55:34
【问题描述】:

我创建了一个Popup Style 用于在我的 Windows 8.1 应用程序中使用。我将PopupThemeTransition 应用于它的ChildTransitions 属性。

<Style x:Key="AnimatedPopupStyle" TargetType="Popup">
    <Setter Property="IsLightDismissEnabled" Value="False"/>
    <Setter Property="ChildTransitions">
        <Setter.Value>
            <TransitionCollection>
                <PopupThemeTransition/>
            </TransitionCollection>
        </Setter.Value>
    </Setter>
</Style>

我的问题是它在打开时动画而在关闭时不动画。如何处理隐藏内容的动画?我想创建一个自定义弹出控件吗?

注意:我知道PopInThemeAnimation 和PopOutThemeAnimation 在那里。但不知道在这种情况下如何使用它?

【问题讨论】:

  • 不想使用主题的原因是什么?
  • @JerryNixon-MSFT 对不起,你的意思是什么?

标签: xaml windows-8 popup winrt-xaml windows-8.1


【解决方案1】:

这不是 Popup 原生的,因为它们通常没有退出动画。话虽如此,没有理由不能将退出动画添加到 Popup 控件。

试试这个:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.Resources>
        <Storyboard x:Name="ShowPopup">
            <PopInThemeAnimation Storyboard.TargetName="MyPopup" />
        </Storyboard>
        <Storyboard x:Name="HidePopup">
            <PopOutThemeAnimation Storyboard.TargetName="MyPopup" />
        </Storyboard>
    </Grid.Resources>
    <Popup x:Name="MyPopup" IsOpen="True"
           HorizontalAlignment="Center" VerticalAlignment="Center">
        <Popup.Transitions>
            <TransitionCollection>
                <PopupThemeTransition />
            </TransitionCollection>
        </Popup.Transitions>
        <Grid Height="200" Width="200" Background="Red">
            <StackPanel>
                <Button Content="Hide (Native)" HorizontalAlignment="Center">
                    <Interactivity:Interaction.Behaviors>
                        <Core:EventTriggerBehavior EventName="Click">
                            <Core:ChangePropertyAction 
                                PropertyName="IsOpen" 
                                TargetObject="{Binding ElementName=MyPopup}" />
                        </Core:EventTriggerBehavior>
                    </Interactivity:Interaction.Behaviors>
                </Button>
                <Button Content="Hide (Storyboard)" HorizontalAlignment="Center">
                    <Interactivity:Interaction.Behaviors>
                        <Core:EventTriggerBehavior EventName="Click">
                            <Media:ControlStoryboardAction 
                                Storyboard="{StaticResource HidePopup}"/>
                        </Core:EventTriggerBehavior>
                    </Interactivity:Interaction.Behaviors>
                </Button>
            </StackPanel>
        </Grid>
    </Popup>
    <StackPanel>
        <Button Content="Show Popup (Native)" HorizontalAlignment="Left" VerticalAlignment="Top">
            <Interactivity:Interaction.Behaviors>
                <Core:EventTriggerBehavior EventName="Click">
                    <Core:ChangePropertyAction 
                        TargetObject="{Binding ElementName=MyPopup}" 
                        PropertyName="IsOpen" Value="True"/>
                </Core:EventTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
        </Button>
        <Button Content="Show Popup (Storyboard)" HorizontalAlignment="Left" VerticalAlignment="Top">
            <Interactivity:Interaction.Behaviors>
                <Core:EventTriggerBehavior EventName="Click">
                    <Core:ChangePropertyAction 
                        TargetObject="{Binding ElementName=MyPopup}" 
                        PropertyName="IsOpen" Value="True"/>
                    <Media:ControlStoryboardAction 
                        Storyboard="{StaticResource ShowPopup}"/>
                </Core:EventTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
        </Button>
    </StackPanel>
</Grid>

使用这些:

xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" 
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
xmlns:Media="using:Microsoft.Xaml.Interactions.Media"

祝你好运!

【讨论】:

  • 如果您使用存储板来显示/隐藏某些内容,我还建议您不要使用弹出窗口。只需使用网格,您就无需担心 IsOpen 属性
  • 让我考虑一下,肖恩。 Popup 的两个核心优势是它能够将自己注入到可视化树中,以及能够处理灯光关闭。既然他已经禁用了光解散,唯一的另一个就是树的东西了。那有多难?实际上,这并不容易,但也没有那么难。您的建议很好 - 但是,话虽如此,有什么害处?它的重量很轻。
  • 完全得到我要找的东西。感谢和 +1 的帮助。
猜你喜欢
  • 2014-11-20
  • 2013-04-13
  • 2014-09-20
  • 2017-10-28
  • 2022-12-31
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 2015-12-16
相关资源
最近更新 更多