(VB) 使用按钮在 XAML 页面之间导航

问题描述 投票:0回答:1

如何使用按钮控件在 XAML 应用程序的不同页面之间导航。例如,您创建了一个登录表单,输入详细信息后,下一个菜单页面将显示

我尝试使用“.Show”方法将页面附加到我的代码中的单击事件,就像我对 Windows 窗体所做的那样,但代码编辑器没有将其列在我可以使用的所有可能方法中,所以我把它在我自己身上,它给了我这个错误

错误 BC30456“显示”不是“SelectAction”的成员。

vb.net uwp uwp-xaml
1个回答
0
投票

可以参考官方文档实现两个页面之间的导航,它使用 Frame.Navigate 方法来导航页面。

首先需要创建一个登录页面,然后修改App.xaml.vb中启动的页面,将App.xaml.vb中的

MainPage
更改为
loginPage
。使用loginPage中的
Public Function Navigate (sourcePageType As Type, parameter As Object) As Boolean 
方法来传递信息。

重写Mainpage中的

OnNavigatedTo
方法,获取登录页面传递的参数。

这里有 VB 代码示例:

应用程序.xaml.vb

If e.PrelaunchActivated = False Then
    If rootFrame.Content Is Nothing Then
        ' When the navigation stack isn't restored navigate to the first page,
        ' configuring the new page by passing required information as a navigation
        ' parameter
        rootFrame.Navigate(GetType(loginPage), e.Arguments)
    End If

    ' Ensure the current window is active
    Window.Current.Activate()
End If

登录页面.xaml

<Grid>
    <StackPanel VerticalAlignment="Center">
        <TextBlock HorizontalAlignment="Center" Text="Enter your name"/>
        <TextBox HorizontalAlignment="Center" Width="200" x:Name="name"/>
        <Button Content="Click to go to page 2"
                            Click="Button_Click"
                            HorizontalAlignment="Center"/>
    </StackPanel>
</Grid>

登录页面.xaml.vb

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    'Navigate to Page2
    Me.Frame.Navigate(GetType(MainPage), name.Text)
End Sub

主页.xaml

<Grid>
    <TextBlock  x:Name="greeting" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>

MainPage.xaml.vb

  Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
        MyBase.OnNavigatedTo(e)
        Dim param As String = e.Parameter
        greeting.Text = param
© www.soinside.com 2019 - 2024. All rights reserved.