将数据从ListView传递到TextBlock

时间:2015-04-06 13:04:46

标签: c# listview windows-phone-8.1 winrt-xaml message-passing

在我的Windows Phone 8.1 RT项目中,我有两页设置,Page1.xaml和Page2.xaml。 Page1.xaml有一个名为 p1tbl 的文本块,其抽头事件p1tbl_Tapped将您带到Page2.xaml。它有一个国家/地区列表(在列表视图中,其中的所有内容都可以正常工作。我想知道,如何将ListView选项(比如名为丹麦的国家/地区)传递给文本块 p1tbl (用于导航到第2页的相同文本块)。

还是有另一种解决方法,没有组合框? ListView是必不可少的,因为我需要使用SemanticZoom。

似乎无法解决这个问题!

谢谢!

1 个答案:

答案 0 :(得分:1)

有几种方法可以在页面之间传递数据。当然可以。

1。您可以通过指定静态变量并在Page2.xaml中使用它来发送数据。您必须使用列表框的SelectionChanged事件。请查看下面的示例

Page2.xaml

public class Country
{
    public string Name { get; set; }
    public string President { get; set; }
}

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //currentItem is a variable specified in General class
    //Also in this example, i supposed you have country class that contains information about a country
    //Use Country.Name to get name of selected country
    General.currentItem = (Country)ListView.SelectedItem;
    NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
}

的Page1.xaml

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    if (General.currentItem != null)
        p1tbl.Text = General.currentItem.Name;
}

2. 您还可以在Web开发中使用与查询字符串相同的导航字符串中发送数据。以下是一个例子:

NavigationService.Navigate(new Uri("/Page1.xaml?country=" + ((Country)ListView.SelectedItem).Name, UriKind.Relative));

不要忘记处理所选项目的空状态。有关查询字符串的更多信息(第二种方式),请查看此问题。

Passing a string between pages in Windows Phone 8

如果我错了,请有人帮我纠正。