我有一个gridview,它使用链接http://..../mobileapp/GetPostByCategoryXMLa?term_id=(item_id)从JSON数据中获取数据 我想如果在点击"再试一次"时没有互联网连接,它可以再次刷新页面(当有连接互联网时)。 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,用于用户选择的项目
答案 0 :(得分:0)
在RefreshBtn_Click中,您已经创建了一个新按钮,但这不再提供详细ID值,因为它是一个新的(它不会获得/接收任何东西) )。 虽然,它也让我想知道你为什么要做一个家具课,称呼一个方法,并制作另一个家具类,并给它相同的细节&#39;名。
尝试制作宣称详细ID的家具类。 这最终可能在两种方法之外发生,通过在开始时声明它。