简体   繁体   English

Xamarin forms: System.InvalidCastException: '指定的转换无效

[英]Xamarin forms: System.InvalidCastException: 'Specified cast is not valid

I have a listview in xamarin and when i try to cast the selecteditem in a button clicked event it gives me the error.我在 xamarin 中有一个列表视图,当我尝试在按钮单击事件中投射所选项目时,它给了我错误。 The class SavedVira is for a sqlite database table, i'm trying to use async but it seems to have been more trouble then worth. class SavedVira 用于 sqlite 数据库表,我正在尝试使用异步,但它似乎更麻烦然后值得。 (i'm very new to programing). (我对编程很陌生)。 Here is my code:这是我的代码:

XAML: 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>

XAML.CS: XAML.CS:

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;

    }

SavedVira cast/model: SavedVira 演员/模型:

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

Any help would be grealty appriciated!任何帮助将不胜感激!

the sender is a Button so this doesn't work sender是一个Button ,所以这不起作用

var LoadVirus = ((ListView)sender).SelectedItem as SavedVira;

instead do this而是这样做

var btn = (Button)sender;
var selected = (SavedVira)btn.BindingContext;

also, using the same name for an instance variable and a class is confusing, I'd suggest you avoid it此外,对实例变量和 class 使用相同的名称会造成混淆,我建议您避免使用它

SavedVira = new ObservableCollection<SavedVira>(Viralist);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin 形式:System.InvalidCastException:“指定的强制转换无效。” - Xamarin forms: System.InvalidCastException: 'Specified cast is not valid.' 在 xamarin 项目 System.InvalidCastException:“指定的演员表无效。” - in xamarin project System.InvalidCastException: 'Specified cast is not valid.' Xamarin 表单与 Firebase。 数据检索抛出 System.InvalidCastException: &#39;指定的转换无效。&#39; - Xamarin Forms with Firebase. Data retrival throwing System.InvalidCastException: 'Specified cast is not valid.' 指定的转换无效-System.InvalidCastException - Specified cast is not valid - System.InvalidCastException System.InvalidCastException:指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid System.InvalidCastException:&#39;指定的强制转换无效。 - System.InvalidCastException: 'Specified cast is not valid.' “ System.InvalidCastException:指定的转换无效”-DateTime - “System.InvalidCastException: Specified cast is not valid” - DateTime System.InvalidCastException:指定的强制转换在.ExecuteScalar上无效 - System.InvalidCastException: Specified cast is not valid at .ExecuteScalar 未处理的异常:System.InvalidCastException:指定的强制转换无效。 Xamarin中发生错误 - Unhandled Exception: System.InvalidCastException: Specified cast is not valid. occurred ERROR in Xamarin System.InvalidCastException:指定的强制转换无效(linq查询) - System.InvalidCastException: Specified cast is not valid (linq query)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM