2016-11-28 126 views
0

我有一個gridview從JSON數據鏈接的數據http://..../mobileapp/GetPostByCategoryXMLa?term_id=(item_id) 我想如果沒有互聯網連接,當我點擊「再試一次」,它可以刷新頁面(當有一個連接互聯網)。 XAML:互聯網連接丟失時的按鈕處理程序

<Button x:Name="refreshBtn" Margin="0,15,0,0" Foreground="White" Width="100" Height="40" Content="Try Again" Background="#FF3C3C3C" BorderBrush="White" BorderThickness="1" Click="refreshBtn_Click"/> 

全碼:

public sealed partial class FurnitureCategory : Page 
{ 
    private ObservableCollection<Furniture> datasource = new ObservableCollection<Furniture>(); 

    public FurnitureCategory() 
    { 
     this.InitializeComponent(); 
    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     //FurnitureHome item = e.Parameter as FurnitureHome; 
     Furniture detail = e.Parameter as Furniture; 
     StoreDetail(int.Parse(detail.ID));//send id 
     pageTitle.Text = detail.Title.ToString(); 
    } 

    private async void StoreDetail(int id) 
    { 
     statusKosong.Visibility = Visibility.Collapsed; 
     koneksiErrorStack.Visibility = Visibility.Collapsed; 
     requestErrorStack.Visibility = Visibility.Collapsed; 
     ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile(); 
     if (connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess) 
     { 
      loading.IsActive = true; 
      try 
      { 
       Furniture detail = new Furniture(); 

       var httpClient = new HttpClient(new HttpClientHandler()); 
       string urlPath = "http://.../mobileapp/GetPostByCategoryXMLa?term_id="+id; 

       var values = new List<KeyValuePair<string, string>> 
       { 

       }; 

       HttpResponseMessage response = await httpClient.PostAsync(urlPath, new FormUrlEncodedContent(values)); 
       response.EnsureSuccessStatusCode(); 

       if (!response.IsSuccessStatusCode) 
       { 
        loading.IsActive = false; 
        RequestException(); 
       } 

       string jsonText = await response.Content.ReadAsStringAsync(); 
       JsonObject jsonObject = JsonObject.Parse(jsonText); 
       JsonArray jsonData1 = jsonObject["posts"].GetArray(); 

       foreach (JsonValue groupValue in jsonData1) 
       { 

        JsonObject groupObject = groupValue.GetObject(); 

        double menuId = groupObject["post_id"].GetNumber(); 
        string title = groupObject["post_title"].GetString(); 
        string image = groupObject["featured_image"].GetString(); 
        string deskripsi = groupObject["post_clean"].GetString(); 

        Furniture file = new Furniture(); 
        file.ID = menuId.ToString(); 
        file.Title = title; 
        file.Gambar = image; 
        file.Deskripsi = deskripsi; 

        datasource.Add(file); 
       } 

       if (jsonData1.Count > 0) 
       { 
        itemGridView.ItemsSource = datasource; 
       } 
       else 
       { 
        loading.IsActive = false; 
        statusKosong.Visibility = Visibility.Visible; 
       } 
      } 
      catch (HttpRequestException ex) 
      { 
       loading.IsActive = false; 
       RequestException(); 
      } 
     } 
     else 
     { 
      loading.IsActive = false; 
      ConnectionException(); 
     } 
    } 

    private void ConnectionException() 
    { 
     koneksiErrorStack.Visibility = Visibility.Visible; 
    } 

    private void RequestException() 
    { 
     requestErrorStack.Visibility = Visibility.Visible; 
    } 

    private void refreshBtn_Click(object sender, RoutedEventArgs e) 
    { 
     Furniture detail = new Furniture(); 
     StoreDetail(int.Parse(detail.ID)); 
    } 

    private void ItemView_ItemClick(object sender, ItemClickEventArgs e) 
    { 
     Furniture itemDetail = e.ClickedItem as Furniture; 
     DetailId.Text = itemDetail.ID; 
     DetailJudul.Text = itemDetail.Title; 
     DetailDeskripsi.Text = itemDetail.Deskripsi; 
     this.Frame.Navigate(typeof(FurnitureDetail), itemDetail); 
    } 

    private void Image_ImageOpened(object sender, RoutedEventArgs e) 
    { 
     loading.IsActive = false; 
    } 

    private void cover_Loaded(object sender, RoutedEventArgs e) 
    { 
     loading.IsActive = false; 
    } 

    private void cover_Loading(FrameworkElement sender, object args) 
    { 
     loading.IsActive = true; 
    } 

    private void backButton_Click(object sender, RoutedEventArgs e) 
    { 
     this.Frame.GoBack(); 
    } 
} 

}

傢俱類:

class Furniture 
    { 
     public string ID { get; set; } 

     public string Title { get; set; } 

     public string Gambar { get; set; } 

     public string Deskripsi { get; set; } 
    } 

通過使用上面的代碼不工作,並顯示一個錯誤消息: error message

如何處理呢? 注: ITEM_ID從數據ID採取已經被用戶選擇的項目上頁,

+1

您正在創建一個新的對象'detail'和訪問它的ID'detail.ID'這是空。這就是爲什麼你得到'ArgumentNullException'。 – Prajwal

+0

你能解決我的代碼嗎?如何從方法StoreDetail(int id)獲取id? – Rose

+0

這個傢俱類是做什麼的? – Joseph

回答

0

在RefreshBtn_Click,你創建一個新的按鈕,但是這不會給詳細ID價值,因爲它是一個新的(它不會得到/接收任何東西)。 雖然這也讓我想知道你爲什麼要做傢俱課,打電話給一個方法,並且製作另一個傢俱班,並給它同樣的'細節'名稱。

嘗試使其中的細節ID被宣佈爲傢俱類。 這可能最終發生在兩種方法之外,在開始時聲明它。

+0

在方法StoreDetail(int id)中還包含Furniture類。我如何在refreshBtn上聲明id?我給我的職位 – Rose

+0

這取決於你如何使用它的全部代碼,但我會給出一個參數的 – Steven

+0

對不起,不小心按。但是,當我做出來的時候,我會給課堂上一個參數。 ,如:「傢俱f =新傢俱(int 1)」。然後在類本身中,它將把參數放到變量中。 – Steven

相關問題