Ui 没有实时更新

Ui is not updating on realtime

我有一个列表视图,用于显示我从 api.i 获得的所有项目有一个实时事件,它将修改 observablecollection 类型的项目中的一个字段

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Dyocense.Views.ManageJobs"
             Title="All jobs">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="ADD" Clicked="AddJob"></ToolbarItem>
    </ContentPage.ToolbarItems>
    <ContentPage.Content>
        <StackLayout>
            <SearchBar x:Name="Search" SearchButtonPressed="SearchBar_SearchButtonPressed"></SearchBar>
            <ListView x:Name="JobsListView"   
             ItemsSource="{Binding Items}"
             VerticalOptions="FillAndExpand"
             HasUnevenRows="true"
             CachingStrategy="RecycleElement"
             ItemAppearing="BrowseJobList_ItemAppearing"
             IsPullToRefreshEnabled="true"
             >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Padding="10">
                                <Frame HasShadow="True" >
                                    <StackLayout>
                                        <Grid>
                                           <Grid.RowDefinitions>
                                                <RowDefinition Height="*" />
                                                <RowDefinition Height="*" />
                                                <RowDefinition Height="*" />
                                                <RowDefinition Height="*" />
                                                <RowDefinition Height="*" />
                                                <RowDefinition Height="*" />
                                                <RowDefinition Height="*" />
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*" />
                                                <ColumnDefinition Width="*" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>

                                        <Label Text="{Binding Name}" 
                                               Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" 
                                               FontSize="Medium"
                                               FontAttributes="Bold"/>
                                        <Label Text="Status" 
                                               Grid.Row="1" Grid.Column="0"
                                               FontSize="16"
                                               FontAttributes="Bold"/>
                                        <Label Text="{Binding Status}" 
                                               Grid.Row="1" Grid.Column="1"
                                               FontSize="16"/>
                                        <Label Text="Goal" 
                                               Grid.Row="2" Grid.Column="1"
                                               FontSize="16"
                                               FontAttributes="Bold"/>
                                        <Label Text="{Binding Goal}" 
                                               Grid.Row="2" Grid.Column="2"
                                               FontSize="16" />
                                        <Label Text="Part" 
                                               Grid.Row="3" Grid.Column="1"
                                               FontSize="16"
                                               FontAttributes="Bold"/>
                                        <Label Text="{Binding PartName}" 
                                               Grid.Row="3" Grid.Column="2"
                                               FontSize="16" />
                                        <Label Text="Assembly" 
                                               Grid.Row="4" Grid.Column="1"
                                               FontSize="16"
                                               FontAttributes="Bold"/>
                                        <Label Text="{Binding NodeName}" 
                                               Grid.Row="4" Grid.Column="2"
                                               FontSize="16" />
                                        <Label Text="GoodCount" 
                                               Grid.Row="5" Grid.Column="0"
                                               FontSize="16"
                                               FontAttributes="Bold"/>
                                        <Label Text="{Binding GoodCount}" 
                                               Grid.Row="6" Grid.Column="0"
                                               FontSize="16"
                                               HorizontalTextAlignment="Center"/>
                                        <Label Text="RejectCount" 
                                               Grid.Row="5" Grid.Column="1"
                                               FontSize="16"
                                               FontAttributes="Bold"/>
                                        <Label Text="{Binding RejectCount}" 
                                               Grid.Row="6" Grid.Column="1"
                                               FontSize="16" 
                                               HorizontalTextAlignment="Center"/>
                                        <Label Text="DownTimeCount" 
                                               Grid.Row="5" Grid.Column="2"
                                               FontSize="16"
                                               FontAttributes="Bold"/>
                                        <Label Text="{Binding DownTimeCount}" 
                                               Grid.Row="6" Grid.Column="2"
                                               FontSize="16" 
                                               HorizontalTextAlignment="Center"/>
                                        </Grid>
                                    </StackLayout>

                                </Frame>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
                <ListView.Footer>
                    <Grid Padding="6" IsVisible="{Binding IsBusy}">
                         <!--set the footer to have a zero height when invisible--> 
                        <Grid.Triggers>
                            <Trigger TargetType="Grid" Property="IsVisible" Value="False">
                                <Setter Property="HeightRequest" Value="0" />
                            </Trigger>
                        </Grid.Triggers>
                         <!--the loading content--> 
                        <Label Text="Loading..." VerticalOptions="Center" HorizontalOptions="Center" />
                    </Grid>
                </ListView.Footer>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

我想实时更新好计数。我调试了它在我的列表中更新的代码,但只更新了用户滚动上的 UI。这是我的视图模型

public class JobViewModel: INotifyPropertyChanged
    {
        private ObservableCollection<Job> items;

        public event PropertyChangedEventHandler PropertyChanged;


        public ObservableCollection<Job> Items
        {
            get { return items; }
            set
            {

                items = value;
                if (items != value)
                {
                    items = value;
                    OnPropertyChanged(nameof(Items));
                }
            }
        }
 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
}
}
private void updateJoblistOnEvent(NotificationEntity message)
        {
            var jobId = message.JobId;
            Job job = Items.FirstOrDefault(a => a.JobId.Equals(jobId));

            switch (message.EventCode)
            {


                case MessageTypeEnum.Pulse: //Pulse
                job.GoodCount = job.GoodCount + 1;

                //job.Status = 'Running';
                break;
                default:
                    break;
            }
        }

我怀疑两件事

  1. CachingStrategy="RecycleElement" 我删除了这个,它也不会随着滚动更新
  2. INotifyPropertyChanged 有任何问题,我试图从列表中删除一个项目,它正在工作并更新 ui.i 想更新项目内的字段 谁能帮帮我

从评论中推断,您还需要在 Job 对象上实现 INotifyPropertyChanged 接口。使用 ObservableCollection 仅有助于该集合中的更改,因此当您删除或添加 Job 对象时,如果 inside Job 中的某些内容发生更改则不会对象。

因此,在您的 Job 对象中执行此操作(根据您发布的代码进行逆向工程):

public class Job : INotifyPropertyChanged
{
   private int goodCount;
   public int GoodCount
   {
       get { return goodCount; }
       set
       {
           if (goodCount != value)
           {
               goodCount = value;
               OnPropertyChanged(nameof(GoodCount));
            }
        }
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

您可以从 JobViewModel 中删除 INotifyPropertyChanged,但有时您可能也需要它