Example #1
0
        /// <summary>
        /// 設定 UI 事件
        /// </summary>
        private void SetEvent()
        {
            // 城市下拉選單
            cityPicker.SelectedIndexChanged += async(sender, e) =>
            {
                if (cityPicker.SelectedIndex != -1)
                {
                    var areaData = await myWebApiService.GetAreaDataAsync(cityPicker.Items[cityPicker.SelectedIndex]);

                    var areaList = JsonConvert.DeserializeObject <List <AreaData> >(areaData);

                    areaPicker.Items.Clear();

                    foreach (var area in areaList)
                    {
                        areaPicker.Items.Add(area.town);
                    }

                    SetListView();
                }
            };
            // 搜尋按鈕
            areaPicker.SelectedIndexChanged += async(sender, e) =>
            {
                if (areaPicker.SelectedIndex != -1)
                {
                    var city       = cityPicker.Items[cityPicker.SelectedIndex];
                    var crea       = areaPicker.Items[areaPicker.SelectedIndex];
                    var resultData = await myWebApiService.GetFamilyStoreDataAsync(city, crea);

                    myStoreDataList = JsonConvert.DeserializeObject <List <FamilyStore> >(resultData);

                    Debug.WriteLine(myStoreDataList.Count);
                    SetListView();
                }
            };
        }
        public MyListViewPage(string title)
        {
            myWebApiService = new WebApiService();

            // 搜尋店鋪 Button 按鈕

            searchButton = new Button { Text = "Search" };

            // 建立縣市 Picker 下拉選單
            cityPicker = new Picker
            {
                Title = "縣市選單",
                VerticalOptions = LayoutOptions.CenterAndExpand
            };

            // 初始化選項
            foreach (string cityName in nameToCity.Keys)
            {
                cityPicker.Items.Add(cityName);
            }

            // 預設選取第一個項目
            cityPicker.SelectedIndex = 0;

            areaPicker = new Picker
            {
                Title = "鄉鎮市區選單",
                VerticalOptions = LayoutOptions.CenterAndExpand
            };

            foreach (var item in defaultAreaList)
            {
                areaPicker.Items.Add(item.town);
            }

            areaPicker.SelectedIndex = 4;

            // 註冊 cityPicker SelectedIndexChanged 事件
            cityPicker.SelectedIndexChanged += async (sender, args) =>
            {
                if (cityPicker.SelectedIndex != -1)
                {
                    // 將舊有的鄉鎮區項目刪除
                    // Error : 因為沒辦法全部刪除,會顯示 Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index,所以留下一個,加新項目後在刪掉
                    int count = areaPicker.Items.Count - 1;

                    for (int i = 0; i < count; i++)
                    {
                        areaPicker.Items.RemoveAt(0);
                    }

                    // 透過 api 拿到對應縣市的鄉鎮區項目
                    var areaResult = await myWebApiService.GetAreaDataAsync(cityPicker.Items[cityPicker.SelectedIndex]);

                    // 將 Json 轉為 List<AreaData>
                    var myAreaDataList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<AreaData>>(areaResult);

                    // 替鄉鎮區下拉選單,增加對應縣市的新項目
                    foreach (var item in myAreaDataList)
                    {
                        areaPicker.Items.Add(item.town);
                    }

                    // 將之前留下來最後一個舊項目刪除
                    areaPicker.Items.RemoveAt(0);

                    // 預設選取第一個項目
                    areaPicker.SelectedIndex = 0;
                }
            };

            // 註冊 areaPicker SelectedIndexChanged 事件
            areaPicker.SelectedIndexChanged += (sender, args) =>
            {
                // log 記錄
                if (areaPicker.SelectedIndex != -1)
                {
                    Debug.WriteLine("AreaName is " + areaPicker.Items[areaPicker.SelectedIndex]);
                }
            };

            Title = title;

            var listView = new ListView
            {
                IsPullToRefreshEnabled = true,
                RowHeight = 80,
                ItemsSource = new List<StoreData>()
                {
                    new StoreData {Name = "全家大安店", Address = "台北市大安區大安路一段20號", Tel = "02-27117896"},
                    new StoreData {Name = "全家仁慈店", Address = "台北市大安區仁愛路四段48巷6號", Tel = "02-27089002"},
                    new StoreData {Name = "全家明曜店", Address = "台北市大安區仁愛路四段151巷34號", Tel = "02-27780326"},
                    new StoreData {Name = "全家國泰店", Address = "台北市大安區仁愛路四段266巷15弄10號", Tel = "02-27542056"},
                    new StoreData {Name = "全家忠愛店", Address = "台北市大安區仁愛路四段27巷43號", Tel = "02-27314580"},
                },
                ItemTemplate = new DataTemplate(typeof(MyListViewCell))
            };

            listView.ItemTapped += (sender, e) =>
            {
                var baseUrl = "https://www.google.com.tw/maps/place/";
                var storeData = e.Item as StoreData;

                if (storeData != null)
                    Device.OpenUri(new Uri($"{baseUrl}{storeData.Address}"));

                ((ListView)sender).SelectedItem = null;
            };

            // 註冊 searchButton Clicked 事件
            searchButton.Clicked += async (sender, e) =>
            {
                // 透過 api 拿到對應區域的全家店鋪資料
                var resultData = await myWebApiService.GetFamilyStoreDataAsync(cityPicker.Items[cityPicker.SelectedIndex], areaPicker.Items[areaPicker.SelectedIndex]);

                // 將 Json 轉為 List<FamilyStore>
                var familyStoreList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FamilyStore>>(resultData);

                // 將 List<FamilyStore> 轉為 List<StoreData>
                var myStoreDataList = familyStoreList.Select(p => new StoreData()
                {
                    Address = p.addr,
                    Name = p.NAME,
                    Tel = p.TEL
                }).ToList();

                // 更換店鋪資訊
                listView.ItemsSource = myStoreDataList;
                Debug.WriteLine(myStoreDataList.Count);
            };

            Padding = new Thickness(0, 20, 0, 0);
            Content = new StackLayout
            {
                Orientation = StackOrientation.Vertical,
                Children =
                {
                    cityPicker,
                    areaPicker,
                    searchButton,
                    new Label
                    {
                        HorizontalTextAlignment= TextAlignment.Center,
                        Text = Title,
                        FontSize = 30
                    },
                    listView
                }
            };
        }