Xamarin.forms--------------Picker

Guides----https://developer.xamarin.com/guides/xamarin-forms/user-interface/picker/

API-----https://developer.xamarin.com/api/type/Xamarin.Forms.Picker/

Demo: Picker省,市,区 三级联动


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class District //区
    {
        public string DistrictName { get; set; }    //区名字
        public string DistrictPostcode { get; set; } //区的邮编
    }

    public class City
    {
        public ObservableCollection<District> DistrictObservableCollection { get; set; } //区的集合

        public string CityName { get; set; } //城市的名字
    }

    public class Province
    {
        public ObservableCollection<City> CityObservableCollection { get; set; } //城市的集合

        public string ProvinceName { get; set; } //省份的名字
    }

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public static class Data
    {
        public static ObservableCollection<Province> Provinces { get; private set; }

        static Data()
        {
            Provinces = new ObservableCollection<Province>()
            {
                new Province()
                {
                    ProvinceName  = "福建省",
                    CityObservableCollection = new ObservableCollection<City>()
                    {
                        new City()
                        {
                            CityName = "福州市",
                            DistrictObservableCollection = new ObservableCollection<District>()
                            {
                                new District()
                                {
                                    DistrictName = "鼓楼区",
                                    DistrictPostcode = "350001"
                                },
                                new District()
                                {
                                    DistrictName = "晋安区",
                                    DistrictPostcode = "350002"
                                },
                                new District()
                                {
                                    DistrictName = "台江区",
                                    DistrictPostcode = "350003"
                                },
                                new District()
                                {
                                    DistrictName = "仓山区",
                                    DistrictPostcode = "350004"
                                },
                            }
                        },
                        new City()
                        {
                            CityName = "厦门市",
                            DistrictObservableCollection = new ObservableCollection<District>()
                            {
                                new District()
                                {
                                    DistrictName = "思明区",
                                    DistrictPostcode = "361001"
                                },
                                new District()
                                {
                                    DistrictName = "湖里区",
                                    DistrictPostcode = "361002"
                                },
                                new District()
                                {
                                    DistrictName = "集美区",
                                    DistrictPostcode = "361003"
                                },
                                new District()
                                {
                                    DistrictName = "海沧区",
                                    DistrictPostcode = "361004"
                                },
                                new District()
                                {
                                    DistrictName = "翔安区",
                                    DistrictPostcode = "361005"
                                },
                            }
                        },
                        new City()
                        {
                            CityName = "泉州市",
                            DistrictObservableCollection = new ObservableCollection<District>()
                            {
                                new District()
                                {
                                    DistrictName = "丰泽区",
                                    DistrictPostcode = "362001"
                                },
                                new District()
                                {
                                    DistrictName = "鲤城区",
                                    DistrictPostcode = "362002"
                                },
                                new District()
                                {
                                    DistrictName = "洛江区",
                                    DistrictPostcode = "362003"
                                },
                                new District()
                                {
                                    DistrictName = "泉港区",
                                    DistrictPostcode = "362004"
                                },
                            }
                        },
                    }
                },
                new Province()
                {
                    ProvinceName  = "浙江省",
                    CityObservableCollection = new ObservableCollection<City>()
                    {
                        new City()
                        {
                            CityName = "杭州市",
                            DistrictObservableCollection = new ObservableCollection<District>()
                            {
                                new District()
                                {
                                    DistrictName = "西湖区",
                                    DistrictPostcode = "310001"
                                },
                                new District()
                                {
                                    DistrictName = "余杭区",
                                    DistrictPostcode = "310002"
                                },
                                new District()
                                {
                                    DistrictName = "萧山区",
                                    DistrictPostcode = "310003"
                                },
                                new District()
                                {
                                    DistrictName = "下城区",
                                    DistrictPostcode = "310004"
                                },
                            }
                        },
                        new City()
                        {
                            CityName = "宁波市",
                            DistrictObservableCollection = new ObservableCollection<District>()
                            {
                                new District()
                                {
                                    DistrictName = "海曙区",
                                    DistrictPostcode = "315001"
                                },
                                new District()
                                {
                                    DistrictName = "江东区",
                                    DistrictPostcode = "315002"
                                },
                                new District()
                                {
                                    DistrictName = "江北区",
                                    DistrictPostcode = "315003"
                                },
                                new District()
                                {
                                    DistrictName = "镇海区",
                                    DistrictPostcode = "315004"
                                },
                                new District()
                                {
                                    DistrictName = "北仑区",
                                    DistrictPostcode = "315005"
                                },
                            }
                        },
                        new City()
                        {
                            CityName = "温州市",
                            DistrictObservableCollection = new ObservableCollection<District>()
                            {
                                new District()
                                {
                                    DistrictName = "鹿城区",
                                    DistrictPostcode = "325001"
                                },
                                new District()
                                {
                                    DistrictName = "龙湾区",
                                    DistrictPostcode = "325002"
                                },
                                new District()
                                {
                                    DistrictName = "瓯海区",
                                    DistrictPostcode = "325003"
                                },
                            }
                        },
                    }
                },
                new Province()
                {
                    ProvinceName  = "安徽省",
                    CityObservableCollection = new ObservableCollection<City>()
                    {
                        new City()
                        {
                            CityName = "合肥市",
                            DistrictObservableCollection = new ObservableCollection<District>()
                            {
                                new District()
                                {
                                    DistrictName = "蜀山区",
                                    DistrictPostcode = "230001"
                                },
                                new District()
                                {
                                    DistrictName = "庐阳区",
                                    DistrictPostcode = "230002"
                                },
                                new District()
                                {
                                    DistrictName = "瑶海区",
                                    DistrictPostcode = "230003"
                                },
                                new District()
                                {
                                    DistrictName = "包河区",
                                    DistrictPostcode = "230004"
                                },
                            }
                        },
                        new City()
                        {
                            CityName = "淮北市",
                            DistrictObservableCollection = new ObservableCollection<District>()
                            {
                                new District()
                                {
                                    DistrictName = "相山区",
                                    DistrictPostcode = "235001"
                                },
                                new District()
                                {
                                    DistrictName = "杜集区",
                                    DistrictPostcode = "235002"
                                },
                                new District()
                                {
                                    DistrictName = "烈山区",
                                    DistrictPostcode = "235003"
                                },
                            }
                        },
                        new City()
                        {
                            CityName = "宿州市",
                            DistrictObservableCollection = new ObservableCollection<District>()
                            {
                                new District()
                                {
                                    DistrictName = "埇桥区",
                                    DistrictPostcode = "234001"
                                },
                            }
                        },
                    }
                },
                new Province()
                {
                    ProvinceName  = "江西省",
                    CityObservableCollection = new ObservableCollection<City>()
                    {
                        new City()
                        {
                            CityName = "南昌市",
                            DistrictObservableCollection = new ObservableCollection<District>()
                            {
                                new District()
                                {
                                    DistrictName = "东湖区",
                                    DistrictPostcode = "330001"
                                },
                                new District()
                                {
                                    DistrictName = "西湖区",
                                    DistrictPostcode = "330002"
                                },
                                new District()
                                {
                                    DistrictName = "青云谱区",
                                    DistrictPostcode = "330003"
                                },
                                new District()
                                {
                                    DistrictName = "湾里区",
                                    DistrictPostcode = "330004"
                                },
                            }
                        },
                        new City()
                        {
                            CityName = "景德镇市",
                            DistrictObservableCollection = new ObservableCollection<District>()
                            {
                                new District()
                                {
                                    DistrictName = "珠山区",
                                    DistrictPostcode = "333001"
                                },
                                new District()
                                {
                                    DistrictName = "昌江区",
                                    DistrictPostcode = "333002"
                                },
                            }
                        },
                        new City()
                        {
                            CityName = "九江市",
                            DistrictObservableCollection = new ObservableCollection<District>()
                            {
                                new District()
                                {
                                    DistrictName = "浔阳区",
                                    DistrictPostcode = "332001"
                                },
                                new District()
                                {
                                    DistrictName = "庐山区",
                                    DistrictPostcode = "332002"
                                },
                            }
                        },
                    }
                }
            };

        }
    }
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="DemoTest_3_28.PickerDemo.PickerDemoPage">
    <StackLayout Orientation="Vertical"
                 VerticalOptions="Center"
                 HorizontalOptions="Center">
        <Label Text="邮编:"
               HorizontalOptions="Center"
               FontAttributes="Bold"
               FontSize="20"
               x:Name="label_Postcode" />
        <StackLayout Orientation="Horizontal">
            <Picker x:Name="provincePicker" Title="省份" SelectedIndexChanged="ProvincePicker_OnSelectedIndexChanged"/>
            <Picker x:Name="CityPicker" Title="城市" SelectedIndexChanged="CityPicker_OnSelectedIndexChanged"/>
            <Picker x:Name="DistrictPicker" Title="区" SelectedIndexChanged="DistrictPicker_OnSelectedIndexChanged"/>
        </StackLayout>
    </StackLayout>
