【发布时间】:2018-02-23 11:56:03
【问题描述】:
我是 Xamarin.Forms 的新手,我正在制作一个 Listview,每次我在数据库中插入新信息时都需要更新,到目前为止,我可以显示我的列表信息并通过 PHP 文件添加它,但是我不能让它自动刷新。
namespace Proyect
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Alarms : ContentPage
{
public Alarms ()
{
InitializeComponent();
AlarmsList.ItemTemplate = new DataTemplate(typeof(Cells.AlarmsCell)); //Template of the Alarms
this.LoadAlarms();
}
private async void LoadAlarms()
{
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("Http://192.168.0.13");
string url = string.Format("/Proyect/alarmscode.php?");
var response = await client.GetAsync(url);
var result = response.Content.ReadAsStringAsync().Result;
var jsonalarms = JsonConvert.DeserializeObject<ObservableCollection<GetAlarms>>(result);
AlarmsList.ItemsSource = jsonalarms;
}
catch (Exception e)
{
await DisplayAlert("ERROR", e + "", "OK");
return;
}
}
}
}
【问题讨论】:
-
您是否使用计时器或某种计划任务来定期从您的服务器请求更新信息?
-
如果你会使用 MVVM 会很容易 -> 只需定期更新警报属性。
-
我知道如果我使用 ObservableCollection
作为我的 ItemSource 列表视图将在添加或从中删除项目时自动更新,我认为这就是我正在做的事情。它没有工作
标签: xamarin.forms