【问题标题】:Navigate to next page on button click event在按钮单击事件上导航到下一页
【发布时间】:2016-07-12 14:18:18
【问题描述】:

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

现在,它会自动转到 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);
}

【问题讨论】:

  • “下一页”是什么?请包含与此问题相关的完整代码。最好的问候,
  • 什么是“下一页”?
  • @AlexBell 它是 WPF 中的一个页面。
  • 你试过this.NavigationService.Navigate(typeof(NextPage);吗?
  • @ganchito55 它有效。但我希望在用户再次单击按钮 (button1) 后进行导航。谢谢

标签: c# wpf xaml user-interface button


【解决方案1】:

您可以添加一个全局布尔变量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;
        }
}

【讨论】:

    【解决方案2】:

    您可以使用标签创建按钮: <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;
        }      
    
    }
    

    我希望代码对你有所帮助。

    【讨论】:

    • 谢谢!但是,它仍然不等待第二次单击导航到 NextPage。 @PLOW 的答案正是我想要的。
    • @Imsa 对不起,我弄错了,我忘记了返回:S
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2021-06-15
    • 1970-01-01
    相关资源
    最近更新 更多