</ContentPage>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DemoTest_3_28.Models;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace DemoTest_3_28.PickerDemo
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class PickerDemoPage : ContentPage
    {
        private ObservableCollection<Province> source;
        private string postcode = null;

        public PickerDemoPage()
        {
            InitializeComponent();
            source = Data.Provinces;
            foreach (var province in source)
            {
                provincePicker.Items.Add(province.ProvinceName);
            }

            provincePicker.SelectedIndex = 0;
        }

        private void ProvincePicker_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (provincePicker.SelectedIndex!=-1)
            {
                var result_1 = source[provincePicker.SelectedIndex].CityObservableCollection;
                CityPicker.Items.Clear();//重要, 而且触发CityPicker_OnSelectedIndexChanged,CityPicker.SelectedIndex=-1
                foreach (var city in result_1)
                {
                    CityPicker.Items.Add(city.CityName);
                }
                CityPicker.SelectedIndex = 0;
            }
        }

        private void CityPicker_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (CityPicker.SelectedIndex!=-1)
            {
                var result_2 = source[provincePicker.SelectedIndex].CityObservableCollection[CityPicker.SelectedIndex].DistrictObservableCollection;
                DistrictPicker.Items.Clear(); //重要
                foreach (var district in result_2)
                {
                    DistrictPicker.Items.Add(district.DistrictName);
                }
                DistrictPicker.SelectedIndex = 0; //重要
            }
        }

        private void DistrictPicker_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (DistrictPicker.SelectedIndex!=-1)
            {
                postcode = source[provincePicker.SelectedIndex].CityObservableCollection[CityPicker.SelectedIndex].DistrictObservableCollection[DistrictPicker.SelectedIndex].DistrictPostcode;

                label_Postcode.Text = "邮编:" + postcode;
            }
        }
    }
}
时间: 2024-12-15 23:33:58

