Запустить анимацию в Silverlight - PullRequest
1 голос
/ 02 апреля 2011

Я создал страницу Silverlight, которая имеет две анимации: одну, которая отображается при открытии страницы, и одну, которая отображается перед закрытием страницы. У меня также есть другая главная страница с рамкой навигации, которая содержит эти страницы.

При нажатии на кнопки меню переключение работает отлично. У меня есть обработчик событий page_loaded, который отображает первую анимацию, но у меня проблема с вызовом «исчезнуть анимация».

На главной странице я сделал следующее:

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

но я получаю ошибку, что targetProperty не может быть решен.

Вот как определяются анимации

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

Насколько я вижу, я установил targetProperty в анимации ... Я также пытался сделать это как

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

но это также не работает ... кто-нибудь знает, как я могу вызвать запуск анимации, которая определена на другой странице

EDIT: вот что я сделал, чтобы решить эту проблему, чтобы прояснить ситуацию. Home, About, Comet - это страницы xaml, которые я показываю пользователю, а ContentFrame - это навигационная рамка, в которой размещаются страницы.

Флипанимация просто переворачивает навигационную рамку на 360 градусов, чтобы она выглядела немного лучше:)

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

Теперь мне просто нужно выяснить, почему исчезающая анимация заканчивается менее чем за полсекунды ... но порядок, в котором запускаются анимации (текущий сайт исчезает, а при следующем запуске появляется анимация), в порядке

надеюсь, это поможет кому-то еще

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...