locked
Navigate to a new page after an animation completes. RRS feed

  • Question

  • Hey, I am using Application.xaml to set up the style of a button that's going to be on a lot of the pages in my program. I've got it so that the button will animate an arrow inside the button when it's pressed AND the mouse is over the button.

    What I want is for the button to navigate back a page if it is simply clicked, but go to a specific HOME page if it is pressed and held for 1.5 seconds.

    This is my code (It's probably terrible D:) (removed a bunch of stuff so that it pertains to what I'm talking about) 

    <Style x:Key="HomeButton" TargetType="{x:Type Button}">
    
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
    
                    <Grid>
    
                        <!--Contains a Path with line segments and such-->
    
                    </Grid>
    
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
    
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True"/>
                                <Condition Property="IsPressed" Value="True"/>
                            </MultiTrigger.Conditions>
    
                            <MultiTrigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
    
                                        <!--Contains animations of the Path and color-->
    
                                    </Storyboard>
                                </BeginStoryboard>
                            </MultiTrigger.EnterActions>
    
                            <MultiTrigger.ExitActions>
    
                                <BeginStoryboard>
                                    <Storyboard>
    
                                        <!--Reverses Path animation-->
    
                                    </Storyboard>
                                </BeginStoryboard>
    
                            </MultiTrigger.ExitActions>
    
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
    
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    
    </Style>
    

    What do I do? D:



    • Edited by DarkBox Monday, October 24, 2011 7:20 AM
    Monday, October 24, 2011 1:56 AM

Answers

  • use below code instead:

    (App.Current.MainWindow as NavigationWindow).NavigationService.Navigate(new Page1());

    it works on my side.


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Sheldon _Xiao Thursday, October 27, 2011 2:54 AM
    • Marked as answer by DarkBox Thursday, October 27, 2011 2:55 AM
    Thursday, October 27, 2011 2:26 AM

All replies

  • What I have tried doing is the following

                                    <MultiTrigger.EnterActions>
                                        <BeginStoryboard>
                                            <Storyboard Completed="ArrowTransformComplete">
                                                
    ...
    
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </MultiTrigger.EnterActions>
    

     

    and in the code behind (Application.xaml.vb)

    Private Sub ArrowTransformComplete(ByVal sender As Object, ByVal e As EventArgs)
    Dim HomePanda As New HomePanda()
    Me.NavigationService.Navigate(HomePanda)
    End Sub


    However, it tells me that

    'NavigationService' is not a member of 'WpfApplication1.Application'.

    I'm assuming this is because Application.xaml isn't in a navigation window like the rest of my program. Putting this code into a page with the button causes the error

    'ArrowTransformComplete' is not a member of 'WpfApplication1.Application'. in file Application.g.vb

    When you attempt to debug the program.

    Will I have to add code to every single page of my application or is there some way to keep it in the style/control template? :s



    • Edited by DarkBox Monday, October 24, 2011 5:56 AM
    Monday, October 24, 2011 5:49 AM
  • No ideas? :( This has to be really really simple...
    • Edited by DarkBox Tuesday, October 25, 2011 2:56 AM
    Tuesday, October 25, 2011 2:49 AM
  • Hi DarkBox,

    If you are developing with WPF, it it is, NavigationService is not a member of your App instance, so you could not use Me.NavigationService, you could try to find your Page object, and then use this page to Navigate, for example:

    private void Storyboard_Completed(object sender, EventArgs e)
    {
        var parent = VisualTreeHelper.GetParent(currentObject);
        while (!(parent is Page))
        {
            parent = VisualTreeHelper.GetParent(parent);
        }
        (parent as Page).NavigationService.Navigate(new Page1());
        // App.Current.
    }
    


    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 26, 2011 6:47 AM
  • Hi DarkBox,

    If you are developing with WPF, it it is, NavigationService is not a member of your App instance, so you could not use Me.NavigationService, you could try to find your Page object, and then use this page to Navigate, for example:

    private void Storyboard_Completed(object sender, EventArgs e)
    {
        var parent = VisualTreeHelper.GetParent(currentObject);
        while (!(parent is Page))
        {
            parent = VisualTreeHelper.GetParent(parent);
        }
        (parent as Page).NavigationService.Navigate(new Page1());
        // App.Current.
    }
    


    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thank you a lot for the response, and I can see where you're going with this, however I am running VB.net and not C#. I have converted it and changed it a bit to the following:

        Public Sub ArrowTransformComplete(ByVal sender As Object, ByVal e As EventArgs)
            Dim parent = VisualTreeHelper.GetParent(currentObject)
            While Not (TypeOf parent Is Page)
                parent = VisualTreeHelper.GetParent(parent)
            End While
            TryCast(parent, Page).NavigationService.Navigate(New HomePanda())
            ' App.Current.
        End Sub
    

    However, the only issue I run into now is that it tells me "currentObject" is not declared. Is currentObject a C# specific property?
    Wednesday, October 26, 2011 3:54 PM
  • currentObject is a object in your VisualTree, my method is loop your VisualTree to find your root Page object, and then navigate to new page.
    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 27, 2011 1:57 AM
  • Okay, but when I put that code into Application.xaml.vb I get the following error:

    'currentObject' is not declared. It may be inaccessible due to its protection level.

    I am using Visual Basic, and I've converted the code you posted to the one that I've posted. I understand the purpose of 'currentObject' but my program is not accepting it. Do you know of a Visual Basic equivalent to that statement or am I missing something here?

    I've tried entering 'this', 'me', 'current', 'self' etc and they are pretty much all "not declared".


    • Edited by DarkBox Thursday, October 27, 2011 2:12 AM
    Thursday, October 27, 2011 2:08 AM
  • use below code instead:

    (App.Current.MainWindow as NavigationWindow).NavigationService.Navigate(new Page1());

    it works on my side.


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Sheldon _Xiao Thursday, October 27, 2011 2:54 AM
    • Marked as answer by DarkBox Thursday, October 27, 2011 2:55 AM
    Thursday, October 27, 2011 2:26 AM
  • In a new page, I've got that code linked to a button, and it works. However, running the application and letting the animation finish doesn't navigate to a new page :/ I've see that some people have issues with the Completed event handler not working so I'll read into that.
    Thursday, October 27, 2011 2:40 AM