zoukankan      html  css  js  c++  java
  • Xamarin.formsPicker

    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;
                }
            }
        }
    }
  • 相关阅读:
    编译原理语法分析程序(最左推导)
    剑指offer:02
    剑指offer:01二维数组中的查找
    pthread编译选项(命令行,codeblocks)
    学习apue的pthread synchronize之condition variables
    pthreads之joinable和detach函数
    c++指针空间大小的决定因素
    2.3.1引用
    2.2.1变量定义
    扫描PDF417崩溃的原因找到:手机摄像头分辨率低
  • 原文地址:https://www.cnblogs.com/ThenDog/p/6790783.html
Copyright © 2011-2022 走看看