Tôi đang thực sự làm việc với đoạn mã nàyXamarin - thanh toán bù trừ ListView lựa chọn
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);
}
}
}
Mã này thực sự cho thấy một menu flyout dễ dàng, bằng cách sử dụng Xamarin Forms công nghệ. Tôi hiện đang cố gắng để hiểu làm thế nào tôi có thể dễ dàng xóa lựa chọn ListView sau khi tôi đã chọn trang mà tôi muốn đến!
Tôi tìm thấy đoạn mã này trên trang web của Xamarin dành cho nhà phát triển (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
};
Nhưng tôi không thể hiện ra làm thế nào tôi cần tích hợp nó với mã của tôi trên đó :)
Trước tiên, bạn sẽ không cần tách các sự kiện ItemSelected để thực hiện việc này. Tôi nghĩ bạn chỉ có thể gọi đây là 'listview.SelectedItem = null' để thiết lập lại thuộc tính listeditem listviewem. Hoặc là trước hoặc sau khi bạn đẩy trang mới. Thứ hai, tốt hơn nên sử dụng 'ItemTapped' để bắt đầu quá trình chuyển đổi trang và sử dụng các mục được chọn để xử lý/chuyển dữ liệu sang chế độ xem tiếp theo. –