使用导航页时第一页上的后退按钮
本文关键字:按钮 第一页 导航 | 更新日期: 2023-09-27 18:33:34
我的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();
}
}
}
列表页.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);
}
}
}
应用.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);
不如将其更改为:
Content = multiPage;
这应将已传递给导航页的列表页的页面内容设置为在代码中设置的SelectMultipleBasePage<T>
。这应该是导航中的第一页,不应该有后退按钮。
编辑:抱歉,请尝试将其更改为:Content = multiPage.Content;