0

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

4

3 回答 3

5

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

于 2017-04-21T16:45:30.200 回答
0

公共类 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();
    }
}
于 2017-04-21T19:24:48.567 回答
0

启动定时器

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

            return true; // runs again, or false to stop
        });
于 2017-04-21T16:54:07.643 回答