如何将 XAML 和一个模板与 Rg.Plugins.Popup 组合成一个 C# 模板?

How can I combine XAML and a template into one C# template with Rg.Plugins.Popup?

我想将我代码中的对话框弹出窗口的添加简化到所需的绝对最低限度。

目前我有这个代码:

<pages:PopupPage
    x:Class="Test.Popup"
    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">
    <ContentView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <t:PopupFrame>
           <Label Text="ABC" />
           <Label Text="ABC" />
        </t:PopupFrame>
    </ContentView>
</pages:PopupPage>
public partial class Popup : Rg.Plugins.Popup.Pages.PopupPage
{
    public Popup()
    {
        InitializeComponent();
    }
}

我这里有PopupFrame

[Xamarin.Forms.ContentProperty("Contents")]
public class PopupFrame : Frame
{
    StackLayout contentStack { get; } = new StackLayout()
    {
        Spacing = 0,
        Padding = new Thickness(0),
        Orientation = StackOrientation.Vertical
    };
    public IList<View> Contents { get => contentStack.Children; }

    public PopupFrame()
    {
        Content = contentStack;
        HasShadow = true;
        HorizontalOptions = LayoutOptions.FillAndExpand;
        Padding = 0;
        SetDynamicResource(BackgroundColorProperty, "PopUpBackgroundColor");
        SetDynamicResource(CornerRadiusProperty, "PopupCornerRadius");
        VerticalOptions = LayoutOptions.Center;   
    }
}

任何人都可以建议我可以将这两者结合起来的方法,以便只需要以下内容:

<t:PopupPage
    x:Class="Test.Popup"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:t="clr-namespace:Test.Templates">
    <Label Text="ABC" />
    <Label Text="ABC" />
</pages:PopupPage>

所以我正在寻找的内容是:

[Xamarin.Forms.ContentProperty("Contents")]
public class Popup : Rg.Plugins.Popup.Pages.PopupPage 
{
}

更新:这是我根据建议的答案尝试过的方法:

<?xml version="1.0" encoding="UTF-8" ?>
<pages:PopupPage
    x:Class="Memorise.DecksTab.CopyDeckPopup"
    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">
    <Label Text="ABC" />
    <Label Text="ABC" />
</pages:PopupPage>

支持代码:

[Xamarin.Forms.ContentProperty("Contents")]
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CopyDeckPopup : Rg.Plugins.Popup.Pages.PopupPage
{

    PopupFrame contentFrame { get; } = new PopupFrame();

    public IList<View> Contents { get => contentFrame.Contents; }

    public CopyDeckPopup(string clickedDeckName, string clickedDeckDescription)
    {
        BindingContext = new CopyDeckPopupViewModel(clickedDeckName, clickedDeckDescription);
        InitializeComponent();
        Content = new ContentView()
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            Content = contentFrame
        };
    }

}

它给我这个错误,但显示是正确的,我看到两个 ABC。

来自这段代码:

<pages:PopupPage
    x:Class="Test.Popup"
    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">
    <ContentView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <t:PopupFrame>
           <Label Text="ABC" />
           <Label Text="ABC" />
        </t:PopupFrame>
    </ContentView>
</pages:PopupPage>

可以看出PopupPageContentProperty是一个单独的View(被ContentView中的一个ContentView占用了上面的代码)。

但是如果你希望能够编写像

这样的代码
<t:PopupPage
    x:Class="Test.Popup"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:t="clr-namespace:Test.Templates">
   <Label Text="ABC" />
   <Label Text="ABC" />
</pages:PopupPage>

那么你希望 PopupPageContentProperty 能够获取项目列表(比如上面代码中的两个 Label),另外这些项目设置为您的 PopupFrame 应该放在 ContentView...

你所要做的就是修改PopupPage(我假设它继承自ContentPage[?])以接受多个项目作为其内容,如下

[Xamarin.Forms.ContentProperty("Contents")]
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PopupPage : ContentPage
{

    PopupFrame contentFrame { get; } = new PopupFrame();

    public IList<View> Contents { get => contentFrame.Contents; }

    public PopupPage()
    {
        InitializeComponent();
        Content = new ContentView()
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            Content = contentFrame
        };
    }
}

通过这样做,现在您将能够编写您想要的代码

<?xml version="1.0" encoding="utf-8" ?>
<t:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:t="clr-namespace:popupframe"
    x:Class="popupframe.MainPage">
    <Label Text="Label1"/>
    <Label Text="Label2"/>
</t:PopupPage>