Xamarin Picker 选定的项目持久存在于多个页面上 [英] Xamarin Picker selected item persisting on multiple pages

查看:36
本文介绍了Xamarin Picker 选定的项目持久存在于多个页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:也许挑战可以这样表述:如何将内容页面中的单个属性绑定到 Xamarin Forms 中的全局存储变量?

Question: Perhaps the challenge could be stated this way: How do I bind a single property in a content page to a globally stored variable in Xamarin Forms?

详情:我正在使用 MVVM 模式.我有一个带有 Picker 对象的导航内容页面(3 个此类页面中的 1 个),该对象是从 collectionModel 动态填充的,并且所述模型是读/写的.我试图通过所有 3 个内容页面保留 SelectedItem(或索引,以最合适的方式为准),以便从页面到页面的导航显示相同的项目(从用户的角度来看).我该怎么做?

Details: I am using the MVVM pattern. I have a navigation content page (1 of 3 such pages) with a Picker object which is populated dynamically from the collectionModel and said model is read/write. I am attempting to persist the SelectedItem (or index, whichever is most appropriate) thru all 3 content pages such that navigation from page to page shows the same item (from the user's perspective). How should I do this?

我可以在 ContentPage_Appearing() 事件中手动设置 Picker.SelectedIndex.我更愿意使用绑定.

I can set the Picker.SelectedIndex manually in ContentPage_Appearing() event. I would much rather use binding.

推荐答案

请按照以下步骤操作:

  1. 创建一个静态类,像这样:

  1. Create a static class, like this:

public static class DataClass
{
   public static int PickerSelectedIndex = 0;
}

  • 在每个内容页面的ContentPage标记中添加xmlns:local,如下所示:

  • Add the xmlns:local in ContentPage mark in each Content Page, like this:

    xmlns:local="clr-namespace:DataPersist"
    

  • 在每个页面的 Xaml 中为您的控件绑定数据,如下所示:

  • Binding the data for your controls in each page's Xaml, like this:

    <Picker  x:Name="picker" SelectedIndex="{x:Static local:DataClass.PickerSelectedIndex}">
    

  • 它是这样工作的:

    这篇关于Xamarin Picker 选定的项目持久存在于多个页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