简体   繁体   中英

Navigate to next page on button click event

I have a button that runs some code when clicked. It then updates its text. I would like the same button to navigate to next page when clicked.

Right now, it automatically goes to NewPage. How can I make it such that when it's clicked, it goes to next page?

private void Button1_Click(object sender, RoutedEventArgs e) {
    // some code here
    Button1.Content = "Next";
    // here is the problem
    NewPage np = new NewPage();
    this.NavigationService.Navigate(np);
}

You can add a global bool variable bool isSecondClick = false; and when user clicks on the button for the first time set isSecondClick = false; and change the text on the button. On the second click go the next page.

private void Button1_Click(object sender, RoutedEventArgs e) 
{         
        if (isSecondClick)
        {   
            NewPage np = new NewPage();
            this.NavigationService.Navigate(np);            
        }
        else
        { 
            Button1.Content = "Next";
            isSecondClick = true;
        }
}

You can create your Button with a Tag: <Button Tag="1" ...

Then you can use this tag to know if you need to change the text or navigate:

private void Button1_Click(object sender, RoutedEventArgs e) {
    if(Button1.Tag==1){
       Button1.Content = "Next";
       Button1.Tag=2;
       return;
    }
    if(Button1.Tag==2){
       this.NavigationService.Navigate(typeof(NextPage);
       Button1.Tag=1;
       return;
    }      

}

I hope that code helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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