【问题标题】:How to refresh ListView in ContentPage in xamarin forms periodically如何定期刷新xamarin表单中ContentPage中的ListView
【发布时间】:2017-04-23 00:24:08
【问题描述】:

我在内容页面中有一个列表视图,列表视图项目是从 SQLite 中挑选出来的。我想定期刷新页面,以便能够显示插入到 sql lite 中的最新项目。 1.当我第一次在本地数据库中添加该记录的记录状态为“排队”时,将显示List Item并且该项目的状态将显示为“[EmployeeNo]它已排队5​​分钟后它将被同步” . 2.5分钟后,所有本地数据库[Sqlite]将与实际的sql服务器同步,然后该记录的状态将在本地数据库中更新为“完成”,然后状态我想显示“[EmployeeNo]它已完成" 自动在列表视图中。

【问题讨论】:

    标签: xamarin xamarin.forms content-pages


    【解决方案1】:

    使用ObservableCollection<T> 作为您的 ItemSource - 每当添加或删除项目时,它都会自动更新 UI

    【讨论】:

    • 你好杰森,谢谢你的回答。我尝试使用 ObservableCollection。它奏效了。
    【解决方案2】:

    StartTimer

            Device.StartTimer (new TimeSpan (0, 0, 10), () => {
                // do something every 10 seconds
    
                return true; // runs again, or false to stop
            });
    

    【讨论】:

    • 感谢您的回复,我按照网址stackoverflow.com/questions/41574642/…实现了相同的操作
    • 我是新手,我需要将此代码放在活动或我的适配器中吗?
    • 活动?这是 xamarin 形式。您可以将其添加到您的页面构造函数
    【解决方案3】:

    公共类 EmployeeListPage : ContentPage {

        ListView listView;
    
        public EmployeeListPage()
        {
            Title = "Todo";
            StartTimer();
            var toolbarItem = new ToolbarItem
            {
                Text = "+",
                Icon = Device.OnPlatform(null, "plus.png", "plus.png")
            };
            toolbarItem.Clicked += async (sender, e) =>
            {
                await Navigation.PushAsync(new EmployeeItemPage
                {
                    BindingContext = new Employee()
                });
            };
            ToolbarItems.Add(toolbarItem);
    
            listView = new ListView
            {
                Margin = new Thickness(20),
                ItemTemplate = new DataTemplate(() =>
                {
                    var label = new Label
                    {
                        VerticalTextAlignment = TextAlignment.Center,
                        HorizontalOptions = LayoutOptions.StartAndExpand
                    };
                    label.SetBinding(Label.TextProperty, "EmployeeName");
    
                    var labelText = new Label
                    {
                        VerticalTextAlignment = TextAlignment.Center,
                        HorizontalOptions = LayoutOptions.StartAndExpand
                    };
                    label.SetBinding(Label.TextProperty, "EmpStatusDisplayText");
    
                    var tick = new Image
                    {
                        Source = ImageSource.FromFile("check.png"),
                        HorizontalOptions = LayoutOptions.End
                    };
                    tick.SetBinding(VisualElement.IsVisibleProperty, "IsActive");
    
                    var stackLayout = new StackLayout
                    {
                        Margin = new Thickness(20, 0, 0, 0),
                        Orientation = StackOrientation.Horizontal,
                        HorizontalOptions = LayoutOptions.FillAndExpand,
                        Children = { label, tick }
                    };
    
                    return new ViewCell { View = stackLayout };
                })
            };
            listView.ItemSelected += async (sender, e) =>
            {
                EmployeeDatabindingDto dto = (e.SelectedItem as EmployeeDatabindingDto);
                Employee emp = new Employee {EmployeeID=dto.EmployeeID,EmployeeName=dto.EmployeeName,Salary=dto.Salary,IsActive=dto.IsActive };
                Debug.WriteLine("Employee ResumeAt Id = " + emp.EmployeeID);
    
                await Navigation.PushAsync(new EmployeeItemPage
                {
                    BindingContext = emp
                });
            };
    
            Content = listView;
        }
    
        protected override async void OnAppearing()
        {
            base.OnAppearing();
            List<Employee> employees = await App.EmpDatabase.GetItemsAsync();
            List<EmployeeDatabindingDto> listBindingDto = await MapEmpWithEmpBindingDto(employees);
            listView.ItemsSource = listBindingDto;
        }
    
        public async Task<List<EmployeeDatabindingDto>> MapEmpWithEmpBindingDto(List<Employee> employees)
        {
            List<EmployeeDatabindingDto> bindEmployees = new List<EmployeeDatabindingDto>();
    
            foreach (var employee in employees)
            {
                string displaysText = "";
                string displayDate = "";
    
                displayDate = employee.IsActive == false ? employee.Createddate.ToString() : employee.Modifieddate.ToString();
                displaysText = employee.IsActive == false ? string.Format("{0} {1}", "is in queued on", displayDate) : string.Format("{0} {1} ", "is submitted on", displayDate);
                bindEmployees.Add(new EmployeeDatabindingDto
                {
                    EmployeeID = employee.EmployeeID
                    ,
                    EmployeeName = employee.EmployeeName
                    ,
                    Salary = employee.Salary
                    ,
                    Createddate = employee.Createddate
                    ,
                    IsActive = employee.IsActive
                    ,
                    EmpStatusDisplayText = string.Format("{0} {1}", employee.EmployeeName, displaysText)
                });
            }
            return bindEmployees;
        }
    
        private void StartTimer()
        {
            Device.StartTimer(System.TimeSpan.FromSeconds(10), () =>
            {
                List<Employee> employees = App.EmpDatabase.GetItemsAsync().Result;
                List<EmployeeDatabindingDto> listBindingDto = MapEmpWithEmpBindingDto(employees).Result;
                listView.ItemsSource = listBindingDto;
                Device.BeginInvokeOnMainThread(UpdateUserDataAsync);
                return true;
            });
        }
    
        private async void UpdateUserDataAsync()
        {
            await App.EmpDatabase.UpdateEmpStatusAsync();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 2018-02-07
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多