Saya belajar xamarin dan mencoba menggunakan puglin Rg.Plugins.Popup plugin ini berguna untuk membuat halaman Popup

Saya ingin mengembalikan nilai boolean ketika pengguna mengklik ya (benar) atau tidak (salah) pada Halaman PopPup.

Ini kode Xaml saya:

<Label x:Name="NoLabel"ClassId="1">
<Label.GestureRecognizers>
    <TapGestureRecognizer Tapped="OnNopeorYesPopUp"  />
</Label.GestureRecognizers>
</Label>
<Label x:Name="YesLabel">
<Label.GestureRecognizers>
    <TapGestureRecognizer Tapped="OnNopeorYesPopUp"  />
</Label.GestureRecognizers>
</Label>

Inilah cara saya ingin menggunakan fungsi (OnNopeorYesPopUp) yang mengembalikan bolean di kelas YesOrNotPopup saya tetapi itu tidak mungkin:

 public bool OnNopeorYesPopUp(object sender, EventArgs e)
        {
            var LabelTapped = (Label)sender;

            if (LabelTapped.ClassId == "1")
            {
                return true;
            }
            else if (LabelTapped.ClassId == "2")
            {
                return false;
            }

            return false;


        }

Ini adalah bagaimana saya akan memanggil PopupPage di ContentPage:

var answer =  PopupNavigation.Instance.PushAsync(new YesOrNotPopup());

Dari bantuan tumpukan:

// display the popup pahe 
     await PopupNavigation.Instance.PushAsync(new YesOrNotPopup ()
                            {
// instace the variable 
                                Message = "Submit your request?" ,
// instace the variable 
                                PrimaryButtonText = "Yes" ,
// instace the variable 
                                PrimaryCommand = new Command (async() =>
                                                               {
                                                                   Console.WriteLine("label1");

                                                               }) ,
// instace the variable 
                                SecondaryButtonText = "No" ,
// instace the variable 
                                SecondaryCommand = new Command ( async() =>
                                                               {
                                                                   Console.WriteLine("label2");
                                                               }) ,

                                            }
                            );

Terima kasih atas bantuan Anda

0
hugokis 30 Maret 2020, 02:04

1 menjawab

Jawaban Terbaik

Alih-alih mengembalikan nilai bool, buat dua Command di YesOrNotPopup:

public static readonly BindableProperty PrimaryCommandProperty = BindableProperty.Create(nameof(PrimaryCommand), typeof(ICommand), typeof(YesOrNotPopup));
public static readonly BindableProperty SecondaryCommandProperty = BindableProperty.Create(nameof(SecondaryCommand), typeof(ICommand), typeof(YesOrNotPopup));

public ICommand PrimaryCommand
{
    get => ( ICommand ) this.GetValue ( PrimaryCommandProperty );
    set => this.SetValue ( PrimaryCommandProperty , value );
}
public ICommand SecondaryCommand
{
    get => ( ICommand ) this.GetValue ( SecondaryCommandProperty );
    set => this.SetValue ( SecondaryCommandProperty , value );
}

Dan ikat mereka ke properti Commands dari TapGestureRecognizer dari dua Labels.

Perlu lebih banyak fleksibilitas atas kontrol?

Buat PrimaryButtonText, SecondaryButtonText dan Message properti yang dapat diikat..

XAML dengan pengikatan:

<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
                 WidthRequest="60"
                 HeightRequest="60"
                 Padding="30"
                 x:Class="MyApp.PopupPages.YesOrNotPopup "
                 x:Name="ThisPopUpPage" >
    <StackLayout>
        <Label Text="{Binding Message, Source={Reference ThisPopUpPage}}" HorizontalOptions="CenterAndExpand"/>

        <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
            <Label x:Name="NoLabel" ClassId="1" Text="{Binding SecondaryButtonText, Source={Reference ThisPopUpPage}}">
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding SecondaryCommand, Source={Reference ThisPopUpPage}}"/>
                </Label.GestureRecognizers>
            </Label>
            <Label x:Name="YesLabel" Text="{Binding PrimaryButtonText, Source={Reference ThisPopUpPage}}">
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding PrimaryCommand, Source={Reference ThisPopUpPage}}"/>
                </Label.GestureRecognizers>
            </Label>
        </StackLayout>
    </StackLayout>
</pages:PopupPage>

Pemakaian:

PopupNavigation.Instance.PushAsync(new YesOrNotPopup {
{
    Message = "Submit your request?" ,
    PrimaryButtonText = "Yes" ,
    PrimaryCommand = new Command (async() =>
                                   {
                                       ShowLoading();
                                       await BookingService.BookRoom(booking);
                                       HideLoading();
                                   }) ,
    SecondaryButtonText = "No" ,
    SecondaryCommand = new Command (()=> /*do nothing*/)
});
2
mshwf 30 Maret 2020, 08:59