Xamarin.forms--------------Picker的相关文章

菜鸟的Xamarin.Forms前行之路——原生Toast的简单实现方法

项目中信息提示框,貌似只有个DisplayAlert,信息提示太过于单一,且在有些场合Toast更加实用,以下是一个简单的原生Toast的实现方法 项目地址:https://github.com/weiweu/TestProject/tree/dev/Toast 共享项目 定义一个接口IToast,包括Short和Long两个方法: public interface IToast { void LongAlert(string message); void ShortAlert(string m

张高兴的 Xamarin.Forms 开发笔记:为 Android 与 iOS 引入 UWP 风格的汉堡菜单 ( MasterDetailPage )

所谓 UWP 样式的汉堡菜单,我曾在"张高兴的 UWP 开发笔记:汉堡菜单进阶"里说过,也就是使用 Segoe MDL2 Assets 字体作为左侧 Icon,并且左侧使用填充颜色的矩形用来表示 ListView 的选中.如下图 但怎样通过 Xamarin.Forms ,将这一样式的汉堡菜单带入到 Android 与 iOS 中呢? 一.大纲-细节模式简介 讲代码前首先来说说这种导航模式,官方称"大纲-细节模式"(MasterDetail).左侧的汉堡菜单称为&qu

菜鸟的Xamarin.Forms前行之路——绪言

作者入门时间不是很久,差不多一年,期间自学的东西比较杂乱,到目前为止,编程方面的知识比较薄弱.之所以做这个系列,也只是因为做了两个月的Xamarin.Forms方面的东西,由于资料和自身实力的原因,过程走的比较艰难,但所幸的是也解决了部分的问题,积累了一些经验.期望通过这个系列,和大家分享经验,查漏纠错. 作为一个菜鸟,在解决问题的时候,往往比较直接,就是仅仅为了解决问题,期间可能根本没有考虑性能等方面的问题.所以在这个系列中,问题肯定是作者亲身实践能够解决的,但是在性能资源等方面作者没有做过考

