更新Xamarin表单iOS中的单元格背景

时间:2017-12-04 10:39:35

标签: ios xamarin

在用xamarin形式更新细胞的背景颜色的时候有没有人遇到烦恼的问题? 我有通知列表,当用户点击项目时,它被标记为已读,则背景颜色将被更新。 它适用于Android但iOS。 大多数情况下,颜色会变回原点。 在示例中,如果读取通知,则单元格背景为蓝色。 当用户点击单元格时,单元格背景应为红色,但大部分时间都不会更改。

通知模型

 public class Notification : ObservableObject
{
    private string _message;
    private bool _isRead;

    public string Message
    {
        get => _message;
        set => SetProperty(ref _message, value);
    }

    public bool IsRead
    {
        get => _isRead;
        set => SetProperty(ref _isRead, value);
    }
}

视图模型

public class MainPageViewModel : BaseViewModel
{
    private ObservableCollection<Notification> _notifications;

    public ObservableCollection<Notification> Notifications
    {
        get => _notifications;
        set => SetProperty(ref _notifications, value);
    }

    public ICommand TappedCommand => new Command((o => OnTapped(o)));

    public MainPageViewModel()
    {
        _notifications = new ObservableCollection<Notification>()
        {
            new Notification()
            {
                IsRead = false,
                Message = "First notification"
            },
            new Notification()
            {
                IsRead = false,
                Message = "Second notification"
            },
            new Notification()
            {
                IsRead = false,
                Message = "Third notification"
            },
            new Notification()
            {
                IsRead = false,
                Message = "Fourth notification"
            }
        };
    }

    private void OnTapped(object o)
    {
        if(!(o is Notification noti)) return;

        noti.IsRead = true;
    }
}

页:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:NotificationTest"
         x:Class="NotificationTest.MainPage">
<ContentPage.Content>
    <ListView x:Name="NotiList"  ItemsSource="{Binding Notifications}" ItemTapped="OnTapped" ItemSelected="OnSelected" CachingStrategy="RecycleElement">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>                        
                <Grid BackgroundColor="{Binding IsRead,Converter={StaticResource ReadToColorConverter}}">
                    <Label Text="{Binding Message}"/>
                </Grid>                         
               </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>

页面代码背后(只是在listview中隐藏选定的线条颜色

 public partial class MainPage : ContentPage
{
    private MainPageViewModel _vm;
    public MainPage()
    {
        InitializeComponent();
        _vm = new MainPageViewModel();
        BindingContext = _vm;
    }

    private void OnTapped(object sender, ItemTappedEventArgs e)
    {
        _vm.TappedCommand.Execute(e.Item);
    }

    private void OnSelected(object sender, SelectedItemChangedEventArgs e)
    {
        NotiList.SelectedItem = null;
    }
}

1 个答案:

答案 0 :(得分:0)

问题是由iOS中选择单元格时的默认行为引起的。

默认选择的颜色将覆盖红色,因此它无法正常工作。

请参阅我最近的回答Here

测试

enter image description here