简体   繁体   中英

Back Button on first page when NavigationPage used

I have a custom listview on my Xamarin.Forms apps which has switch control for multi selection. Now I have this issue where in I get backbutton on the first page of my app. The back Button on the first page is not required as it redirects to a black page. I know it has to do something with Navigation.PushAsync. But I could not figure it out where the change is required. If anyone can direct me in correct way wil be helpful.

Here is SelectMultipleBasePage.cs page:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
//using System.Reflection.Emit;
using System.Text;
using Xamarin.Forms;

namespace _____
{
    public class SelectMultipleBasePage<T> : ContentPage
    {
        public class WrappedSelection<T> : INotifyPropertyChanged
        {
            public T Item { get; set; }
            bool isSelected = false;
            public bool IsSelected
            {
                get
                {
                    return isSelected;
                }
                set
                {
                    if (isSelected != value)
                    {
                        isSelected = value;
                        PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
                        //                      PropertyChanged (this, new PropertyChangedEventArgs (nameof (IsSelected))); // C# 6
                    }
                }
            }
            public event PropertyChangedEventHandler PropertyChanged = delegate { };
        }
        public class WrappedItemSelectionTemplate : ViewCell
        {
            public WrappedItemSelectionTemplate()
                : base()
            {

                Grid objGrid = new Grid();
                objGrid.BackgroundColor = Color.Gray;
                objGrid.RowDefinitions.Add(new RowDefinition
                {
                    Height = new GridLength(1, GridUnitType.Star)
                });

                objGrid.ColumnDefinitions.Add(new ColumnDefinition
                {
                    Width = new GridLength(75, GridUnitType.Absolute)
                });
                objGrid.ColumnDefinitions.Add(new ColumnDefinition
                {
                    Width = new GridLength(1, GridUnitType.Star)
                });
                objGrid.ColumnDefinitions.Add(new ColumnDefinition
                {
                    Width = GridLength.Auto
                });

                //
                // Column 1:-
                Image objImage = new Image();
                objImage.BackgroundColor = Color.Green;
                objImage.SetBinding(Image.SourceProperty, new Binding("Item.Image"));
                objGrid.Children.Add(objImage, 0, 0);

                //
                // Column 2:-
                StackLayout objStackLayoutCol2 = new StackLayout();
                objGrid.Children.Add(objStackLayoutCol2, 1, 0);

                Label name = new Label()
                {
                    Text = "Name"
                };
                Label date = new Label()
                {
                    Text = "Date"
                };
                name.SetBinding(Label.TextProperty, new Binding("Item.Name"));
                date.SetBinding(Label.TextProperty, new Binding("Item.Date"));
                objStackLayoutCol2.Children.Add(name);
                objStackLayoutCol2.Children.Add(date);

                //
                // Column 3:-
                Switch mainSwitch = new Switch();
                mainSwitch.SetBinding(Switch.IsToggledProperty, new Binding("IsSelected"));
                objGrid.Children.Add(mainSwitch, 2, 0);

                View = objGrid;


            }
        }
        public List<WrappedSelection<T>> WrappedItems = new List<WrappedSelection<T>>();
        public SelectMultipleBasePage(List<T> items)
        {
            WrappedItems = items.Select(item => new WrappedSelection<T>() { Item = item, IsSelected = false }).ToList();
            ListView mainList = new ListView()
            {
                ItemsSource = WrappedItems,
                ItemTemplate = new DataTemplate(typeof(WrappedItemSelectionTemplate)),
            };

            mainList.ItemSelected += (sender, e) =>
            {
                if (e.SelectedItem == null) return;
                var o = (WrappedSelection<T>)e.SelectedItem;
                o.IsSelected = !o.IsSelected;
                ((ListView)sender).SelectedItem = null; //de-select
            };
            Content = mainList;

            if (Device.OS == TargetPlatform.WinPhone)
            {   // fix issue where rows are badly sized (as tall as the screen) on WinPhone8.1
                mainList.RowHeight = 40;
                // also need icons for Windows app bar (other platforms can just use text)
                ToolbarItems.Add(new ToolbarItem("All", "check.png", SelectAll, ToolbarItemOrder.Primary));
                ToolbarItems.Add(new ToolbarItem("None", "cancel.png", SelectNone, ToolbarItemOrder.Primary));
            }
            else
            {
                ToolbarItems.Add(new ToolbarItem("All", null, SelectAll, ToolbarItemOrder.Primary));
                ToolbarItems.Add(new ToolbarItem("None", null, SelectNone, ToolbarItemOrder.Primary));
            }
        }
        void SelectAll()
        {
            foreach (var wi in WrappedItems)
            {
                wi.IsSelected = true;
            }
        }
        void SelectNone()
        {
            foreach (var wi in WrappedItems)
            {
                wi.IsSelected = false;
            }
        }
        public List<T> GetSelection()
        {
            return WrappedItems.Where(item => item.IsSelected).Select(wrappedItem => wrappedItem.Item).ToList();
        }
    }
}

Listpage.cs:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
//using System.Reflection.Emit;
using System.Text;
using System.Xml.Serialization;
using Xamarin.Forms;

namespace ______
{
    public class ListPage : ContentPage
    { 
        SelectMultipleBasePage<CheckItem> multiPage= null;
        public ListPage ()
        {
            loadlist();
        }

        async void loadlist()
        {
            var items = new List<CheckItem>();
            items.Add(new CheckItem { Name = "Xamarin.com", Date = "01/01/2015", Image = "img.png" });
            items.Add(new CheckItem { Name = "Twitter", Date = "01/01/2015", Image = "img.png" });
            items.Add(new CheckItem { Name = "Facebook", Date = "01/01/2015", Image = "img.png" });

            if (multiPage == null)
                multiPage = new SelectMultipleBasePage<CheckItem>(items) { Title = "Check all that apply" };

            await Navigation.PushAsync(multiPage);
        }
    }
}

App.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Xamarin.Forms;

namespace ____
{
    public class App : Application
    {
        public App ()
        {
            MainPage = new NavigationPage(new ListPage());
        }

        protected override void OnStart ()
        {
            // Handle when your app starts
        }

        protected override void OnSleep ()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume ()
        {
            // Handle when your app resumes
        }
    }
}

Instead of doing await Navigation.PushAsync(multiPage); change this to:

Content = multiPage;

This should set the page content for the ListPage you have passed to the NavigationPage to be the SelectMultipleBasePage<T> you setup in code. And this should be the first page in the Navigation and there shouldn't be a back button present.

EDIT: Sorry try changing it to: Content = multiPage.Content;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM