2014-12-05 85 views
7

我实际上这段代码Xamarin - 清除的ListView选择

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); 
     } 
    } 
} 

此代码实际上显示了一个简单的弹出式菜单的工作,使用Xamarin窗体技术。 我目前正在尝试了解如何在选择了我要前往的页面后轻松地清除ListView选择!

我在Xamarin的网站上为devs找到这段代码(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 
}; 

但我目前还无法弄清楚如何我应该把它与我的代码集成上面有:)

+1

首先你不需要分开ItemSelected事件来做到这一点。我想你可以调用这个'listview.SelectedItem = null'来重置listview selecteditem属性。在推新页面之前或之后。其次,最好使用ItemTapped来启动页面转换,并使用itemselected来处理/传递数据到下一个视图。 – 2014-12-05 18:49:02

回答

11

你分配ItemSelected处理器的两倍,这是一个坏主意。所有你应该做的就是这一行添加到您现有的ItemSelected处理

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

感谢您的及时答复!无论如何,我已经尝试过,我得到的是这样的:http://1drv.ms/1s0K0cO只要我点击ListView中的任何元素 – 2014-12-05 00:47:42

+0

如果您将该行添加到您的ItemSelected处理程序的顶部,那么所有对SelectedItem的后续引用将因为现在为空而中断。在引用它的所有其他代码完成后,尝试将其添加到处理程序的底部。 – Jason 2014-12-05 00:56:23

+0

那么,如果你看看我链接的截图,我正在做你在说什么,仍然一切都崩溃:( – 2014-12-05 22:37:33

8

我想补充杰森的答案,因为它忽略了一些重要信息。当您将ListView SelectedItem属性设置为null时,它将再次触发ItemSelected事件。所以如果你没有一个空检查,它会抛出一个异常。

这是它应该是什么样子:

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

我有同样的问题,但其他的解决方案并没有为我工作。由于我需要将自定义对象传递到下一页,因此我将选定的项目引用取消,并将自定义对象的项目引用引用。

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

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

};