Xamarin - 清除ListView选择

时间:2014-12-05 00:17:14

标签: c# listview xamarin xamarin-studio xamarin.forms

我实际上正在处理这段代码

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

但我目前无法弄清楚如何将其与我上面的代码集成:)

5 个答案:

答案 0 :(得分:14)

你要两次分配ItemSelected处理程序,这是一个坏主意。您应该做的就是将此行添加到现有的ItemSelected处理程序

  ((ListView)sender).SelectedItem = null; 

答案 1 :(得分:11)

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

这应该是这样的:

WebView

答案 2 :(得分:2)

我有同样的问题,但其他解决方案对我不起作用。由于我需要将自定义对象传递到下一页,因此我将所选项目引用置为无效,并使用项目点击引用作为我的自定义对象。

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

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

};

答案 3 :(得分:1)

我尊重所有给出的答案,但是在MVVM应用程序中,您最好避免过多的代码。我通常要做的是:

  1. 通常将ListView的ItemsSource绑定到ObservableCollection,其中T是我的情况下的CarViewModel

  2. 设置 SelectionMode =“ None” :这样可以避免在点击时选择SelectedItem

  3. 使用EventToCommandBehavior(我使用我自己的实现;请参见github.com或使用Prism.Forms的实现)将ListView的ItemTapped事件绑定到我的ViewModel命令SelectedCarChangedCommand。

  4. 在ViewModel的SelectedCarChangedCommand中,您将把选项卡式项作为ItemTappedEventArgs对象接收。

     <ListView
       x:Name="CarsListView"
       ItemsSource="{Binding Cars}"
       SelectionMode="None">
         <ListView.Behaviors>
             <behaviors:EventToCommandBehavior
                 Command="{Binding SelectedCarChangedCommand}"
                 EventName="ItemTapped" />
         </ListView.Behaviors>
    

答案 4 :(得分:0)

ListView.SelectedItem 没有设置器(我的意思是简单的Xamarin Android-不是Xamarin.Forms)。我建议使用以下代码:

private void DeselectEntities()
{
    if (this.listView != null && this.listView.CheckedItemPositions != null)
    {
        this.listView.CheckedItemPositions.Clear();
    }
}