Xamarin Forms UWP listview在第二次创建页面时不起作用

时间:2019-04-04 16:41:16

标签: listview xamarin uwp

我正在使用Xamarin Forms UWP应用程序,并且有一个页面可以加载机会列表视图(我自己的模型)。当我第一次尝试访问listview时,它工作正常。我可以在列表视图中单击一个项目,它会加载另一个页面,其中包含与该列表视图中的特定机会项目有关的更多详细信息。然后,我可以单击“后退”按钮,导航到包含列表视图的页面,然后选择另一个项目,并导航到显示有关该特定机会项目的更多详细信息的页面。

当我单击一个完全不同的页面然后尝试导航回加载“机会”列表视图的页面时,就会出现问题。列表视图将正确加载,但是当我尝试单击其中一项时,它不会导航到显示与该项目有关的更多详细信息的页面,这意味着itemtapped / ItemSelected由于某种原因不会再次触发。

我正在使用PRISM,并且还利用了Prism的事件来指挥行为。然后,我切换并最终创建了自己的事件来命令行为,并在OnDetachingFrom()方法中将Listview.SelectedItem设置为null,但仍然看到相同的结果。

我还尝试在后面的代码中将SelectedItem设置为null。还是没用。

在Android版本中,一切正常。这里可能是什么问题? 非常感谢您的帮助。我是一个初学者,这是我的第一个Forms应用程序。

<!--My OpportunitiesPage View-->
<ScrollView>
        <ListView x:Name="OpportunitiesList" CachingStrategy="RecycleElement" BackgroundColor="Transparent" RowHeight="200" ItemsSource="{Binding OpportunitiesList, Mode=TwoWay}" SeparatorVisibility="None" SelectedItem="{Binding SelectedOpportunity, Mode=TwoWay}">
            <ListView.Behaviors>
                <Local:ItemSelectedToCommandBehaviour Command="{Binding SelectOpportunityCommand}"/>
            </ListView.Behaviors>
            <!--ViewCell stuff goes here-->
        </ListView>
</ScrollView>
//My ViewModel for the Opportunities Page
    public class OpportunitiesPageViewModel : BindableBase, INavigationAware
    {
        INavigationService _navigationService;

        private DelegateCommand<Opportunities> _selectedOpportunityCommand;
        public DelegateCommand<Opportunities> SelectedOpportunityCommand => _selectedOpportunityCommand ?? (_selectedOpportunityCommand = new DelegateCommand<Opportunities>(async (EventArgs)=> await ShowOpportunityDetails(EventArgs)));

        public ICommand SelectOpportunityCommand { get; private set; }

        private Opportunities _selectedOpportunity;
        public Opportunities SelectedOpportunity { get { return _selectedOpportunity; } set => SetProperty(ref _selectedOpportunity, value); }

        IList<Opportunities> _listCopy;
        public IList<Opportunities> ListCopy { get { return _listCopy; } set => SetProperty(ref _listCopy, value); }

        IList<Opportunities> _opportunitiesList;
        public IList<Opportunities> OpportunitiesList { get { return _opportunitiesList; } set => SetProperty(ref _opportunitiesList, value); }

        Opportunities _opportunity1;
        Opportunities _opportunity2;
        Opportunities _opportunity3;
        Opportunities _opportunity4;
        Opportunities _opportunity5;

        public Opportunities Opportunity1 { get { return _opportunity1; } set => SetProperty(ref _opportunity1, value); }
        public Opportunities Opportunity2 { get { return _opportunity2; } set => SetProperty(ref _opportunity2, value); }
        public Opportunities Opportunity3 { get { return _opportunity3; } set => SetProperty(ref _opportunity3, value); }
        public Opportunities Opportunity4 { get { return _opportunity4; } set => SetProperty(ref _opportunity4, value); }
        public Opportunities Opportunity5 { get { return _opportunity5; } set => SetProperty(ref _opportunity5, value); }

        public OpportunitiesPageViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;

            OpportunitiesList = new ObservableCollection<Opportunities>
            {
                new Opportunities
                {
                    Id = 0,
                HeaderImage = "https://images.unsplash.com/photo-1462899006636-339e08d1844e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80",
                Author = "Justin Pillay",
                DatePublished = DateTime.Now.ToShortDateString(),
                Title = "Funding for SMMEs",
                Body = "Lorem ipsum dolor sit amet,consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                Category= "Funding",
                Organisation = "Yobuningi (Pty) Ltd",
                ContactWebsite = "www.yobuningi.co.za",
                ContactEmail = "info@yobuningi.co.za",
                ContactNumber1 = "0111234567",
                ContactNumber2 = "0111234567",
                ContactPerson = null
                },

                new Opportunities
                {
                    Id = 0,
                HeaderImage = "https://images.unsplash.com/photo-1444653614773-995cb1ef9efa?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1355&q=80",
                Author = "Justin Pillay",
                DatePublished = DateTime.Now.ToShortDateString(),
                Title = "Growth for SMMEs",
                Body = "Lorem ipsum dolor sit amet,consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                Category = "Business Support",
                Organisation = "Yobuningi (Pty) Ltd",
                ContactWebsite = "www.yobuningi.co.za",
                ContactEmail = "info@yobuningi.co.za",
                ContactNumber1 = "0111234567",
                ContactNumber2 = "0111234567",
                ContactPerson = null
                },

                new Opportunities
                {
                     Id = 0,
                HeaderImage = "https://images.unsplash.com/photo-1507679799987-c73779587ccf?ixlib=rb-1.2.1&auto=format&fit=crop&w=1351&q=80",
                Author = "Justin Pillay",
                DatePublished = DateTime.Now.ToShortDateString(),
                Title = "Business Support for SMMEs",
                Body = "Lorem ipsum dolor sit amet,consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                Category = "Business Support",
                Organisation = "Yobuningi (Pty) Ltd",
                ContactWebsite = "www.yobuningi.co.za",
                ContactEmail = "info@yobuningi.co.za",
                ContactNumber1 = "0111234567",
                ContactNumber2 = "0111234567",
                ContactPerson = null
                },

                new Opportunities
                {
                    Id = 0,
                HeaderImage = "https://images.unsplash.com/photo-1517245386807-bb43f82c33c4?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80",
                Author = "Justin Pillay",
                DatePublished = DateTime.Now.ToString(),
                Title = "New Incubation Hub to open in Johannesburg",
                Body = "Lorem ipsum dolor sit amet,consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                Category = "Business Support",
                Organisation = "Yobuningi (Pty) Ltd",
                ContactWebsite = "www.yobuningi.co.za",
                ContactEmail = "info@yobuningi.co.za",
                ContactNumber1 = "0111234567",
                ContactNumber2 = "0111234567",
                ContactPerson = null
                },

                new Opportunities
                {
                     Id = 0,
                HeaderImage = "https://images.unsplash.com/photo-1545239249-27f6fa8b6b4a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=600&q=60",
                Author = "Justin Pillay",
                DatePublished = DateTime.Now.ToShortDateString(),
                Title = "Aunty Sameema gonna soot you!",
                Body = "Lorem ipsum dolor sit amet,consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                Category = "Networking",
               Organisation = "Yobuningi (Pty) Ltd",
                ContactWebsite = "www.yobuningi.co.za",
                ContactEmail = "info@yobuningi.co.za",
                ContactNumber1 = "0111234567",
                ContactNumber2 = "0111234567",
                ContactPerson = null
                }
            };

            SelectOpportunityCommand = new Command(async () => { await ShowOpportunityDetails(SelectedOpportunity); });


        }



        private async Task ShowOpportunityDetails(Opportunities opportunity)
        {
            var p = new NavigationParameters();
            p.Add("SelectedOpportunity", opportunity);

            await _navigationService.NavigateAsync("ViewOpportunityPage", p);




        }
