首页 > 解决方案 > 如何使用来自 Rg.Plugins.Popup 的弹出页面返回布尔值

问题描述

我学习 xamarin 并尝试使用 puglin Rg.Plugins.Popup这个插件对创建弹出页面很有用

当用户在 PopPup 页面上单击是 (true) 或否 (false) 时,我想返回布尔值。

这是我的 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>

这是我想如何使用在我的 YesOrNotPopup 类中返回布尔值的函数(OnNopeorYesPopUp),但这是不可能的:

 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;


        }

这就是我在 ContentPage 中调用 PopupPage 的方式:

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

                                            }
                            );

谢谢你的帮助

标签: c#xamlxamarin.forms

解决方案


不是返回bool值,而是在 中创建两个Commands 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 );
}

并将它们绑定到两个s 的Commands 属性上。TapGestureRecognizerLabel

需要更多的控制灵活性?

创建PrimaryButtonText,SecondaryButtonTextMessage可绑定的属性..

带绑定的 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*/)
});

推荐阅读