使用者介面未即时更新

时间:2019-01-25 08:00:55

标签: xaml mvvm xamarin.forms observablecollection listviewitem

我有一个列表视图,用于显示我从api获取的所有项目。我有一个实时事件,该事件将修改类型为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。我想更新该项目内的字段 谁能帮我

1 个答案:

答案 0 :(得分:1)

从注释中可以推断出,您还需要在INotifyPropertyChanged对象上实现Job接口。使用ObservableCollection仅有助于更改该集合中的内容,因此,当您删除或添加Job对象时,Job对象内部 中没有任何更改。< / p>

因此,请在您的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));
    }
}

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