How to animate a Popup when it hides?

三世轮回 提交于 2019-12-30 04:59:05

问题


i created a Popup Style for using in my windows 8.1 application. And i applied PopupThemeTransition to it's ChildTransitions Property.

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

My problem is that it animates when it Opens & not animating when closing. What to do with that for animating the content on hiding ? Do i want to create a Custom Popup control?

NB: I know that PopInThemeAnimation & PopOutThemeAnimation is there . But don't know how to use it on this condition ?


回答1:


This is not native to the Popup because they typically do not have an exit animation. Having said that, there's no reason you can't add an exit animation to a Popup control.

Try this:

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

Using these:

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

Best of luck!



来源:https://stackoverflow.com/questions/21111162/how-to-animate-a-popup-when-it-hides

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!