【问题标题】:Event handler ordering in XamarinXamarin 中的事件处理程序排序
【发布时间】:2016-10-15 10:32:32
【问题描述】:

我有一个在 Xamarin 表单中弹出的列表视图,如果有人在框外点击,我想隐藏它。我在处理它的列表视图的父布局上有一个点击手势识别器。在 Android 中,一切正常。如果我单击关闭,它会关闭,但如果我单击列表视图中的一个元素,它会正确选择它。在 iOS 中,情况正好相反。布局上的手势处理程序首先触发并在未正确选择项目的情况下关闭列表视图。

所以我的问题是,有没有办法改变事件触发的顺序?如果没有,是否有更好的选择来代替我试图实现这一目标的方式?谢谢!

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    如果您使用的是ListView.ItemSelectedListView.ItemTapped,那么前几天我遇到了完全相同的问题。对我来说,解决方法是不使用其中任何一个,而是将TapGestureRecognizer 附加到ViewCell 中的ListView。我还向绑定 ViewCell 的对象添加了 IsSelected 属性,以便在单击项目后更改项目的背景颜色。

    public class SomePage : ContentPage {
    
        private SomeModel _selectedModel; //It would be best to put this into your ViewModel
    
        ...
    
        public SomePage() {
    
            ListView list = new ListView {
                ItemTemplate = new DataTemplate(() => {
                    ViewCell cell = new ViewCell {
                        View = new ContentView()
                    };
    
                    cell.View.GestureRecognizers.Add(new TapGestureRecognizer {
                        Command = new Command(() => {
                            if(_selectedModel != null) { _selectedModel.IsSelected = false; }
    
                            SomeModel model = (SomeModel)cell.BindingContext;
    
                            model.IsSelected = true;
                            _selectedModel = model;
                        })
                    }
    
                    return cell;
                }
            }
        }
    }
    

    【讨论】:

    • 因此对此进行了更新,此解决方案确实有效,但是我必须先设置 cell.View,然后才能添加 GestureRecognizer。所以我只需要做类似 cell.View = new ContentView { Content = label, Padding = new Thickness(10, 0) };在我添加手势识别器之前。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    相关资源
    最近更新 更多