导航到按钮单击事件的下一页



我有一个按钮,它在单击时运行一些代码。然后更新其文本。我希望在单击时使用相同的按钮导航到下一页。

现在,它会自动转到NewPage。我如何才能使它在点击时进入下一页?

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

您可以添加一个全局布尔变量bool isSecondClick = false;,当用户第一次单击按钮时,设置isSecondClick = false;并更改按钮上的文本。第二次点击进入下一页。

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

您可以使用标签创建按钮:<Button Tag="1" ...

然后你可以使用这个标签来知道你是否需要更改文本或导航:

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

我希望代码能帮助你。

最新更新