Xaml Ui未实时更新
我有一个listview,用于显示我从api获得的所有项目。我有一个live事件,它将修改observablecollection类型项目中的一个字段Xaml Ui未实时更新,xaml,mvvm,xamarin.forms,observablecollection,listviewitem,Xaml,Mvvm,Xamarin.forms,Observablecollection,Listviewitem,我有一个listview,用于显示我从api获得的所有项目。我有一个live事件,它将修改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
<?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;
}
}
公共类JobViewModel:INotifyPropertyChanged
{
私人可观测收集项目;
公共事件属性更改事件处理程序属性更改;
公共可观测收集项目
{
获取{返回项;}
设置
{
项目=价值;
如果(项目!=值)
{
项目=价值;
OnPropertyChanged(项目名称);
}
}
}
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
私有无效更新义务事件(NotificationEntity消息)
{
var jobId=message.jobId;
Job Job=Items.FirstOrDefault(a=>a.JobId.Equals(JobId));
开关(message.EventCode)
{
case MessageTypeEnum.Pulse://Pulse
job.GoodCount=job.GoodCount+1;
//job.Status='Running';
打破
违约:
打破
}
}
我怀疑两件事
根据注释推断,您还需要在
作业
对象上实现INotifyPropertyChanged
接口。使用observateCollection
仅对该集合中的更改有帮助,因此当您删除或添加作业
对象时,如果作业
对象内部发生更改,则不起作用
因此,在您的作业中
对象中执行以下操作(代码根据您发布的内容进行反向工程):
您可以从
JobViewModel
中删除INotifyPropertyChanged
,但您可能在某个时候也需要它从注释中推断,您还需要在作业
对象上实现INotifyPropertyChanged
接口。使用observateCollection
仅对该集合中的更改有帮助,因此当您删除或添加作业
对象时,如果作业
对象内部发生更改,则不起作用
因此,在您的作业中
对象中执行以下操作(代码根据您发布的内容进行反向工程):
您可以从您的
作业视图模型中删除INotifyPropertyChanged
,但是你可能在某个时候也需要它你用来更新数据的代码在哪里?@Gerald Versluis抱歉。我用该代码更新了这个问题,所以你需要在作业
对象@Gerald Versluis上实现INotifyPropertyChanged
接口。如果是这样,我已经将项声明为ObservableCollection不正确您能否指导我如何进行此操作?此操作仅在集合中发生更改时更新UI,而不是该集合中的对象。我将添加它作为答案。您用于更新数据的代码在哪里?@Gerald Versluis抱歉。我也用该代码更新了问题,因此您需要在作业
对象@Gerald Versluis上实现InotifyProperty更改
接口。我已将项声明为可观察集合如果这不正确,您能指导我吗使其正常工作此操作仅在
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));
}
}