xaml - ui没有实时更新

标签 xaml mvvm xamarin.forms observablecollection listviewitem

我有一个 ListView ,用于显示我从 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。这是我的 View 模型
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;
            }
        }

我怀疑两件事
  • CachingStrategy="RecycleElement"我删除了它不使用滚动更新的这个
  • INotifyPropertyChanged 有任何问题,我试图从列表中删除一个项目,它的工作和更新 ui.我想更新项目内的字段
    谁能帮帮我
  • 最佳答案

    从评论中推断,您还需要实现 INotifyPropertyChanged Job上的界面目的。使用 ObservableCollection仅有助于该集合中的更改,因此当您删除或添加 Job 时对象,如果 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));
        }
    }
    

    您可以删除 INotifyPropertyChanged来自您的JobViewModel ,但你可能在某个时候也需要它

    关于xaml - ui没有实时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54361148/

    相关文章:

    c# - 有什么方法可以使用 Wpf (C#) 更改 Web 浏览器的上下文菜单

    visual-studio-2010 - XAML 不反射(reflect)更改

    c# - 有没有一种方法可以仅使用XAML来基于“自身文本”更改Textblock前景?

    c# - Xamarin 的优点和缺点 Vis a Vis 其他框架

    mvvm - 为什么 Xamarin.Forms 中的 MvvmLight 优于 Mvvm?

    c# - 选择后 ContextMenu 不会消失。

    WPF 缩放+滚动条?

    wpf - LIstView键盘导航,例如WPF中的Windows资源管理器

    android - MVVM : Fragment overlapping issue in view pager with navigation view?

    Azure 视频流传输到 Xamarin