我在Xamarin.Forms應用程序上有一個自定義列表視圖,它具有用於多選的開關控件。現在我遇到了這個問題,我在我的應用的第一頁上獲得了後退按鈕。第一頁上的後退按鈕不需要,因爲它重定向到黑色頁面。我知道它必須使用Navigation.PushAsync做些什麼。但我無法弄清楚需要改變的地方。如果有人能以正確的方式指導我會有幫助。使用NavigationPage時返回按鈕在第一頁
這裏是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
}
}
}
能否請您闡述一下您的問題?您是否需要使用NavigationPage,或者檢測何時按下後退按鈕或其他東西? –
你能打包你的解決方案併發送過去嗎?如果是這樣,我的個人資料就在我的個人資料上,我會看看。您還需要更好地澄清問題,因爲它不清楚實際問題是什麼? – Pete
我不需要應用程序第一頁上的後退按鈕功能。我在重定向到空白頁面的應用程序的第一頁上獲得一個後退按鈕。 – Arti