c# - 如何使用来自 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");
}) ,
}
);
谢谢你的帮助
解决方案
不是返回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 );
}
并将它们绑定到两个s 的Command
s 属性上。TapGestureRecognizer
Label
需要更多的控制灵活性?
创建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*/)
});
推荐阅读
- python - Python在(字符串)列表项中查找和计算某些单词
- node.js - 在模型实例化时无法生成唯一的 UUIDv4
- python - 无法使用 Python 3.7 的 pip 安装 pygame - 命令“python setup.py egg_info”失败,错误代码为 1
- javascript - 调用函数而不是重新加载页面
- c++ - 通过 size_type 和 const_iterator 的正确返回类型是什么?
- spring - 如何获取 @EnableOAuth2Sso 注释
- ruby-on-rails - Travis CI:在 Rails 服务器上启动 cypress
- android - ndk android中无法导入libpng、libjpeg
- sql - Teradata - 计算以前的值
- xml - Apache FOP:PDF 中的图表和图表?