//My Event to Command Behaviour

public class ItemSelectedToCommandBehaviour : Behavior<ListView>
    {
        public static readonly BindableProperty CommandProperty = BindableProperty.Create(propertyName:"Command", returnType:typeof(ICommand), declaringType:typeof(ItemSelectedToCommandBehaviour));

        public ICommand Command
        {
            get
            {
                return (ICommand)GetValue(CommandProperty);
            }
            set
            {
                SetValue(CommandProperty, value);
            }
        }

        protected override void OnAttachedTo(ListView bindable)
        {
            base.OnAttachedTo(bindable);

            bindable.ItemSelected += BindableOnItemSelected;



            bindable.BindingContextChanged += Bindable_BindingContextChanged;
        }

        private void Bindable_BindingContextChanged(object sender, EventArgs e)
        {
            var lv = sender as ListView;
            BindingContext = lv?.BindingContext;
        }

        protected override void OnDetachingFrom(ListView bindable)
        {
            base.OnDetachingFrom(bindable);

            bindable.SelectedItem = null;
            bindable.ItemSelected -= BindableOnItemSelected;
            bindable.BindingContextChanged -= Bindable_BindingContextChanged;

        }

        private void BindableOnItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            Command.Execute(null);
        }
    }

1 个答案:

答案 0 :(得分:0)

发生这种情况的原因是因为您有两个嵌套的滚动容器,这是一种不好的做法。

由于ListView具有其自己的滚动,因此不需要滚动视图,因此它实际上应如下所示:

    <!--My OpportunitiesPage View-->
    <ListView x:Name="OpportunitiesList" CachingStrategy="RecycleElement" BackgroundColor="Transparent" RowHeight="200" ItemsSource="{Binding OpportunitiesList, Mode=TwoWay}" SeparatorVisibility="None" SelectedItem="{Binding SelectedOpportunity, Mode=TwoWay}">
        <ListView.Behaviors>
            <Local:ItemSelectedToCommandBehaviour Command="{Binding SelectOpportunityCommand}"/>
        </ListView.Behaviors>
        <!--ViewCell stuff goes here-->
    </ListView>