Xamarin.Forms开发APP

Xamarin.Forms+Prism(1)-- 开发准备 准备: 1.VS2017(推荐)或VS2015: 2.JDK 1.8以上: 3.Xamarin.Forms 最新版: 4.Prism 扩展,打开VS的扩展和更新,在联机中,搜索Prism,安装第一个扩展Prism Template Pack: 5.Android SDK,这个下载已经非常快了,国内已经支持Android环境下载. 6.都准备好后,请确保创建一个新的Xamarin.Forms程序后,能正常调试运行,不能调试运行的,请百度或

Xamarin.Forms之MessagingCenter

MessagingCenter是Xamarin.Forms中很有用但是很难用的东西,使用起来很方便,但是要注意的地方很多 1.Sender能为空 MessagingCenter.Send<CacheUserInfo>(UserInfo.Instance.CurrentLoginUser, "UpdateMenuIconList"); CurrentLoginUser为空的时候,会直接造成界面卡死(不执行下面的代码,但是也不报错,坑爹的地方)

Xamarin.Forms之Effects的使用

在 Xamarin.Forms 2.1.0-pre1 ,Xamarin.Forms新增了新的Effects API. Effects是一系列方法,为了给View的渲染器添加运行时改变. 然而,我想强调的是, Effects天生被设计为高可复用的. 如果一个Effect能够解决一个难题, 它可能能够在你的整个APP中使用.如果你写了一个Effect来解决你的一个难题, 你能够分享它给其他遇到同样问题的人. 这篇文章尝试展示一种方式,能够帮助我们使分享Effects这件事变得很简单. public

菜鸟的Xamarin.Forms前行之路——按钮的按下抬起事件的监控(可扩展至其他事件)

提问:监控按钮的点击事件,可以通过按钮的Click事件,或者Command绑定,那么如何监控按钮的按下与抬起,或者移动,长按,双击等事件? 解决方法:各个平台自定义渲染依赖注入. 共享项目PCL: 1先定义一个继承Button的实体类NewButton.cs public class NewButton : Button { public event EventHandler Pressed; public event EventHandler Released; public virtual

Xamarin.Forms学习之Platform-specific API和文件操作

这篇文章的分享原由是由于上篇关于Properties的保存不了,调用SavePropertiesAsync()方法也不行,所以我希望通过操作文件的方式保存我的需要的数据,然后我看了一下电子书中的第二十章和需要相关知识的第九章,这篇文章中的内容则是我学习这两章的一点记录和分享,还是那样,有错请留言指正,谢谢! 不同的平台存在着一些特定的API,通过在电子书中两章的学习,实践一下如何调用这些API和将这些API封装成公共的库,供以后的项目调用.以一个显示平台信息的小实例开始做一个简单的演示,其运行效

Xamarin.Forms学习之Page Navigation(一)

在最初接触Xamarin.Forms的时候,我是跟着Xamarin官方的名为“learning-xamarin-ebook”的pdf文档进行学习的,我在成功运行Hello world程序之后,我开始跟着pdf写上面的monkey实例,然而我却遇到了一个问题,我在列表页点击某一个Item的时候,不能如示例上面所展示的那样跳转到详细页面.也正因为此我在官方文档和电子书中寻找答案,也将自己的学习的东西做个简单的笔记和分享. 在Froms的中的提供了如下方法用于导航: Task PushAsync(Pa

Xamarin.Forms学习之初

微软的Build 2016结束的有段时间了,对于一个简单的小屌丝程序员--我来说,关注最大的无疑是Xamarin的免费(开源什么的让大神们上吧),内心激动啊.大会结束的周末我就迫不及待的安装了,然后...一脸的懵逼,更新vs后什么sdk,jdk都不知道,唯一知道的就是没法运行,后来大神们相继出了一些文章,才开始了解一些大概,有错误的请在下面留言提出,免得我误导了别人. xamarin可以简单分为一下几个: Xamarin.Forms:个人觉得这才是Xamarin真正的优势所在,真正的跨平台,一次