我有一個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; }
}
如何處理呢? 注: ITEM_ID從數據ID採取已經被用戶選擇的項目上頁,
您正在創建一個新的對象'detail'和訪問它的ID'detail.ID'這是空。這就是爲什麼你得到'ArgumentNullException'。 – Prajwal
你能解決我的代碼嗎?如何從方法StoreDetail(int id)獲取id? – Rose
這個傢俱類是做什麼的? – Joseph