How can I make a > in a cell with Xamarin.Forms?

后端 未结 2 1429
挽巷
挽巷 2021-01-11 09:41

I have an application where I can change the order and the way cards appear. For anyone who has iOS I need something very similar to the way the Settings > Contacts > Sort

2条回答
  •  执念已碎
    2021-01-11 10:00

    EDIT 1: Simplified property changed logic in iOS renderer; now there are no references or handlers to cleanup.

    In extension to @hankide's answer:

    You can create a bindable property IsChecked while extending a TextCell or ViewCell and bind your VM state to it.

    public class MyTextCell : TextCell
    {
        public static readonly BindableProperty IsCheckedProperty =
            BindableProperty.Create(
                "IsChecked", typeof(bool), typeof(MyTextCell),
                defaultValue: false);
    
        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }
    }
    

    Next step would be to create renderer that listens to this property and shows a check-mark at iOS level.

    [assembly: ExportRenderer(typeof(MyTextCell), typeof(SampleApp.iOS.MyTextCellRenderer))]
    namespace SampleApp.iOS
    {
        public class MyTextCellRenderer : TextCellRenderer
        {
            public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
            {
                var nativeCell = base.GetCell(item, reusableCell, tv);
    
                var formsCell = item as MyTextCell;
                SetCheckmark(nativeCell, formsCell);
    
                return nativeCell;
            }
    
            protected override void HandlePropertyChanged(object sender, PropertyChangedEventArgs args)
            {
                base.HandlePropertyChanged(sender, args);
    
                System.Diagnostics.Debug.WriteLine($"HandlePropertyChanged {args.PropertyName}");
                if (args.PropertyName == MyTextCell.IsCheckedProperty.PropertyName)
                {
                    var nativeCell = sender as CellTableViewCell;
                    if (nativeCell?.Element is MyTextCell formsCell)
                        SetCheckmark(nativeCell, formsCell);
                }
            }
    
            void SetCheckmark(UITableViewCell nativeCell, MyTextCell formsCell)
            {
                if (formsCell.IsChecked)
                    nativeCell.Accessory = UITableViewCellAccessory.Checkmark;
                else
                    nativeCell.Accessory = UITableViewCellAccessory.None;
            }
        }
    }
    

    Sample usage 1

    And, sample usage would like:

    
        
            
            
        
    
    

    Sample usage 2

    You can also listen to Tapped event to ensure IsChecked property works as expected.

    For example, you bind this property to ViewModel:

    
        
            
            
        
    
    

    and handle tap event:

    public SettingViewModel[] Settings = new []{
        new SettingViewModel { Name = "First Last", IsSelected = false },
        new SettingViewModel { Name = "Last First", IsSelected = true },
    };
    
    void Handle_Tapped(object sender, System.EventArgs e)
    {
        var cell = sender as TextCell;
        if (cell == null)
            return;
    
        var selected = cell.Text;
        foreach(var setting in Settings)
        {
            if (setting.Name == selected)
                setting.IsSelected = true;
            else
                setting.IsSelected = false;
        }
    }
    

提交回复
热议问题