C# Xamarin表单:System.InvalidCastException:&x27;指定的强制转换无效

C# Xamarin表单:System.InvalidCastException:&x27;指定的强制转换无效,c#,listview,xamarin,xamarin.forms,C#,Listview,Xamarin,Xamarin.forms,我在xamarin中有一个listview,当我尝试在按钮单击事件中强制转换selecteditem时,它会给出错误。SavedVira类是针对sqlite数据库表的,我尝试使用async,但它似乎比值更麻烦。(我对编程非常陌生)。这是我的密码: XAML: <ListView x:Name="SavedViraView" HasUnevenRows="True" ItemTapped="SavedViraView_ItemTapped&q

我在xamarin中有一个listview,当我尝试在按钮单击事件中强制转换selecteditem时,它会给出错误。SavedVira类是针对sqlite数据库表的,我尝试使用async,但它似乎比值更麻烦。(我对编程非常陌生)。这是我的密码:

XAML:

<ListView x:Name="SavedViraView" HasUnevenRows="True" ItemTapped="SavedViraView_ItemTapped">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                        <StackLayout Padding="10">
                            <Label Text="Navn:" FontSize="18" FontAttributes="Bold"/>
                            <Label Text="{Binding Virusnavn}" FontSize="15"/>
                            <Label Text="Symptomer:" FontSize="18" FontAttributes="Bold"/>
                            <Label Text="{Binding symptom1}"/>
                            <Label Text="{Binding symptom2}"/>
                            <Label Text="{Binding symptom3}"/>
                            <Label Text="{Binding symptom4}"/>
                            <Label Text="{Binding symptom5}"/>
                            <Label Text="{Binding symptom6}"/>
                            <Label Text="Lavet af:" FontSize="18" FontAttributes="Bold"/>
                            <Label Text="{Binding Creator}"/>
                            <Button Text="Load" x:Name="LoadButton" Clicked="LoadButton_Clicked"/>
                        </StackLayout>
                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
private ObservableCollection<SavedVira> SavedVira = new ObservableCollection<SavedVira>();
    public Åben()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        nameLabel.Text = Values.currentUser;
        Init();
    }

    private async void Init()
    {
        BindingContext = new SavedVira();
        var Viralist = await DataServices.GetSavedVira();
        SavedVira = new ObservableCollection<SavedVira>(Viralist);
        SavedViraView.ItemsSource = SavedVira;
    }


    private void LoadButton_Clicked(object sender, EventArgs e)
    {
        var LoadVirus = ((ListView)sender).SelectedItem as SavedVira;
        if (LoadVirus == null)
            return;
        Values.LoadBool = true;
        Values.symptomZero = LoadVirus.symptom1;
        Values.symptomOne = LoadVirus.symptom2;
        Values.symptomTwo = LoadVirus.symptom3;
        Values.symptomThree = LoadVirus.symptom4;
        Values.symptomFour = LoadVirus.symptom5;
        Values.symptomFive = LoadVirus.symptom6;

    }
[Table("savedvira")]
public class SavedVira
{
    [PrimaryKey, AutoIncrement]
    [Column("id")]
    public int Id { get; set; }

    [Column("virusnavn")]
    public string Virusnavn { get; set; }

    [Column("creator")]
    public string Creator { get; set; }

    [Column("symptoms1")]
    public string symptom1 { get; set; }

    [Column("symptoms2")]
    public string symptom2 { get; set; }

    [Column("symptoms3")]
    public string symptom3 { get; set; }

    [Column("symptoms4")]
    public string symptom4 { get; set; }

    [Column("symptoms5")]
    public string symptom5 { get; set; }

    [Column("symptoms6")]
    public string symptom6 { get; set; }
}

任何帮助都将不胜感激

发送方
是一个
按钮
,因此这不起作用

var LoadVirus = ((ListView)sender).SelectedItem as SavedVira;
而是这样做

var btn = (Button)sender;
var selected = (SavedVira)btn.BindingContext;
另外,对实例变量和类使用相同的名称是令人困惑的,我建议您避免使用它

SavedVira = new ObservableCollection<SavedVira>(Viralist);
SavedVira=newobserveCollection(病毒主义者);