【发布时间】:2019-02-28 18:21:46
【问题描述】:
我正在使用 Rg.Plugins.Popup,并且我有一个带有按钮的弹出页面,我想用它来关闭弹出窗口。弹出窗口显示得很好,我现在可以通过背景选择将其关闭。但是对于好的用户体验来说,也需要一个按钮。按钮是这样连接的:
在我的 XAML 中:
<Button BackgroundColor="#2B653E"
Margin="0,20,0,0"
Padding="10,10,10,10"
Command="{Binding OnBtnOkTapped}"
Text="OK"
TextColor="White"
x:Name="btnOk"/>
在我的 ViewModel 中:
public ICommand OnBtnOkTapped = new Command(async () => { await PopupNavigation.Instance.PopAllAsync(); });
我已经确认绑定工作正常(其他属性工作正常)。但是当我点击按钮时什么都没有发生。所以我在这一行放了一个断点,发现它在弹出窗口出现时被调用了。
当我在弹出窗口中点击此按钮时,它不会被调用(即我没有命中断点)。
谁能告诉我为什么会这样?
编辑:做了一些进一步的测试,从 Command 更改为 Clicked 并在后面的代码中处理而不是在 ViewModel 中产生预期的结果。只是不明白为什么在页面加载时调用 ICommand 而不是在单击按钮时调用。
编辑 2:
根据@G.hakim 的要求,这里是此页面的完整 XAML:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
xmlns:lottie="clr-namespace:Lottie.Forms;assembly=Lottie.Forms"
x:Class="MyApp.PopUpPages.ReservationResult"
BackgroundColor="Transparent">
<pages:PopupPage.Animation>
<animations:ScaleAnimation
PositionIn="Center"
PositionOut="Center"
ScaleIn="1.2"
ScaleOut="0.8"
DurationIn="400"
DurationOut="300"
EasingIn="SinIn"
EasingOut="SinOut"
HasBackgroundAnimation="false"/>
</pages:PopupPage.Animation>
<StackLayout
HorizontalOptions="Center"
VerticalOptions="Center"
WidthRequest="300"
HeightRequest="400"
BackgroundColor="White"
Padding="30">
<lottie:AnimationView x:Name="ResultAnimation"
Animation="{Binding AnimationRef}"
AutoPlay="true"
Loop="false"
VerticalOptions="Center"
HorizontalOptions="Center"
HeightRequest="200"
WidthRequest="200"
Scale="1"/>
<Label Text="{Binding ResponseTitle}"
Margin="0,20,0,0"
HorizontalOptions="Center"
VerticalOptions="Start"
TextColor="#2B653E"
FontAttributes="Bold"
FontSize="Large"/>
<Label Text="{Binding ResponseMessage}"
HorizontalOptions="Center"
VerticalOptions="End"
TextColor="#2B653E"/>
<Button BackgroundColor="#2B653E"
Margin="0,20,0,0"
Padding="10,10,10,10"
Clicked="Handle_Clicked"
Text="OK"
TextColor="White"
x:Name="btnOk"/>
</StackLayout>
</pages:PopupPage>
这里是 ViewModel:
using System;
using System.Windows.Input;
using Rg.Plugins.Popup.Services;
using MyApp.Models;
using Xamarin.Forms;
namespace MyApp.ViewModels
{
public class ReservationResultViewModel : ViewModel
{
public string ResponseTitle { get; set; }
public string ResponseMessage { get; set; }
public string AnimationRef { get; set; }
public ICommand OnBtnOkTapped = new Command(async () => { await PopupNavigation.Instance.PopAllAsync(); });
public ReservationResultViewModel(ReservationStatus status)
{
switch (status.status)
{
case ReservationStatus.Status.Booked:
//
ResponseTitle = "Booked!";
ResponseMessage = "We've received your reservation, see you then!";
AnimationRef = "1166-tick.json";
break;
case ReservationStatus.Status.Full:
//
ResponseTitle = "Well this is embarrassing...";
ResponseMessage = "Looks like this is a really popular time and we're full. Head over to the Contact page though, we still might be able to help!";
AnimationRef = "4284-notification.json";
break;
case ReservationStatus.Status.HttpError:
//
ResponseTitle = "Hmmm...";
ResponseMessage = "Something went wrong, but we're pretty sure its our fault. Can you check your form and try again?";
AnimationRef = "3932-error-cross.json";
break;
case ReservationStatus.Status.ServerUnavailable:
//
ResponseTitle = "It's not you, it's me...";
ResponseMessage = "So sorry, it looks like the reservation service is down! Please head over to the Contact page, or try again later.";
AnimationRef = "4386-connection-error.json";
break;
}
}
}
}
视图背后的代码:
using System;
using System.Collections.Generic;
using Rg.Plugins.Popup.Pages;
using Rg.Plugins.Popup.Services;
using MyAPp.Models;
using MyApp.ViewModels;
using Xamarin.Forms;
namespace MyApp.PopUpPages
{
public partial class ReservationResult : PopupPage
{
public ReservationResultViewModel viewModel { get; set; }
public ReservationResult(ReservationResultViewModel vm)
{
InitializeComponent();
viewModel = vm;
BindingContext = viewModel;
}
async void Handle_Clicked(object sender, System.EventArgs e)
{
await PopupNavigation.Instance.PopAllAsync();
//throw new NotImplementedException();
}
}
}
【问题讨论】:
-
您是否还在为此寻找
ViewModel解决方案,或者您对现有的感到满意? -
我更喜欢视图模型解决方案,尽管我当然可以用我现在所拥有的东西来发布它。但我想我还是想弄清楚为什么会这样。
-
好的,所以我需要知道两件事,第一是您的命令之前在点击事件上工作,第二如果之前没有工作,您是否有单独的 ViewModel 用于弹出窗口
-
是的,我有一个单独的 ViewModel 用于弹出窗口,这是我在问题中指的 ViewModel,而不是调用它的页面的 ViewModel。不能说它以前是否有效,我把它内置到弹出窗口中,我没有移动我已经在那里的东西。而且我没有使用 Clicked 对其进行测试,直到它不适用于 ViewModel 中的命令。
-
您可以在问题中添加
ViewModel和View(xaml 和cs)吗?
标签: xamarin.forms