使用NavigationPage时第一页上的后退按钮

时间:2016-02-08 07:39:13

标签: c# xamarin.forms

我的Xamarin.Forms应用程序上有自定义列表视图,它具有用于多选的开关控制。现在我有这个问题,我在我的应用程序的第一页上获得了后退按钮。第一页上的后退按钮不是必需的,因为它重定向到黑页。我知道它必须使用Navigation.PushAsync做一些事情。但我无法弄清楚需要改变的地方。如果有人能以正确的方式指导我将会有所帮助。

这是SelectMultipleBasePage.cs页面:

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

1 个答案:

答案 0 :(得分:1)

而不是await Navigation.PushAsync(multiPage);将其更改为:

Content = multiPage;

这应该将您传递给NavigationPage的ListPage的页面内容设置为您在代码中设置的SelectMultipleBasePage<T>。这应该是导航中的第一页,并且不应该出现后退按钮。

编辑: 抱歉,请尝试将其更改为:Content = multiPage.Content;