2014-12-05 12 views
7

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 đó :)

+1

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. –

Trả lời

11

Bạn đang gán handler ItemSelected hai lần, đó là một ý tưởng tồi. Tất cả những gì bạn cần làm là thêm dòng này vào trình xử lý ItemSelected hiện tại của bạn

((ListView)sender).SelectedItem = null; 
+0

Cảm ơn câu trả lời nhanh chóng! Dù sao tôi đã thử rằng trước đó, tất cả tôi nhận được là điều này: http://1drv.ms/1s0K0cO ngay sau khi tôi bấm vào bất kỳ yếu tố trong ListView –

+0

Nếu bạn thêm dòng đó vào đầu xử lý ItemSelected của bạn, sau đó tất cả các tham chiếu tiếp theo tới SelectedItem sẽ phá vỡ vì nó bây giờ là null. Hãy thử thêm nó ở dưới cùng của trình xử lý, sau khi tất cả các mã khác tham chiếu nó được thực hiện. – Jason

+0

Vâng, nếu bạn nhìn vào ảnh chụp màn hình tôi liên kết ở đó, tôi đang làm những gì bạn đang nói, vẫn còn mọi thứ đang bị rơi: ( –

8

Tôi muốn thêm vào câu trả lời của Jason vì nó bỏ sót một số thông tin quan trọng. Khi bạn đặt thuộc tính ListView SelectedItem thành null, nó sẽ kích hoạt lại sự kiện ItemSelected. Vì vậy, nếu bạn không có một kiểm tra null, nó sẽ ném một ngoại lệ.

Đây là những gì nó sẽ giống như thế:

void ItemSelected(object sender, EventArgs args) 
{ 
    if (((ListView)sender).SelectedItem == null) 
     return; 
    //Do stuff here with the SelectedItem ... 
    ((ListView)sender).SelectedItem = null; 
} 
2

Tôi có vấn đề này tương tự nhưng các giải pháp khác không làm việc cho tôi. Vì tôi cần phải chuyển một đối tượng tùy chỉnh đến trang tiếp theo, tôi đã vô hiệu hóa tham chiếu của mục đã chọn và sử dụng mục đã khai thác tham chiếu cho đối tượng tùy chỉnh của tôi.

listView.ItemTapped += async (sender, e) =>{ 

    await Navigation.PushAsync(new DetailPage(e.Item as CustomObject)); 
    ((ListView)sender).SelectedItem = null; 

}; 
Các vấn đề liên quan