开发者

Start animation in silverlight

I have created a Silverlight Page which has two animations: one which is displayed when the page is opened and one which is displayed before closing the page. I also have another mainpage with a navigation frame that holds this pages.

While clicking on the menu buttons the switching works fine. I have a page_loaded eventhandler which displays the first animation, but I have problem with the call to the "disappear animation".

I did the following thing from the main page:

menuButtonClicked = (HyperlinkButton)sender;
            switch (menuButtonClicked.Name)
            {
                case "homeButton":
                    {

                        About aboutPage = (About)ContentFrame.Content;
                        aboutPage.DisappearAnimation.Begin();
                    }
                    break;
                case "aboutButton":
                    {
                        Home homePage = (Home)ContentFrame.Content;
                        homePage.DisappearAnimation.Begin();
                    }
                    break;
            }

but I get an error that the targetProperty can not be resolved.

Here is how the animations are defined

<Storyboard x:Name="DisappearAnimation">
    <ObjectAnimationUsingKeyFrames
        Storyboard.TargetProperty="(UIElement.Visibility)"
        Storyboard.TargetName="PageScrollViewer">
        <DiscreteObjectKeyFrame KeyTime="0">
            <DiscreteObjectKeyFrame.Value>
                <Visibility>Visible</Visibility>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
    </ObjectAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames
        Storyboard.TargetProperty="(UIElement.RenderTransform)
              .(CompositeTransform.TranslateY)"
        Storyboard.TargetName="PageScrollViewer">
        <EasingDoubleKeyFrame KeyTime="0"
                              Value="0"/>
        <EasingDoubleKeyFrame KeyTime="0:0:0.5"
                              Value="-14"/>
        <EasingDoubleKeyFrame KeyTime="0:0:1"
                              Value="442"/>
    </DoubleAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames
        Storyboard.TargetProperty="(UIElement.Opacity)"
        Storyboard.TargetName="PageScrollViewer">
        <EasingDoubleKeyFrame KeyTime="0:0:0.5"
                              Value="1"/>
        <EasingDoubleKeyFrame KeyTime="0:0:1"
                              Value="0"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

As far as I can see, I have set the targetProperty in the animation ... I have also tried to do it like

aboutPage.SetValue(Storyboard.TargetNameProperty, "PageScrollViewer");

but it also doesn't work ... a开发者_Python百科nyone know how I can call start an animation which is defined on another page

EDIT: here is what I have done to solve the problem to make things a bit more clear. Home, About, Comet are the xaml pages which I display to the user and the ContentFrame is the navigationFrame which hosts the pages.

The flipanimation just flips the navigation frame 360 degrees just to make it look a bit nicer:)

private void HomeButton_MouseLeftButtonDown(object sender, RoutedEventArgs e)
    {
        if (sender != null && menuButtonClicked != (HyperlinkButton)sender)
        {
            ChangeMenuState(sender);
            ChangePage(currentPage);
            currentPage = CurrentPage.Home;
        }
    }

...

private void AboutButton_MouseLeftButtonDown
                        (object sender, RoutedEventArgs e)
{
    if (sender != null && menuButtonClicked != (HyperlinkButton)sender)
    {
        ChangeMenuState(sender);
        ChangePage(currentPage);
        currentPage = CurrentPage.About;
    }
}

...

private void ContactButton_MouseLeftButtonDown
                 (object sender, RoutedEventArgs e)
{
    if (sender != null && menuButtonClicked != (HyperlinkButton)sender)
    {
        ChangeMenuState(sender);
        ChangePage(currentPage);
        currentPage = CurrentPage.Contact;
    }
}

...

private void ChangePage(CurrentPage currentPage)
{
        switch (currentPage)
        {
            case CurrentPage.Welcome:
                {
                    ContentHolder.Visibility = Visibility.Visible;
                    FlipAnimation.Stop();
                    FlipAnimation.SetValue(Storyboard.TargetNameProperty, 
                                              "ContentHolder");
                    FlipAnimation.Begin();
                }
                break;
            case CurrentPage.Home:
                {
                    ContentHolder.Visibility = Visibility.Visible;
                    Home homePage = (Home)ContentFrame.Content;
                    homePage.DisappearAnimation.Begin();
                    homePage.DisappearAnimation.Completed += StopStoryboard;
                }
                break;
            case CurrentPage.About:
                {
                    ContentHolder.Visibility = Visibility.Visible;
                    About aboutPage = (About)ContentFrame.Content;
                    aboutPage.DisappearAnimation.Begin();
                    aboutPage.DisappearAnimation.Completed += StopStoryboard;
                }
                break;
            case CurrentPage.Contact:
                {
                    ContentHolder.Visibility = Visibility.Visible;
                    Contact contactPage = (Contact)ContentFrame.Content;
                    contactPage.DisappearAnimation.Begin();
                    contactPage.DisappearAnimation.Completed += StopStoryboard;
                }
                break;
    }
}

...

private void ChangeMenuState(object sender)
    {
        VisualStateManager.GoToState(sender as HyperlinkButton, 
                                       "ActiveLink", true);
        (sender as HyperlinkButton).Foreground = 
               new SolidColorBrush(Colors.White);
        MouseEnterAnimation.Stop();
        if (menuButtonClicked != null)
        {
            VisualStateManager.GoToState(menuButtonClicked, 
                                               "InactiveLink", true);
            MouseLeaveAnimation.Stop();
            MouseLeaveAnimation.SetValue(Storyboard.TargetNameProperty,
                menuButtonClicked.Name);
            MouseLeaveAnimation.Begin();
        }
        menuButtonClicked = (HyperlinkButton)sender;
    }

...

    private void StopStoryboard(object sender, EventArgs args)
    {
        Storyboard storyboard = (Storyboard)sender;
        storyboard.Stop();

        FlipAnimation.Stop();
        FlipAnimation.SetValue(Storyboard.TargetNameProperty,
                                           "ContentHolder");
        FlipAnimation.Begin();
    }

Now I just have to figure out why the disappear animation finishes in less the half a second... but the order in which the animatinos start (the present site disappears and the next starts it's appear animation) is ok

hope this will help someone else

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