简体   繁体   English

WPF框架通过情节提要触发器

[英]WPF Frame triggers via storyboard

I am quite new to C#/WPF in general, so it is quite possible I am missing something. 一般来说,我对C#/ WPF还是很陌生,所以很可能会丢失一些东西。

Is there a way to animate a Frame via a storyboard that will trigger on the Frame.Navigating event ? 有没有一种方法可以通过会在Frame.Navigating事件上触发的情节Frame.Navigating来对Frame进行动画Frame.Navigating Most of the common ones I can choose from such as Frame.Loaded not the event I am after however. 我可以从中选择大多数常见的东西,例如Frame.Loaded而不是我Frame.Loaded的事件。

edit: I wonder is a Custom RoutedEvent the best way to approach this? 编辑:我想知道自定义RoutedEvent是解决此问题的最佳方法吗?

When a user clicks a button the Frame.Navigate method is called and loads in a new Page. 当用户单击按钮时,将Frame.Navigate方法并加载到新的Page中。

What I would like to happen is... 我想发生的是...

  • Button is clicked 单击按钮
  • (key frame : 0.1s) - Frame fades from 100-0 opacity (关键帧:0.1s)-帧从100-0不透明度逐渐消失
  • (key frame : 0.15s) - Frame moves off the canvas using a transform (关键帧:0.15s)-使用变换将帧移出画布
  • (key frame : 0.2s) - Frame moves back onto screen, opacity back to 100% (关键帧:0.2s)-帧移回屏幕,不透明度恢复到100%
  • Page is loaded 页面已加载

This will do it for you. 这将为你做到。

<Frame x:Name="Frm" Width="499" Height="248" Content="Frame"  Navigating="Frame_Navigating_1" BorderThickness="2" BorderBrush="#FF21BD9A">
    <Frame.RenderTransform>
        <TranslateTransform x:Name="TransTrfm" />
    </Frame.RenderTransform>
    <Frame.Resources>
        <Storyboard x:Key="SbKey">
            <DoubleAnimationUsingKeyFrames  Storyboard.TargetProperty="Opacity">
                <DoubleAnimationUsingKeyFrames.KeyFrames>
                    <LinearDoubleKeyFrame KeyTime="0:0:0" Value="1.0"/>
                    <LinearDoubleKeyFrame KeyTime="0:0:0.1" Value="0"/>
                </DoubleAnimationUsingKeyFrames.KeyFrames>
            </DoubleAnimationUsingKeyFrames>

            <DoubleAnimationUsingKeyFrames BeginTime="0:0:0.15" Storyboard.TargetProperty="X" Storyboard.TargetName="TransTrfm">
                <DoubleAnimationUsingKeyFrames.KeyFrames>
                    <LinearDoubleKeyFrame  KeyTime="0:0:0" Value="0.0" />
                    <LinearDoubleKeyFrame  KeyTime="0:0:0.17" Value="-1000.0" />
                    <LinearDoubleKeyFrame  KeyTime="0:0:0.2" Value="0.0" />
                </DoubleAnimationUsingKeyFrames.KeyFrames>
            </DoubleAnimationUsingKeyFrames>

            <DoubleAnimationUsingKeyFrames BeginTime="0:0:0.35" Storyboard.TargetProperty="Opacity">
                <DoubleAnimationUsingKeyFrames.KeyFrames>
                    <LinearDoubleKeyFrame KeyTime="0:0:0" Value="1.0"/>
                </DoubleAnimationUsingKeyFrames.KeyFrames>
            </DoubleAnimationUsingKeyFrames>

        </Storyboard>
    </Frame.Resources>
</Frame>

Code : 代码:

    Storyboard sb;
    private void Frame_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        if (sb != null)
        {
            sb.Begin();
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        sb = (Storyboard)Frm.Resources["SbKey"];
        Storyboard.SetTargetName(sb, "Frm");

        Frm.Navigate("Page1.xaml");         
    }

You can also try using ControlStoryboardAction behavior, and ChangePropertyAction behavior to avoid code-behind. 您也可以尝试使用ControlStoryboardAction行为和ChangePropertyAction行为来避免代码隐藏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM