在 Xamarin 应用程序 (C#) 中制作加载器
Make a loader in Xamarin App (C#)
我有 Android 个应用程序。
我在 .txt 文件中实现了 JSON 的缓存。
但是当我转到类别时,JSON 中的文本显示在哪里。屏幕冻结 2-3 秒并变黑。然后 Activity 打开,我看到了 JSON 的所有文本。我认为它很冷,因为我从 url 下载图像 JSON。
我可以做类似loader的东西吗?我将点击一个按钮,查看加载程序,然后 json 将从文件中读取,然后我看到 activity.
我这样下载和缓存JSON
string url2 = "http://papajohn.pp.ua/?mkapi=getProductsByCat&cat_id=83";
var json2 = await FetchAsync2(url2);
var path2 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var filename2 = System.IO.Path.Combine(path2, "cache2.txt");
File.WriteAllText(filename2, json2);
public async Task<string> FetchAsync(string url)
{
string jsonString;
using (var httpClient = new System.Net.Http.HttpClient())
{
var stream = await httpClient.GetStreamAsync(url);
StreamReader reader = new StreamReader(stream);
jsonString = reader.ReadToEnd();
}
return jsonString;
}
像这样在目标 activity 上显示 JSON
var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var filename = System.IO.Path.Combine(path, "cache4.txt");
JsonValue readJson;
var jsonString = File.ReadAllText(filename);
readJson = JsonObject.Parse(jsonString);
Console.WriteLine(jsonString);
private void ParseAndDisplay1(JsonValue readJson)
{
JsonValue firstitem = readJson[0];
productname.Text = firstitem["post_title"];
price.Text = firstitem["price"] + "грн";
weight.Text = firstitem["weight"] + "г";
var imageBitmap = GetImageBitmapFromUrl1(firstitem["img_url"]);
imagen.SetImageBitmap(imageBitmap);
}
感谢您的帮助。
您可以使用 MonoDroid.UrlImageViewHelper 从互联网异步下载图像:
var img = this.FindViewById<ImageView>(Resource.Id.SomeImageView);
img.SetUrlDrawable("http://example.com/image.png");
您可以使用 ProgressDialog
在第一个 Activity 中显示 Loaed 并获得如下所示的 json,
async Task DownLoadData ()
{
string url2 = "http://papajohn.pp.ua/?mkapi=getProductsByCat&cat_id=83";
ProgressDialog progress = new ProgressDialog (this, Resource.Style.progress_bar_style);
progress.Indeterminate = true;
progress.SetProgressStyle (ProgressDialogStyle.Spinner);
progress.SetCancelable (false);
progress.SetContentView (Resource.Layout.ProgressLayout);
var json2 = await FetchAsync2(url2);
var path2 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var filename2 = System.IO.Path.Combine(path2, "cache2.txt");
File.WriteAllText(filename2, json2);
progress.Dismiss ();
}
在第二个 Activity 中,您可以使用 UrlImageViewHelper Xamarin Component 异步加载图像。
您可以添加该 Xamarin Componentn,然后像下面这样异步加载您的图像
Koush.UrlImageViewHelper.SetUrlDrawable (imagen, firstitem["img_url"],Resource.Drawable.ic_dummyprofileimage);
其中 ic_dummyprofileimage
是一个虚拟图像,您可以在图像被异步下载之前显示它。
我有 Android 个应用程序。
我在 .txt 文件中实现了 JSON 的缓存。
但是当我转到类别时,JSON 中的文本显示在哪里。屏幕冻结 2-3 秒并变黑。然后 Activity 打开,我看到了 JSON 的所有文本。我认为它很冷,因为我从 url 下载图像 JSON。
我可以做类似loader的东西吗?我将点击一个按钮,查看加载程序,然后 json 将从文件中读取,然后我看到 activity.
我这样下载和缓存JSON
string url2 = "http://papajohn.pp.ua/?mkapi=getProductsByCat&cat_id=83";
var json2 = await FetchAsync2(url2);
var path2 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var filename2 = System.IO.Path.Combine(path2, "cache2.txt");
File.WriteAllText(filename2, json2);
public async Task<string> FetchAsync(string url)
{
string jsonString;
using (var httpClient = new System.Net.Http.HttpClient())
{
var stream = await httpClient.GetStreamAsync(url);
StreamReader reader = new StreamReader(stream);
jsonString = reader.ReadToEnd();
}
return jsonString;
}
像这样在目标 activity 上显示 JSON
var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var filename = System.IO.Path.Combine(path, "cache4.txt");
JsonValue readJson;
var jsonString = File.ReadAllText(filename);
readJson = JsonObject.Parse(jsonString);
Console.WriteLine(jsonString);
private void ParseAndDisplay1(JsonValue readJson)
{
JsonValue firstitem = readJson[0];
productname.Text = firstitem["post_title"];
price.Text = firstitem["price"] + "грн";
weight.Text = firstitem["weight"] + "г";
var imageBitmap = GetImageBitmapFromUrl1(firstitem["img_url"]);
imagen.SetImageBitmap(imageBitmap);
}
感谢您的帮助。
您可以使用 MonoDroid.UrlImageViewHelper 从互联网异步下载图像:
var img = this.FindViewById<ImageView>(Resource.Id.SomeImageView);
img.SetUrlDrawable("http://example.com/image.png");
您可以使用 ProgressDialog
在第一个 Activity 中显示 Loaed 并获得如下所示的 json,
async Task DownLoadData ()
{
string url2 = "http://papajohn.pp.ua/?mkapi=getProductsByCat&cat_id=83";
ProgressDialog progress = new ProgressDialog (this, Resource.Style.progress_bar_style);
progress.Indeterminate = true;
progress.SetProgressStyle (ProgressDialogStyle.Spinner);
progress.SetCancelable (false);
progress.SetContentView (Resource.Layout.ProgressLayout);
var json2 = await FetchAsync2(url2);
var path2 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var filename2 = System.IO.Path.Combine(path2, "cache2.txt");
File.WriteAllText(filename2, json2);
progress.Dismiss ();
}
在第二个 Activity 中,您可以使用 UrlImageViewHelper Xamarin Component 异步加载图像。
您可以添加该 Xamarin Componentn,然后像下面这样异步加载您的图像
Koush.UrlImageViewHelper.SetUrlDrawable (imagen, firstitem["img_url"],Resource.Drawable.ic_dummyprofileimage);
其中 ic_dummyprofileimage
是一个虚拟图像,您可以在图像被异步下载之前显示它。