加载包含属性的对象列表的ListView不会像应设置的那样设置BackgroundColor

时间:2019-05-28 23:57:59

标签: listview xamarin xamarin.forms prism

很抱歉标题不正确,但我无法更好地描述我的问题。如果有人拥有更好更精确的头衔,我想知道。谢谢。

在一个应用程序中,我正在将Xamarin.Forms与Prism结合使用,在某个页面上包含一个ListView,该页面的项目中包含一些数据和一个Button。我从API加载了ObservableCollection中的MyObject,其中有一些用于定义列表项的标签和样式的属性。每次刷新或单击项目按钮之一时,都会重新加载此列表。单击按钮后,它将调用API,该API会更新一些属性,包括该项目的按钮背景色。

我的问题是,有时按钮的颜色不会在屏幕上更新。我的属性设置正确,甚至“单击”的颜色也正确,但是按钮本身的颜色却不正确。至少在乍一看时,如果滚动该项目然后再打开它,就会发生变化,即它变为正确的颜色。

我的ObservableCollection<MyObject>被竞标到ListView的ItemsSource中,并且我的Button BackgroundColor属性被设置了{Binding ButtonColor},我试图继承MyObject来自INotifyPropertyChanged,但并没有改变任何事情。我已经多次检查代码中的颜色是否正确,并且始终正确。

MyObject:

      public class MyObject
      {
            public int Id { get; set; }
            public string Value { get; set; }
            public string Status { get; set; }  
            public string BackgroundColor { get; set; }
            public string ButtonColor { get; set; }  
            public string ButtonText { get; set; }
       }

我的ListView:

      <ListView 
            VerticalOptions="FillAndExpand"
            ItemsSource="{Binding MyObjectList}"
            IsPullToRefreshEnabled="True"
            RefreshCommand="{Binding RefreshListCommand}"
            IsRefreshing="{Binding IsLoading}" 
            HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell >
                        <Grid 
                            RowSpacing="0"
                            BackgroundColor="{Binding BackgroundColor}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="3" />
                                <RowDefinition Height="20" />
                                <RowDefinition Height="3" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1.3*" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <Label 
                                Text="{Binding Value"
                                Grid.Row="2" Grid.Column="0"/>
                            <Button
                                Grid.Row="2" Grid.Column="1"
                                Text="{Binding ButtonText}"
                                BackgroundColor="{Binding ButtonColor}"
                                Clicked="Update_Clicked"/>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

我的命令(Update_Clicked执行UpdateCommand,传递所选项目的ID)

      private ObservableCollection<MyObject> _myObjectList;
      public ObservableCollection<MyObject> MyObjectList
      {
          get => _myObjectList;
          set => SetProperty(ref _myObjectList, value);
      }

        private DelegateCommand _refreshListCommand;
        public DelegateCommand RefreshListCommand=>
            _refreshListCommand?? (_refreshListCommand= new DelegateCommand(ExecuteRefreshListCommand));

      private async void ExecuteRefreshListCommand()
      {
           MyObjectList?.Clear();
           var serviceReturn = await _myService.GetObjectList();//Returns a ordered by Status (Stopped, ToBegin, Started) List of MyObject with properties depending on its status (they all are born with the ToBegin Status
           MyObjectList =  new ObservableCollection<MyObject>(serviceReturn )
      }

        private DelegateCommand<int?> _updateCommand;
        public DelegateCommand<int?> UpdateCommand=>
            _updateCommand?? (_updateCommand= new DelegateCommand<int?>(ExecuteUpdateCommand));

      private async void ExecuteUpdateCommand(int? id)
      {
           await _myService.UpdateToNextStatus(id); //Changes the object's status and its properties to the next in a cicle ToBegin -> Started -> Stopped -> Started -> ...
           ExecuteRefreshListCommand()
      }

MyObject根据其状态有3种可能的状态:

Status = "ToBegin"
BackgroundColor = "#fff"
ButtonColor = "#00ff00"
ButtonText = "Start"

Status = "Started"
BackgroundColor = "#aaa"
ButtonColor = "#ff0000"
ButtonText = "Stop"

Status = "Stopped"
BackgroundColor = "#fff"
ButtonColor = "#ff0000"
ButtonText = "Restart"

它的作用是,一切都会很好地更新,网格的背景颜色,按钮文本...但是最终经过了几轮从“开始”到“停止”和“停止到开始”状态的更改之后,列表中的ToBegin < / strong>,该列表似乎丢失了按钮应如何显示,并以错误的颜色显示,即使刷新了几次也无法正常工作。

0 个答案:

没有答案