e.SelectedItem.ToString() 产生 class 名称 Xamarin.Forms

e.SelectedItem.ToString() produces class name Xamarin.Forms

我是 Xamarin.Forms 的新手,尽管进行了搜索,但我未能成功获取与我的 ListView 中的选定项目可绑定属性关联的字符串。目标是根据所选项目采取行动。我在 XAML 中定义了我的 UI 如下:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="EdgeNet.EdgeNetAddModule">
<StackLayout Padding="10">
    <Label Text="" FontSize="12" />
    <Label Text="Add Module" TextColor="#00A79D" HorizontalOptions="Center" FontSize="36"/>
    <Label Text="" FontSize="12"/>
    <ListView x:Name="moduleTypesListView"
        HorizontalOptions="FillAndExpand" 
        VerticalOptions="FillAndExpand" 
        Header="Select A Module" 
        ItemSelected="OnSelection"
        RowHeight="80">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextCell Text="{Binding Name}" TextColor="#00A79D" Detail="{Binding Detail}" DetailColor="#A7A9AC"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Image Source="Logo.png" />

我已将列表视图绑定到代码隐藏中的 ObservableCollection,如下所示:

 moduleTypesListView.ItemsSource = new ObservableCollection<Products>()
        {
            new Products {Name = "Outlet", Detail = "Standard 3-Prong Residential"},
            new Products {Name = "Switch", Detail = "Standard Wall Switch"},
            new Products {Name = "Thermostat", Detail = "edgeNet Smart Thermostat"}
        };

我想根据所选项目打开特定的 activity。对于任何选定的项目,我的事件处理程序如下 e.SelectedItem.ToString() returns "EdgeNet.Products":

        void OnSelection(object sender, SelectedItemChangedEventArgs e)
    {
        if (e.SelectedItem == null)
        {
            return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
        }

        Console.WriteLine(e.SelectedItem.ToString()); //This outputs the class name Products

        //Disable visual ugly orange highlighter for selected item
        ((ListView)sender).SelectedItem = null;

        //ToDo: Figure out which item was selected and start setup routine
    }

提前感谢您的帮助。我确定我在这里遗漏了一些简单的东西,只是不确定它是什么。

e.SelectedItem 是一个 Product,因此您可以转换它然后访问它的属性

var product = (Product) e.SelectedItem;

if (product.Name == "blah") {
  ...
}