【问题标题】:Update cell background in Xamarin Form iOS在 Xamarin Form iOS 中更新单元格背景
【发布时间】:2017-12-07 09:33:01
【问题描述】:

在 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>

后面的页面代码(只是在列表视图中隐藏选定的线条颜色

 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;
    }
}

【问题讨论】:

  • 请直接在问题中发布代码,而不是作为链接提供。
  • 我根据您的建议更新了代码。谢谢

标签: ios xamarin


【解决方案1】:

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

默认选择的颜色会覆盖红色,所以它不起作用。

参考我最近的回答Here

测试

【讨论】:

  • 对不起,我之前附错了链接,请检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多