【问题标题】:Passing data from a ListView to a TextBlock将数据从 ListView 传递到 TextBlock
【发布时间】:2015-06-10 21:18:59
【问题描述】:

在我的 Windows Phone 8.1 RT 项目中,我有一个两页设置,Page1.xaml 和 Page2.xaml。 Page1.xaml 有一个名为 p1tbl 的文本块,其点击事件 p1tbl_Tapped 会将您带到 Page2.xaml。它有一个国家列表(在 listview 中,上面的一切都很好。我想知道,如何将 ListView 选择(比如一个名为丹麦的国家)传递给文本块 p1tbl(用于导航到 page2 的相同文本块)。

或者有没有其他方法可以解决这个问题,没有组合框? ListView 是必不可少的,因为我需要使用 SemanticZoom。

似乎无法弄清楚这一点!

谢谢!

【问题讨论】:

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


    【解决方案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

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

    【讨论】:

    • 那行不通。另外,它是一个 RT 项目。不是银光。有没有办法可以使用 Windows.Storage.ApplicationDataContainer?我试过了,但我得到一个“在 listview_test2.exe 中发生'System.NullReferenceException'类型的异常,但未在用户代码中处理”错误。
    • 我这样做了:在 ItemClick 我把这个:Windows.Storage.ApplicationDataContainer localStorage = Windows.Storage.ApplicationData.Current.LocalSettings; localStorage.Values["c"] = listofallcountries.SelectedItem;在 Page1 的 OnNavigatedTo 中,我放了这个:Windows.Storage.ApplicationDataContainer localStorage = Windows.Storage.ApplicationData.Current.LocalSettings; p1tbl_country.Text = localStorage.Values["c"].ToString();有什么问题?
    • ItemClick 在 Page2 中,它是 p1tbl.Text 而不是 Page1 中的 p1tbl_country.Text,输入错误,所以它是 Windows.Storage.ApplicationDataContainer localStorage = Windows.Storage.ApplicationData.Current。本地设置; p1tbl.Text = localStorage.Values["c"].ToString();
    • 您是否创建了 Country 数据类型的集合并将其作为 itemssource 提供给 listview?
    • 好的,我现在将给你一个广泛的尝试,让你知道。
    猜你喜欢
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    相关资源
    最近更新 更多