覆盖 ContentPage 的 ContentProperty 时如何修复 XAML 中的 "The property Content is set more than once" 错误

How to fix "The property Content is set more than once" error in XAML when overriding the ContentProperty of ContentPage

这是我的代码:

<?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 />
    <Label />
</pages:PopupPage>

支持代码: csharp

[ContentProperty("Contents")]
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CopyDeckPopup : Rg.Plugins.Popup.Pages.PopupPage
{
    StackLayout contentStack { get; } = new StackLayout()
    {
        Spacing = 0,
        Padding = new Thickness(0),
        Orientation = StackOrientation.Vertical
    };
     public IList<View> Contents { get => contentStack.Children; }

     public CopyDeckPopup(string clickedDeckName, string clickedDeckDescription)
     {
         BindingContext = new CopyDeckPopupViewModel(clickedDeckName, clickedDeckDescription);
         InitializeComponent();
         Content = contentStack;
     
}

第一个 XAML 我在第二个 Label

时出错

The property "Content" is set more than once

我可以重现该行为。

尽管如此,在我这边,即使 VS 显示 warning/error 如果我 运行 项目它正确部署并显示两个标签。

VS 似乎没有识别出 ContentProperty 正在被 覆盖 ,而是假定您正在尝试将两个 Label 添加到 Content,这是错误的,因为您将它们添加到您的自定义 ContentProperty Contents!

请运行项目,如果部署失败请告诉我...

更新

整个问题在我看来就像是 VS 的局限性。最后这只是一个警告,你应该可以忽略它没有任何危险。

另一方面,如果您选择在您可以编写的代码上设置标签

public CopyDeckPopup(string clickedDeckName, string clickedDeckDescription)
{
    BindingContext = new CopyDeckPopupViewModel(clickedDeckName, clickedDeckDescription);
    InitializeComponent();
    Content = contentStack;
    Contents.Add(new Label() { Text = "ABC" });
    Contents.Add(new Label() { Text = "DEF" });
}

但现在这看起来是多余的,因为据我所知,ContentProperty

的一个属性

XAML processor uses to determine the content property.

Decorating types with ContentPropertyAttribute allows shorter XAML syntax.

如果你在 C# 上编写所有代码,只需使用旧的 Stacklayout 并编写你的代码

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CopyDeckPopup : Rg.Plugins.Popup.Pages.PopupPage
{
    public CopyDeckPopup(string clickedDeckName, string clickedDeckDescription)
    {
        BindingContext = new CopyDeckPopupViewModel(clickedDeckName, clickedDeckDescription);
        InitializeComponent();

        StackLayout contentStack = new StackLayout()
        {
            Spacing = 0,
            Padding = new Thickness(0),
            Orientation = StackOrientation.Vertical,
            Children =
            {
                new Label() { Text = "ABC" },
                new Label() { Text = "ABC" }
            }

        };

        Content = contentStack;
     
}