2017-04-21 86 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(); 
    } 
}