【问题标题】:How can I combine XAML and a template into one C# template with Rg.Plugins.Popup?如何使用 Rg.Plugins.Popup 将 XAML 和模板组合成一个 C# 模板?
【发布时间】:2020-11-25 05:06:38
【问题描述】:

我想将在我的代码中添加对话框弹出窗口的过程简化为所需的绝对最小值。

目前我有这个代码:

<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。

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms contentproperty


    【解决方案1】:

    从这段代码中:

    <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>
    

    可以看出PopupPage的ContentProperty是一个单独的View(上面代码中被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>
    

    那么您希望PopupPage 的ContentProperty 能够获取项目列表(例如上面代码中的两个Labels),并且这些项目设置为您的@ 987654329@ 应该放在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>
    

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 2012-10-05
      • 1970-01-01
      相关资源
      最近更新 更多