Xamarin DataTemplate定时刷新viewModel,iOS刷新整个页面,如何像Android一样更新进度栏?

问题描述 投票:0回答:1

未知学生名单(1-99?)每个学生卡都有一个进度条(如果适用)。定时更新。 (学生的数量可以根据数据,一天中的时间等进行更改,但不会像进度条那样多地更新)

[在Android上一切正常,每隔一段时间更新viewModel,如果有变化,进度条就会发生变化...在iOS上,它会刷新整个页面(如果您向下滚动以检查特定的学生,这会很烦人,必须继续这样做)

是否有可能在iOS上获得相同的结果?

这里是TodayPage.xaml的摘要

    <DataTemplate x:Key="runTemplate">
        <ViewCell>
            <Grid> ....
    <DataTemplate x:Key="studentTemplate">
        <ViewCell>
            <Grid> ...
                  <ProgressBar Grid.Row="0" Grid.ColumnSpan="6" x:Name="pb_TodayRun" Margin="0,0" Progress="{Binding progress}" ProgressColor="{StaticResource Secondary}" IsVisible="{Binding displayPB}"></ProgressBar>

    <toolkit:TodayDataTemplateSelector x:Key="todayDataTemplateSelector"
        RunTemplate="{StaticResource runTemplate}"
        StudentTemplate="{StaticResource studentTemplate}"
        TopTemplate="{StaticResource topTemplate}" />

某些背后的代码:

 public partial class TodayPage : ContentPage
    {
        TodayViewModel viewModel;
        private bool isShown = true;

        public TodayPage()
        {
            InitializeComponent();

            BindingContext = viewModel = AppContainer.Resolve<TodayViewModel>();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            UpdateProgress(true);
            isShown = true;

            Device.StartTimer(TimeSpan.FromSeconds(60/*viewModel.AutoRefreshTime*/), () =>
            {
                if (isShown)
                {
                    UpdateProgress(false);
                    return true;
                }
                else
                    return false;
            });
        }
        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            isShown = false;
        }

        public async void UpdateProgress(bool allowCached)
        {
            await viewModel.LoadItems(allowCached);
        }

和TodayViewModel.cs

 public class TodayViewModel : BaseViewModel , INotifyPropertyChanged
    {
        private readonly IStudentService _studentService;
        private readonly ISettingsService _settingsService;

        public ObservableCollection<TodayCell> Items { get; set; }

        public Command LoadItemsCommand { get; set; }
        public int AutoRefreshTime
        {
            get => _settingsService.AutoRefreshTimeSetting;
        }

        public TodayViewModel(IConnectionService connectionService, IDialogService dialogService, IStudentService studentService, ISettingsService settingsService)
            : base(connectionService, dialogService)
        {
            _studentService = studentService;
            _settingsService = settingsService;
            Items = new ObservableCollection<TodayCell>
            {
                new TodayCell.Top()
            };
            LoadItemsCommand = new Command(async (Object param) => await ExecuteLoadItemsCommand(param != null && param.Equals(true)));
        }

        public async Task ExecuteLoadItemsCommand(bool allowCached)
        {
            if (IsBusy) return;
            IsBusy = true;

            try
            {
                await LoadItems(allowCached);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }
c# xamarin xamarin.ios
1个回答
0
投票

您尚未显示关键代码-LoadItems方法,但是从样式中可以很容易地猜测到它为Items分配了值,并导致了不良行为。

如果您希望有新的物品出现,则应使用添加和删除方法。

由于可能不是这种情况,您应该在项目类上实现PropertyChanged,并在刷新列表时指出每个项目上都有更改。

© www.soinside.com 2019 - 2024. All rights reserved.