【问题标题】:Transferring ListView selection from Page2 to Page1将 ListView 选择从 Page2 转移到 Page1
【发布时间】:2015-03-29 06:53:22
【问题描述】:

我有一个项目需要我询问用户的国家和电话号码。电话号码部分工作正常。就其本身而言,乡村部分也运行良好。考虑到这是一个 2 页的项目,我在 Page2.xaml 中有一个 ListView,它显示所有国家/地区的列表(数据绑定到具有我的 ViewModel 的 .cs 文件(类文件))。在 Page1.xaml 中,我有一个文本块(和一个我们不需要担心的电话号码文本框),其 tapped 属性导航到 Page2(并显示国家/地区列表)。我想知道:

  1. 如何从列表中(即在 Page2 中)获取所选项目并将其显示在文本块中(在 Page1 中)。

  2. 如果有 1 页的方式来实现这一点,也就是说,如果我可以使用 PlaceholderText 之类的内容(即 选择您的国家/地区)显示列表视图本身,并且当它被点击,它会打开整个国家列表。这类似于注册 WhatsApp 或 Line 或此类服务。

如果有办法实现#2,请引导我走向正确的道路。

仅供参考,对于#1,我尝试通过放置来实现本地存储

Windows.Storage.ApplicationDataContainer localsetting = Windows.Storage.ApplicationData.Current.LocalSettings; lstv_countries.SelectedItem = localsetting.Values["Countries"].ToString();

在ListView(Page2)的ItemsSelection事件中,然后

Windows.Storage.ApplicationDataContainer localsetting = Windows.Storage.ApplicationData.Current.LocalSettings; localsetting.Values["Countries"] = testText.Text;

在文本块的 GotFocus 事件中(在 Page1 中)。这不起作用:(

感谢您的回复。

谢谢!

【问题讨论】:

    标签: c# xaml windows-phone-8.1


    【解决方案1】:

    您可以使用组合框控件轻松实现相同的目标。

    参考https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh780616.aspx

    编辑 针对您的评论,我建议您这样做:

    在 NavigatedTo 部分设置:

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            int x;
            if (int.TryParse(e.Parameter.ToString(), out x))
            {
                textText.Text=CountryList[x].Name
            }
            else
            {
                textText.Text="--Select your Country--";
            }
        }
    

    在您的 page2 中,在您的 selectionchanged 事件处理程序中设置它

    Frame.Navigate( typeof( Page1),Listview.SelectedIndex );
    

    【讨论】:

    • 嗨,谢谢。我想使用 SemanticZoom 功能。这就是为什么我热衷于使用它。你有什么建议?
    • 如果您坚持使用相同的方法,那么通过页面保持数据可用的最佳方法是在 App.xaml.cs 文件中创建一个静态变量。在第 2 页进行选择时更新它并在第 1 页使用它
    • 我在 ListView 的 SelectionChanged 事件中使用了这个逻辑 - Windows.Storage.ApplicationDataContainer localsettings = Windows.Storage.ApplicationData.Current.LocalSettings; Windows.Storage.ApplicationDataContainer 容器 = localsettings.CreateContainer("Countries", Windows.Storage.ApplicationDataCreateDisposition.Always); if (localsettings.Containers.ContainsKey("Countries")) { lstv_countries.SelectedItem = localsettings.Containers["Countries"].Values["Countries"]; } App.Global_Variable = (string)lstv_countries.SelectedItem;正如你所说,Global_Variable 是 App.cs 中的静态变量
    • 在 Page1 的 Textblock 的 GotFocus 事件中,我把这个:Windows.Storage.ApplicationDataContainer localsettings = Windows.Storage.ApplicationData.Current.LocalSettings; bool hasContainer = localsettings.Containers.ContainsKey("Countries");布尔 hasSettings = false; if (hasContainer) {hasSettings = localsettings.Containers["Countries"].Values.ContainsKey("Countries");} testText.Text = localsettings.Containers["Countries"].Values.ContainsKey("Countries").ToString( );它仍然不接受所选项目。你有什么建议? :(
    • 您确定当您导航回第 1 页时会触发此 textblock.gotfocus 事件吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 2012-10-20
    • 2012-11-07
    • 2011-09-09
    相关资源
    最近更新 更多