首页 > 解决方案 > 使用相同的按钮删除项目

问题描述

在使用与执行其他任务相同的按钮时,我在弄清楚如何从项目中删除项目时遇到问题。总而言之,我正在做一个项目,我有一堆用户可以喜欢/不喜欢的事件。我遇到的问题是试图弄清楚一旦用户使用“喜欢”按钮喜欢该事件后如何从我的项目中删除该事件。本质上,我希望“喜欢”按钮执行 2 件事,其中之一是按下它时将其设置为“true”,这已经在执行此操作,但我也希望它在喜欢它后从屏幕上删除该事件。

这是我的 XAML:

    <StackLayout VerticalOptions="FillAndExpand">
        <Grid RowSpacing="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Image x:Name="LblEventImage"
                       Aspect="Fill"
                       HeightRequest="350" />
            <BoxView BackgroundColor="Black"
                         HeightRequest="350"
                         Opacity="0.3" />
            <Image Source="BackIconOrg.png"
                       Margin="5,40,0,0"
                       HorizontalOptions="Start"
                       VerticalOptions="Start"
                       Grid.Row="0">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer x:Name="ImgBack" Tapped="backButton_Clicked"/>
                </Image.GestureRecognizers>
            </Image>
        </Grid>
        <Frame VerticalOptions="FillAndExpand"
                   IsClippedToBounds="False"
                   BackgroundColor="#263A4F"
                   Margin="0,-40,0,0"
                   CornerRadius="25"
                   HasShadow="False">
            <StackLayout Margin="0,-50,0,0">
                <Grid HeightRequest="200"
                          VerticalOptions="Start">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="4*" />
                        <ColumnDefinition Width="6*"/>

                    </Grid.ColumnDefinitions>
                    <StackLayout Grid.Column="0"
                                     Padding="5"
                                     Spacing="5"
                                     Margin="0,40,0,0">
                        <Label x:Name="LblEventName"
                                   TextColor="#fc4600"
                                   FontSize="Large"
                                   FontFamily="TTBold"
                                   FontAttributes="Bold"/>
                        <StackLayout Orientation="Horizontal"
                                         Spacing="0">
                            <Frame BackgroundColor="#8B93A6"
                                       BorderColor="#8B93A6"
                                       Padding="8"
                                       CornerRadius="15">
                                <Label TextColor="White"
                                           x:Name="LblEventCategory"
                                           FontFamily="TTNorms"/>
                            </Frame>
                        </StackLayout>
                        <StackLayout Orientation="Vertical" Grid.ColumnSpan="3">
                            <Label x:Name="LblEventCity"
                                       TextColor="White"
                                       FontFamily="TTNorms"/>
                            <Label x:Name="LblEventState"
                                       TextColor="White"
                                       FontFamily="TTNorms"/>
                        </StackLayout>
                        <!--<Label x:Name="LblLanguage"
                                   TextColor="White" />
                            <Label x:Name="LblDuration"
                                   TextColor="White" />-->
                    </StackLayout>
                </Grid>
                <Label Text="Details:"
                           FontSize="Medium"
                           Margin="0,20,0,0"
                           TextColor="White"
                           FontFamily="TTNorms"/>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.65*" />
                        <ColumnDefinition Width="0.35*" />
                    </Grid.ColumnDefinitions>
                    <Label Text="Scheduled Date:"
                               TextColor="White"
                               Grid.Row="0"
                               Grid.Column="0"
                               FontFamily="TTNorms"/>
                    <Label Text="August 20, 2021"
                               TextColor="#8B93A6"
                               Grid.Row="1"
                               Grid.Column="0"
                               FontFamily="TTNorms"/>
                    <Label Text="Group Size:"
                               TextColor="White"
                               Grid.Row="0"
                               Grid.Column="1"
                               FontFamily="TTNorms"/>
                    <Label x:Name="LblEventGroupSize"
                               TextColor="#8B93A6"
                               Grid.Row="1"
                               Grid.Column="1"
                               FontFamily="TTNorms"/>
                    <Label Text="Paid:"
                               TextColor="White"
                               Grid.Row="2"
                               Grid.Column="0"
                               FontFamily="TTNorms"/>
                    <Label x:Name="LblEventPay"
                               TextColor="#8B93A6"
                               Grid.Row="3"
                               Grid.Column="0"
                               FontFamily="TTNorms"/>
                </Grid>
                <BoxView BackgroundColor="#8B93A6"
                             HeightRequest="1" />
                <Label Text="Description"
                           TextColor="White"
                           FontFamily="TTNorms"/>
                <Label x:Name="LblEventDescription"
                           TextColor="#8B93A6"
                           FontFamily="TTNorms"/>
                <StackLayout Orientation="Horizontal" 
                                 HorizontalOptions="Center"
                                 VerticalOptions="Center"
                                 Spacing="60"
                             Margin="0,30,0,0">
                    <Image Source="LikeButton.png"
                           HeightRequest="150">
                        <Image.GestureRecognizers>
                            <TapGestureRecognizer x:Name="likedevent" Tapped="likeButton_Clicked" />
                        </Image.GestureRecognizers>
                    </Image>
                    <Image Source="DislikeButton.png"
                           HeightRequest="150">
                        <Image.GestureRecognizers>
                            <TapGestureRecognizer x:Name="dislikeevent" Tapped="dislikeevent_Tapped"/>
                        </Image.GestureRecognizers>
                    </Image>
                </StackLayout>
            </StackLayout>
        </Frame>
    </StackLayout>
</ContentPage.Content>

CS:

public partial class MatchDetail : ContentPage
{
    private Event event1;
    public MatchDetail(int eventId)
    {
        InitializeComponent();
        GetEventDetail(eventId);
    }      
    private async void GetEventDetail(int eventId)
    {
        event1 = await ApiService.GetEventById(eventId);
        LblEventName.Text = event1.Name;
        LblEventDescription.Text = event1.Description;
        LblEventCity.Text = event1.City;
        LblEventImage.Source = event1.FullImageUrl;
        LblEventCategory.Text = event1.Category;
        LblEventState.Text = event1.State;
        LblEventGroupSize.Text = event1.GroupSize.ToString();
        LblEventPay.Text = event1.PayTrueFalse;
    }

    private async void likeButton_Clicked(object sender, EventArgs e)
    {
        var likedEvent = new Result()
        {
            Name = LblEventName.Text,
            UserId = Preferences.Get("userId", 0),
            LikeTrueFalse = "True"
            
        };

        if (event1.LikeTrueFalse == "True")
        {
            //RemoveEvent
            
        }

        var response = await ApiService.LikedEvents(likedEvent);
        if (response)
        {
            
            await DisplayAlert("", "Your event has been liked", "Ok");
            
        }
        else
        {
            await DisplayAlert("Oops", "something went wrong", "cancel");
        }
    }

    private void backButton_Clicked(object sender, EventArgs e)
    {
        Navigation.PopModalAsync();
    }

标签: c#xamarinxamarin.forms

解决方案


在您的主页中

MessagingCenter.Subscribe<object, int>(this, "RemoveEvent", (sender,id) =>
{
  // do whatever to remove event "id" here
});

然后在您的详细信息页面中,将其标记为收藏后

Messaging.Center.Send<object, int>(this, "RemoveEvent", eventId);

推荐阅读