2017-04-21 88 views
-1

我有一个列表视图的内容页面,列表视图项目从SQLite中挑选。我想定期刷新页面,以便能够显示插入到sql lite中的最新项目。 1.当我第一次添加该记录的记录状态在本地数据库中“排队”时,将显示列表项目,并且该项目的状态将显示为“[员工编号]它排队等待5分钟后将同步”。 2. 5分钟后,所有本地数据库[Sqlite]将与实际的sql server同步,然后该记录的状态将更新为本地数据库中的“已完成”,然后状态我想显示“[EmployeeNo]它是自动完成“列表视图中。如何刷新ListPage中的内容页在xamarin形式定期

回答

0

StartTimer

 Device.StartTimer (new TimeSpan (0, 0, 10),() => { 
      // do something every 10 seconds 

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

谢谢你的回复,我通过以下网址实现相同http://stackoverflow.com/questions/41574642/refresh-or-update-content-page-every-few-seconds-automatically –

+0

即时通讯新手,我需要把这段代码放在活动或我的适配器中? – user3373603

+0

活动?这里是xamarin表单。你可以将它添加到你的页面构造函数中 –

2

使用ObservableCollection<T>作为你的ItemSource - 它会自动更新每当项目添加或从中

+0

Jason,您好,感谢您的回答。我尝试使用ObservableCollection 。有效 。 –

0

公共类EmployeeListPage删除UI: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(); 
    } 
}