如何在WPF中使用按钮导航到其他页面

14

我在名为Page2.xaml的第二个.xaml页面中设置了一个按钮,当用户单击该按钮时,我希望将其带到Page2.xaml

这是我在Page1.xaml中的按钮代码:

<Grid>
    <Button x:Name="localModeBtn" 
            Style="{StaticResource MainButtonStyle}"  
            Content="local mode" 
            Click="localModeBtn_Click" />
</Grid>

按钮事件处理程序:

private void localModeBtn_Click(object sender, RoutedEventArgs e)
    {
        Uri uri = new Uri("Page2.xaml", UriKind.Relative);
        this.NavigationService.Navigate(uri);
    }

在单击按钮后,我收到一个错误,显示“无法定位资源page2.xaml”,但事实上Page2.xamlPag1.xaml在同一个文件夹中,所以我不知道问题出在哪里?


定义“taken to”。您希望它加载在Page1中还是要创建一个新的导航窗口? - paparazzo
1
请查看我以前关于WPF页面导航设计的帖子:https://dev59.com/G3PYa4cB1Zd3GeqPgj0S#17071514 - Jasti
9个回答

25

我自己问题的解决方案:

提供自己问题的解决方案可能会让我感到有点傻,但多亏了Jasti的链接,我成功解决了我的代码问题。由于他只发布了评论,我不能将其标记为答案,所以在这里提供解决方案。

我将NavigationWindow更改为Window并插入以下内容:

<DockPanel>
    <Frame x:Name="_NavigationFrame" NavigationUIVisibility="Hidden" />
</DockPanel>

在 MainWindow.xaml.cs 的构造函数中,我添加了以下内容:

_NavigationFrame.Navigate(new Page1());

然后最后一步是调整按钮事件处理程序为:

this.NavigationService.Navigate(new Uri("Pages/Page2.xaml", UriKind.Relative));

3

你应该使用这个,这对于有用:

var Page2= new Page2(); //create your new form.
Page2.Show(); //show the new form.
this.Close(); //only if you want to close the current form.

在您的解决方案中,有一个名为page.xaml的页面的变量类型。 之后,您应该使用它的方法来实现功能。


1
通常情况下,如果答案包含代码的意图和解决问题的原因,而不会引入其他问题,那么这些答案会更有帮助。 - Tom Aranda
好的!如果需要,我可以提供一个简短的描述。 - Milad Xandi

1

你不需要任何C#代码,只需在XML中完成:

<Button Content="local mode"
    Command="NavigationCommands.GoToPage"
    CommandParameter="/Page2.xaml"/>

(重新格式化的代码未经测试)


5
当我尝试应用Command="NavigationCommands.GoToPage"时,我的按钮会变为禁用状态。 - sohaiby

0
private void Navigate_Click(object sender, RoutedEventArgs e)//By Prince Jain 
{
    this.NavigationService.Navigate(new Uri("Page3.xaml", UriKind.Relative));
}

0

在视图中(.xaml 文件):

<StackPanel>
    <TextBlock>Outside area of frame</TextBlock>
    <StackPanel Height="20" Width="400" VerticalAlignment="Top" Orientation="Horizontal">
        <Button Content="Page 1" Width="200" Click="Button_Click"/>
        <Button Content="Page 2" Width="200" Click="Button_Click_1"/>
    </StackPanel>
    <Frame Name="Main" Height="300" Width="700" Background="LightGray">
    </Frame>
</StackPanel>

在代码后台(.xaml.cs文件)中:
private void Button_Click(object sender, RoutedEventArgs e)
    {
        Main.Content = new Page1();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Main.Content = new Page2();
    }

这两个按钮现在将帮助您在名为Page1和Page2的页面之间导航。(如果页面存在于文件夹中,请注意命名空间)。

0
首先,您需要在窗口中添加一个框架来容纳页面,因此在我的MainWindow.xaml中,我会像这样添加一个框架:
<Frame x:name="mainFrame"/>

然后,我们将在MainWindow.xaml中的导航按钮上添加一个事件监听器,像这样:
<Button
    x:Name="navBtn"       
    Content="LIVE VIEW"       
    Click="NavBtn_Click">
</Button>

现在,我们已经设置好了窗口的 XAML,接下来我们将转到 MainWindow.xaml.cs 并编写我们的代码:

//this function should be automatically generated
private void NavBtn_Click(object sender, RoutedEventArgs e) 
    {
        //we'll write this line, which opens our page
        mainFrame.Content = new YourPage(); 
    }

好了,你的导航就准备好了!


0

如果您想要一个单独的窗口

NavigationWindow navWIN = new NavigationWindow();
navWIN.Content = new pageWFbchAdmin();
navWIN.Show(); 
//winBchAdmin.ShowDialog();

0
我的解决方案是在主窗口MainWindow.xaml中添加一个框架。
<Frame Name="Main" Content="" Margin="127,0,0,0" Background="#FFFFEDED" />

导航:

1- 点击按钮从主窗口导航:

private void Button_Click(object sender, RoutedEventArgs e)
{
   // navigate to pages/projects.xaml inside the main frame
   Main.Content = new MyProject.Pages.Projects();
}

2- 如果在框架内从页面导航,例如 Projects.xaml

// declare a extension method in a static class (its your choice if you want to reuse)
// name the class PageExtensions (you can choose any name)

namespace MyProject
{
    public static class PageExtensions
    {
        public static void NavigateTo(this Page page, string path)
        {
            Frame pageFrame = null;
            DependencyObject currParent = VisualTreeHelper.GetParent(page);

            while (currParent != null && pageFrame == null)
            {
                pageFrame = currParent as Frame;
                currParent = VisualTreeHelper.GetParent(currParent);
            }

            if (pageFrame != null)
            {
                pageFrame.Source = new Uri(path, UriKind.Relative);
            }
        }
    }
}


// to navigate from 'pages/projects.xaml' to another page
// heres how to call the extension on button click

this.NavigateTo("NewProject.xaml");

此外,您可以添加另一个扩展方法,该方法期望另一个Page对象,以便在需要向构造函数传递参数时使用。
// overloading NavigateTo
public static void NavigateTo(this Page page, Page anotherPage)
{
    Frame pageFrame = null;
    DependencyObject currParent = VisualTreeHelper.GetParent(page);

    while (currParent != null && pageFrame == null)
    {
        pageFrame = currParent as Frame;
        currParent = VisualTreeHelper.GetParent(currParent);
    }

    // Change the page of the frame.
    if (pageFrame != null)
    {
        pageFrame.Navigate(anotherPage);
    }
}

// usage
this.NavigateTo(new Pages.EditProject(id));

0

使用任何容器并将内容绑定到视图模型或代码后台中的任何属性。 之后,您只需通过设置新页面来更新属性,并调用PropertyChanged事件(请参见INotifyPropertyChanged接口)。这将更新容器的内容,您可以显示任何想要的内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接