Как вернуть значение bool с помощью Popup Page из Rg.Plugins.Popup - PullRequest
0 голосов
/ 30 марта 2020

Я изучаю xamarin и пытаюсь использовать puglin Rg.Plugins.Popup Этот плагин полезен для создания всплывающей страницы

Я хотел бы вернуть логическое значение, когда пользователь нажимает на кнопку да ( true) или no (false) на странице PopPup.

Вот мой код Xaml:

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

Вот как я хотел бы использовать функцию (OnNopeorYesPopUp), которая возвращает логические значения в мой класс YesOrNotPopup, но это невозможно:

 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;


        }

Это способ вызова PopupPage в ContentPage:

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

Из справки стека:

// 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");
                                                               }) ,

                                            }
                            );

Спасибо за вашу помощь

1 Ответ

2 голосов
/ 30 марта 2020

Вместо возврата значения bool создайте два Command s в 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 );
}

и свяжите их со свойствами Command s TapGestureRecognizer двух Label s.

Нужно больше гибкости в управлении?

Создать PrimaryButtonText, SecondaryButtonText и Message привязываемых свойств ..

XAML с привязкой:

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

Использование:

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*/)
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...