如何使用相同的 xaml 文件在弹出窗口 window 中显示不同的名称?

How do I display different names in the pop-up window by using the same xaml file?

我正在尝试制作一个弹出窗口 window,通过按签入按钮,将出现一个显示“签入”字符串的弹出窗口。结帐按钮将执行完全相同的操作,只是将显示“结帐”。我怎样才能只使用一个 PopUp.xaml 文件而不是 2 个类似的文件。

入住和退房功能:

private async void CheckOut_Tapped(object sender, EventArgs args)
    {
        await PopupNavigation.Instance.PushAsync(new CheckOutPopUp(true));
    }

    private async void CheckIn_Tapped(object sender, EventArgs args)
    {
        await PopupNavigation.Instance.PushAsync(new CheckInPopUp(true));
    }

CheckInPopUp.xaml/CheckOutPopUp.xaml:

<StackLayout
            HorizontalOptions="FillAndExpand"
            Orientation="Vertical"
            VerticalOptions="FillAndExpand">
            <Label
                Margin="20,50,20,10"
                FontSize="Large"
                HorizontalOptions="CenterAndExpand"
                Text="Check In"      //"Check Out" on CheckOutPopUp.xaml
                TextColor="White"
                VerticalOptions="CenterAndExpand" />

据推测,当前状态“IsCheckedIn”有一个布尔值。

然后在xaml中针对不同的情况使用triggers

<Label ... Text="Check In">
    <Label.Triggers>
        <DataTrigger
            Binding="{Binding IsCheckedIn}"
            TargetType="Label"
            Value="true">
            <Setter Property="Text" Value="Checked Out" />
        </DataTrigger>
    </Label.Triggers>
</Label>

<Button Clicked="Button_Clicked">

并像这样定义您的事件:

private async void Button_Clicked(object sender, EventArgs args)
{
    await PopupNavigation.Instance.PushAsync(
        IsCheckedIn ? new CheckOutPopUp(true) : new CheckInPopUp(true));
}

如果“IsCheckIn”来自您的视图模型,请改用“viewModel.IsCheckIn”等视图模型值。

是的,您可以使用 Xamarin.Forms Triggers 来实现这一点,就像 Shaw 提到的那样。

如果我没理解错的话,你是想在点击两个按钮(Check In 按钮和 Check Out 按钮)后在同一个 PopupPage 中显示两个不同的文本,对吗?

如果是,可以参考以下代码:

第一页有两个按钮(check in按钮和check out按钮)

    private async void CheckInButton_Clicked(object sender, EventArgs e)
    {
        await PopupNavigation.Instance.PushAsync(new CheckPopUp(true));
    }

    private async void CheckOutButton_Clicked(object sender, EventArgs e)
    {
        await PopupNavigation.Instance.PushAsync(new CheckPopUp(false));
    }

单击按钮 CheckInButtonCheckOutButton 后,将显示页面 CheckPopUp

CheckPopUp.xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CheckPopUp : PopupPage
{
    public bool IsCheckedIn { get; set; }
    public CheckPopUp(bool ischecked)
    {
        InitializeComponent();

        this.IsCheckedIn = ischecked;

        BindingContext = this;
    }
}

CheckPopUp.xaml

<?xml version="1.0" encoding="utf-8" ?>
<animations:PopupPage  xmlns:animations="http://rotorgames.com" xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                       BackgroundColor="Green"
             x:Class="FrameRendererApp.CheckPopUp">
    <ContentPage.Content>
        <StackLayout>
            <Label  Text="Check In">
                <Label.Triggers>
                    <DataTrigger
                       Binding="{Binding IsCheckedIn}"
                       TargetType="Label"
                       Value="false">
                        <Setter Property="Text" Value="Check Out" />
                    </DataTrigger>
                </Label.Triggers>
            </Label>
        </StackLayout>
    </ContentPage.Content>
</animations:PopupPage>