使用NavigationPage时首页上的“后退"按钮 [英] Back Button on first page when NavigationPage used

查看:84
本文介绍了使用NavigationPage时首页上的“后退"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是SelectMultipleBasePage.cs页面:

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:

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

推荐答案

代替执行await Navigation.PushAsync(multiPage);,将其更改为:

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

Content = multiPage;

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

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.

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

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

这篇关于使用NavigationPage时首页上的“后退"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