【问题标题】:xamarin forms listview auto refreshxamarin 表单列表视图自动刷新
【发布时间】: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


【解决方案1】:

您能否尝试保持相同的 ObservableCollection 并更新其内容,而不是每次都设置一个新的 ObservableCollection?

namespace Proyect
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Alarms : ContentPage
{
    private ObservableCollection<GetAlarms> _itemsSource = null;

    public Alarms()
    {
        InitializeComponent();
        AlarmsList.ItemTemplate = new DataTemplate(typeof(Cells.AlarmsCell));   //Template of the Alarms

        _itemsSource = new ObservableCollection<GetAlarms>();
        AlarmsList.ItemsSource = _itemsSource;

        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);

            _itemsSource.Clear();
            foreach (var alarm in jsonalarms)
            {
                _itemsSource.Add(alarm);
            }
        }
        catch (Exception e)
        {
            await DisplayAlert("ERROR", e + "", "OK");
            return;
        }
    }
}
}

【讨论】:

    【解决方案2】:

    Device.StartTimer (new TimeSpan (0, 0, 10), () => { // 每 10 秒执行一次操作

            return true; // runs again, or false to stop
        });
    

    【讨论】:

    • 嘿,谢谢。我试过了,它可以工作,但它并不是我想要的。添加新项目时是否可以只刷新列表视图?
    猜你喜欢
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 2018-07-27
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多