我正在处理这段代码
using System;
using Xamarin.Forms;
using System.Diagnostics;
namespace CryptoUI
{
public class HomePage : Xamarin.Forms.MasterDetailPage
{
public HomePage()
{
// Set up the Master, i.e. the Menu
Label header = new Label
{
Text = "MENU",
Font = Font.SystemFontOfSize(20, FontAttributes.Bold),
HorizontalOptions = LayoutOptions.Center
};
// create an array of the Page names
string[] myPageNames = {
"Main",
"Page 2",
"Page 3",
};
// Create ListView for the Master page.
ListView listView = new ListView
{
ItemsSource = myPageNames,
};
// The Master page is actually the Menu page for us
this.Master = new ContentPage
{
Title = "Test",
Content = new StackLayout
{
Children =
{
header,
listView
},
}
};
// Define a selected handler for the ListView contained in the Master (ie Menu) Page.
listView.ItemSelected += (sender, args) =>
{
// Set the BindingContext of the detail page.
this.Detail.BindingContext = args.SelectedItem;
string currentPage = this.GetType().Name.ToString();
// This is where you would put your “go to one of the selected pages”
if(listView.SelectedItem.Equals("Main") && !currentPage.Equals("HomePage")){
AsyncPush(new HomePage());
}
else if(listView.SelectedItem.Equals("Page 2") && !currentPage.Equals("SecondPage")){
AsyncPush(new SecondPage());
}
else if(listView.SelectedItem.Equals("Page 3") && !currentPage.Equals("ThirdPage")){
AsyncPush(new ThirdPage());
}
// Show the detail page.
this.IsPresented = false;
};
listView.ItemSelected += (senders, e) => {
if (e.SelectedItem == null) return; // don't do anything if we just de-selected the row
// do something with e.SelectedItem
((ListView)senders).SelectedItem = null; // de-select the row
};
// Set up the Detail, i.e the Home or Main page.
Label myHomeHeader = new Label
{
Text = "Home Page",
HorizontalOptions = LayoutOptions.Center
};
string[] homePageItems = { "Alpha", "Beta", "Gamma" };
ListView myHomeView = new ListView {
ItemsSource = homePageItems,
};
var myHomePage = new ContentPage();
myHomePage.Content = new StackLayout
{
Children =
{
myHomeHeader,
myHomeView
} ,
};
this.Detail = myHomePage;
}
public async void AsyncPush(Page page)
{
await Navigation.PushAsync(page);
}
}
}
这段代码显示了一个简单的FlyOut菜单,使用Xamarin Forms技术实现。我正在尝试理解如何在选择要前往的页面后轻松清除ListView的选中状态!
我在Xamarin开发者网站上找到了这段代码(http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/listview/);
listView.ItemSelected += (sender, e) => {
if (e.SelectedItem == null) return; // don't do anything if we just de-selected the row
// do something with e.SelectedItem
((ListView)sender).SelectedItem = null; // de-select the row
};
但是我目前无法弄清楚如何将它与上面的代码集成 :)
listview.SelectedItem = null
来重置listview的selecteditem属性即可。可以在推送新页面之前或之后执行。其次,最好使用"ItemTapped"事件来启动页面转换,并使用ItemSelected来处理/传递数据到下一个视图。 - Jared Reeves