c# - Xamarin - 清除 ListView 选择

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

此代码实际上使用 Xamarin Forms 技术显示了一个简单的 FlyOut 菜单。 我目前正在尝试了解如何在选择要前往的页面后轻松清除 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
};

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

最佳答案

我想补充 Jason 的回答,因为它遗漏了一些重要信息。当您将 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;
}

关于c# - Xamarin - 清除 ListView 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27306685/

相关文章:

c# - 转换为值类型 'Int32' 失败,因为具体化值为 null

c# - 构造函数中的递归

java - 如何将ListView切换到TextView以从数据库检索数据

android - 避免为收件箱重复 Listview 项目

android - 带有 RelativeLayout 的 ListView 在按下时不突出显示背景

C# 将 XML 解析为带有子元素的对象

objective-c - iOS 7,未选择色调颜色?

C# linq 多次分组

mobile - Xamarin-跨平台移动应用入门

c# - 如何将 "1:15 P.M."解析为 NodaTime LocalTime?