Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

c# - Transform parameter 'sender' from the button into my object

I have a Button where I would like to transform the 'sender' object into my Nota Object When I hit the button, however, it fails to cast me correctly and I get an exception

System.NullReferenceException: 'Object reference not set to an instance of an object.'

My code c# is:

private async void Remove_Clicked(object sender, EventArgs e)
        {
            var button = sender as ImageButton;
            var model = button.BindingContext as Note;
            var Nota = listNote.First(x => x.Nota.Contains(model.Nota));
            listNote.Remove(Nota);
        }

public class Note
        {
            public string FraseGiorno { get; set; }
            public string Nota { get; set; }
            public string Data { get; set; }
        } 

The variable 'model' always has a null value

xaml

 <yummy:PancakeView x:Name="ReadNote" IsVisible="False" Grid.Row="1" CornerRadius="30,30,0,0" BackgroundColor="White" VerticalOptions="FillAndExpand">
            <yummy:PancakeView.Border>
                <yummy:Border Color="Blue" Thickness="4" />
            </yummy:PancakeView.Border>
            <Grid Margin="15">
                <Grid.RowDefinitions>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="1"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <BoxView x:Name="BVColorEdit" IsVisible="False" CornerRadius="100" Grid.Row="0" HorizontalOptions="End" WidthRequest="40">
                    <BoxView.Background>
                        <LinearGradientBrush  StartPoint="1,0" EndPoint="1,1">
                            <GradientStop Color="Blue" Offset="0" />
                            <GradientStop Color="LightBlue" Offset="1.0" />
                        </LinearGradientBrush>
                    </BoxView.Background>
                </BoxView>
                <BoxView x:Name="BVGrayEdit" IsVisible="False" CornerRadius="100" Grid.Row="0" HorizontalOptions="End" WidthRequest="40" BackgroundColor="Gray"/>
                <ImageButton x:Name="IMGEdit" Source="IconEdit.png" Grid.Row="0" HorizontalOptions="End" HeightRequest="20" Margin="0,0,60,0" Clicked="IMGEdit_Clicked" BackgroundColor="Transparent"/>
                <ImageButton x:Name="IMGRemove" Source="IconRemove.png" Grid.Row="0" HorizontalOptions="End" HeightRequest="20" Margin="0,0,10,0" BackgroundColor="Transparent" Clicked="IMGRemove_Clicked"/>
                <ImageButton x:Name="Save" IsVisible="False" Source="checked.png" Grid.Row="0" HorizontalOptions="End" HeightRequest="20" Margin="0,0,10,0" Clicked="SaveEdit_Clicked" BackgroundColor="Transparent"/>
                <ImageButton Source="close.png" Grid.Row="0" HorizontalOptions="Start" HeightRequest="20" Clicked="CloseEdit_Clicked" BackgroundColor="Transparent"/>
                <Editor x:Name="EditNota" IsReadOnly="True" Grid.Row="2" Placeholder="Scrivi la tua nota" PlaceholderColor="Gray" TextColor="Black"  MaxLength="150" TextChanged="TestoNota_TextChanged"/>

            </Grid>
        </yummy:PancakeView>
question from:https://stackoverflow.com/questions/65917736/transform-parameter-sender-from-the-button-into-my-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...