Ejemplo n.º 1
0
        public SearchViewModel()
        {
            SearchHistoryList = new ObservableCollection <string>();

            //InitSearchPage();

            SearchCommand = new Command <string>(async(str) =>
            {
                if (!Tools.IsNetConnective())
                {
                    CrossToastPopUp.Current.ShowToastError("无网络连接,请检查网络。", ToastLength.Long);
                    return;
                }

                if (string.IsNullOrEmpty(str))
                {
                    CrossToastPopUp.Current.ShowToastWarning("请输入关键词", ToastLength.Short);
                }
                else
                {
                    if (!SearchHistoryList.Contains(str))
                    {
                        SearchedItem searchedItem = new SearchedItem
                        {
                            createTime     = DateTime.UtcNow.ToString(),
                            searchedString = str
                        };
                        await LocalDatabaseHelper <SearchedItem> .InsertOrReplaceAsync(searchedItem);
                    }

                    ProductListPage productListPage = new ProductListPage(str);
                    SearchString = "";
                    await Application.Current.MainPage.Navigation.PushAsync(productListPage);
                }
            }, (str) => { return(true); });

            ClearCommand = new Command(async() =>
            {
                bool action = await Application.Current.MainPage.DisplayAlert("确认", "删除所有搜索历史?", "确认", "取消");
                if (action)
                {
                    await localDatabaseService.DeleteAllSearchedItem();
                    InitSearchPage();
                }
            }, () => { return(true); });
        }