xamarin 覆盖ContentView上的内容

kadbb459  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(223)

我想做一些我认为很简单的事情,但我还没有找到解决办法。我有一个非常基本的自定义元素,用于在框架内添加厚边框。

XAML

<?xml version="1.0" encoding="UTF-8" ?>
<Frame
    x:Class="ResumeApp.CustomElements.BetterFrame"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Name="OuterFrame"
    HorizontalOptions="FillAndExpand"
    OutlineColor="Black">
    <Frame
        x:Name="InnerFrame"
        HorizontalOptions="FillAndExpand"
        OutlineColor="Black" />
</Frame>

CodeBehind

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace ResumeApp.CustomElements
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    [ContentProperty(nameof(Content))]
    public partial class BetterFrame : Frame
    {
        private int _Thickness = 0;

        public int Thickness
        {
            get { return _Thickness; }
            set
            {
                Padding = new Thickness(value); _Thickness = value;
            }
        }

        public float Corner
        {
            get { return InnerFrame.CornerRadius; }
            set
            {
                InnerFrame.CornerRadius = value; OuterFrame.CornerRadius = value;
            }
        }

        public new View Content
        {
            get
            {
//Breakpoint not hit here
                return (View)GetValue(ContentProperty);
            }
            set
            {
//breakpoint not hit here
                SetValue(ContentProperty, value);
            }
        }

        public Color InnerColor { get { return InnerFrame.BackgroundColor; } set { InnerFrame.BackgroundColor = value; } }
        public Color OuterColor { get { return OuterFrame.BackgroundColor; } set { OuterFrame.BackgroundColor = value; } }
        public new Color BorderColor { get { return InnerFrame.BorderColor; } set { InnerFrame.BorderColor = value; OuterFrame.BorderColor = value; } }

        public BetterFrame()
        {
            InitializeComponent();
        }

        protected override void OnParentSet()
        {
            base.OnParentSet();

            for (Element Parent = this.Parent; Parent != null; Parent = Parent.Parent)
            {
                try
                {
                    Color background = Parent.GetPropertyIfSet<Color>(BackgroundColorProperty, Color.Transparent);
                    if (background != Color.Transparent && InnerFrame.BackgroundColor != Color.Transparent)
                    {
                        InnerFrame.BackgroundColor = background;
                        break;
                    }
                }
                catch
                {
                }
            }
        }
    }
}

所以使用上面的代码,当框架内没有内容时,一切看起来都像预期的那样,但是一旦我添加内容,它就会覆盖InnerFrame。有没有什么方法可以让我在添加内容的时候把它添加到内部框架而不是外部框架。我添加了内容属性,试图捕捉它被设置,但我从来没有击中断点设置,所以我不认为它被使用。

w41d8nur

w41d8nur1#

在另一个Xaml控件中添加内容将覆盖内部的Frame;因为添加的内容是外部Frame的子视图,Frame只能拥有一个子视图。所以内部的Frame将被覆盖。

<local:BetterFrame>
    <StackLayout BackgroundColor="AliceBlue">
        <Entry x:Name="myentry2"
                Text="Second Entry" />
    </StackLayout>
</local:BetterFrame>

这里它不会显示内部的Frame

因此,如果您直接在BetterFrame中添加内容,它将显示。

<?xml version="1.0" encoding="utf-8" ?>
<Frame xmlns="http://xamarin.com/schemas/2014/forms"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       xmlns:d="http://xamarin.com/schemas/2014/forms/design"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       mc:Ignorable="d"
       x:Name="OuterFrame"
       x:Class="AppEntryTest.BetterFrame"
       HorizontalOptions="FillAndExpand"
       OutlineColor="Black">
    <Frame x:Name="InnerFrame"
           HorizontalOptions="FillAndExpand"
           OutlineColor="Black">
        <StackLayout BackgroundColor="AliceBlue">
            <Entry x:Name="myentry2"
                   Text="Second Entry" />
        </StackLayout>
    </Frame>
</Frame>

效果:

=====================================================================================
创建一个自定义ContentView,包含外框内框

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="AppEntryTest.Controls.CustomFrameView">
  <ContentView.ControlTemplate>
        <ControlTemplate >
            <Frame x:Name="FrameExtContainer"
                   Padding="5"
                   HasShadow="False"
                   HorizontalOptions="FillAndExpand"
                   CornerRadius="5"
                   OutlineColor="Black"
                   BorderColor="Black">
                <Frame x:Name="FrameIntContainer"
                       Padding="15"
                       Margin="12"
                       HasShadow="False"
                       HorizontalOptions="FillAndExpand"
                       CornerRadius="5"
                       OutlineColor="Black"
                       BorderColor="Black">
                    <ContentPresenter />
                </Frame>
            </Frame>
        </ControlTemplate>
    </ContentView.ControlTemplate>
</ContentView>

现在用另一个Xaml就可以工作了:

<local:CustomFrameView>
    <StackLayout BackgroundColor="AliceBlue">
        <Entry x:Name="myentry3"
                Text="Third Entry" />
    </StackLayout>
</local:CustomFrameView>

效果:

相关问题