【问题标题】:WP8 LongListSelector - Navigating to different pagesWP8 LongListSelector - 导航到不同的页面
【发布时间】:2013-12-27 23:05:11
【问题描述】:

我最近为我的 Windows Phone 8 应用程序创建了一个 LongListSelector。然而,我现在想要实现的是当用户点击一个项目时导航到另一个页面,但我不知道我的代码中放了什么,而且我也不是 100% 我应该在哪里为每个人插入我的 uri页。有谁知道我如何做到这一点?

如果有人能帮助我解决这个问题,我将不胜感激。

非常感谢。

Partial Public Class Station_Chooser
    Inherits PhoneApplicationPage

    Public Sub New()
        InitializeComponent()

        Dim source As New List(Of Glasgow)()
        source.Add(New Glasgow("Bridge Street"))
        source.Add(New Glasgow("Buchanan Street"))
        source.Add(New Glasgow("Cessnock"))
        source.Add(New Glasgow("Cowcaddens"))
        source.Add(New Glasgow("Govan"))
        source.Add(New Glasgow("Hillhead"))
        source.Add(New Glasgow("Ibrox"))
        source.Add(New Glasgow("Kelvinbridge"))
        source.Add(New Glasgow("Kelvinhall"))
        source.Add(New Glasgow("Kinning Park"))
        source.Add(New Glasgow("Patrick"))
        source.Add(New Glasgow("Shields Road"))
        source.Add(New Glasgow("St Enoch"))
        source.Add(New Glasgow("St George's Cross"))
        source.Add(New Glasgow("West Street"))


        Dim DataSource As List(Of AlphaKeyGroup(Of Glasgow)) = AlphaKeyGroup(Of Glasgow).CreateGroups(source, System.Threading.Thread.CurrentThread.CurrentUICulture, Function(s As Glasgow)
                                                                                                                                                                          Return s.Station
                                                                                                                                                                      End Function, True)
        GlasgowSubway.ItemsSource = DataSource

    End Sub

    Public Class Glasgow
        Public Property Station() As String
            Get
                Return m_Station
            End Get
            Set(value As String)
                m_Station = value
            End Set
        End Property
        Private m_Station As String

        Public Sub New(station As String)
            Me.Station = station
        End Sub
    End Class

End Class

【问题讨论】:

    标签: vb.net xaml windows-phone-8 windows-phone longlistselector


    【解决方案1】:

    有一些方法可以做到这一点。其中一个在this SO question 中显示,可能不是最好或最漂亮的方式,而是一种非常简单的方式。

    SelectionChanged附加事件处理程序,在处理程序中添加导航到新页面的命令,设置SelectedItem = null

    我假设目标页面是所选任何项目的同一页面,仅显示不同的内容/数据。您需要在 Uri 参数中传递所选项目的签名,以使目标页面能够相应地显示信息。例如:

    NavigationService.Navigate(New Uri("/nextpage.xaml?selectedStation=" & selectedItem.Station, UriKind.Relative))
    

    更新:

    当您澄清每个项目的目标页面会有所不同时,我之前的回答仍然有效。只需要修改后面的部分,不使用 Uri 参数。 所以这就是我的想法。在Glasgow 类中添加另一个属性,说它StationId。初始化时:

    //the second parameter is StationId value
    source.Add(New Glasgow("Bridge Street", "BridgeStreet"))
    

    然后用 StationId.xaml 模式命名每个页面,因此 Bridge Street 的页面应该是 BridgeStreet.xaml。这样,在SelectionChanged 事件处理程序中,您可以在不使用Select Case selectedItem.Station ... 的情况下导航到相应的页面:

    NavigationService.Navigate(New Uri(selectedItem.StationId & ".xaml", UriKind.Relative))
    

    注意:模型中有多个属性(在本例中为StationStationId)需要您指定要在LongListSelector 中显示的属性。如果您还不知道,请查看此link 以了解如何操作。

    【讨论】:

    • 响应您的回答,目标页面不一样。有 15 个预期目的地,每个目的地都有自己的 uri/page。
    • @m.findlay93 更新了我的答案以处理每个项目的不同 Uri
    • 我的每个页面都已经有一个 name.xaml,如果我在我的其他帖子中使用点击事件(就像我为我的 WP7 项目所做的那样),它会工作吗?:[链接]@ 987654323@
    • 是的,它应该可以工作。顺便说一句,如果我不能用这个说同样的问题,那就非常接近了:p
    • 太棒了!有用!非常感谢。你想让我在我的应用程序的“感谢”页面中提及你吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多