WPF框架通过故事板触发

时间:2016-02-27 20:36:07

标签: c# wpf events storyboard blend

我对C#/ WPF很新,所以很可能我错过了一些东西。

有没有办法通过会在Frame.Navigating事件上触发的故事板为Frame.Loaded制作动画?我可以选择的大多数常见的例如Frame.Navigate而不是我之后的事件。

编辑:我想知道Custom RoutedEvent是解决此问题的最佳方法吗?

当用户点击按钮时,会调用class1 { callingMethod() { Double[] entry = new Double[size]; while(condition) { for (i over entry) { entry[i] = Double.parseDouble(token.nextToken()); } DataSet.addInput(entry); } } } DataSet { private ArrayList<Double[]> inputsList; public DataSet (...) { list = new <Double[]>ArrayList(); } public void addInput(Double[] anInputVector) { inputsList.add(anInputVector); } } 方法并加载新的页面。

我想要发生的是......

  • 点击按钮
  • (关键帧:0.1s) - 帧从100-0不透明度渐变
  • (关键帧:0.15s) - 帧使用变换
  • 移出画布
  • (关键帧:0.2秒) - 帧移回屏幕,不透明度回到100%
  • 已加载页面

1 个答案:

答案 0 :(得分:0)

这将为你做到。

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

代码:

    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");         
    }

您还可以尝试使用ControlStoryboardAction行为和ChangePropertyAction行为来避免代码隐藏。

相关问题