Xamarin 覆盖ContentView上的内容

Xamarin 覆盖ContentView上的内容,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我正在尝试做一些我认为很简单的事情,但我还没有找到解决办法。 我有一个非常基本的自定义元素,用于在框架中添加厚边框 XAML 代码隐藏 使用Xamarin.Forms; 使用Xamarin.Forms.Xaml; 命名空间ResumeApp.CustomElements { [XamlCompilation(XamlCompilationOptions.Compile)] [ContentProperty(名称(内容))] 公共部分类BetterFrame:Frame { 私有int_厚度=

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

XAML

代码隐藏
使用Xamarin.Forms;
使用Xamarin.Forms.Xaml;
命名空间ResumeApp.CustomElements
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[ContentProperty(名称(内容))]
公共部分类BetterFrame:Frame
{
私有int_厚度=0;
公共int厚度
{
获取{return\u Thickness;}
设置
{
填充=新厚度(值);_厚度=值;
}
}
公众浮标角
{
获取{return InnerFrame.CornerRadius;}
设置
{
InnerFrame.CornerRadius=值;OuterFrame.CornerRadius=值;
}
}
公共新视图内容
{
得到
{
//此处未命中断点
返回(查看)GetValue(ContentProperty);
}
设置
{
//此处未命中断点
SetValue(ContentProperty,value);
}
}
公共颜色InnerColor{get{return InnerFrame.BackgroundColor;}set{InnerFrame.BackgroundColor=value;}}
公共颜色OuterColor{get{return OuterFrame.BackgroundColor;}set{OuterFrame.BackgroundColor=value;}}
公共新颜色BorderColor{get{return InnerFrame.BorderColor;}set{InnerFrame.BorderColor=value;OuterFrame.BorderColor=value;}}
公共BetterFrame()
{
初始化组件();
}
受保护的覆盖无效OnParentSet()
{
base.OnParentSet();
对于(元素父元素=this.Parent;父元素!=null;父元素=父元素.Parent)
{
尝试
{
Color background=Parent.GetPropertyIfSet(BackgroundColorProperty,Color.Transparent);
if(background!=Color.Transparent&&InnerFrame.BackgroundColor!=Color.Transparent)
{
InnerFrame.BackgroundColor=背景;
打破
}
}
抓住
{
}
}
}
}
}

因此,当框架内没有内容时,使用上面的代码,所有内容看起来都与预期的一样,但一旦我添加内容,它就会覆盖内部框架。有没有什么方法可以让我在添加内容时将其添加到内部框架而不是外部框架。我添加了Content属性以尝试捕获设置的内容,但我从未命中设置的断点,因此我认为它没有被使用。

如果在另一个Xaml中添加conetent,它将覆盖内部
帧。因为添加的内容是外部
框架的子视图,Fram只能拥有一个子视图。因此,内部
将被覆盖

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

========================================更新================================

创建自定义的
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>

其效果是:


但是没有办法用自定义元素实现这一点吗?我打算给它添加一些生活质量特性(比如让“角半径”设置两个框架半径,以及将外部填充重命名为边距厚度,并将内部框架默认设置为外部框架父级的背景)@DevlinPaddock Hi,我已经找到了一个可以通过使用
ControlTemplate
实现这一点的方法。我将更新答案。我非常感谢这一点,它完全解决了我的问题。我将进一步研究ControlTemplates。内容实际上是从BindableProperty设置的,所以断点永远不会命中,您必须重写OnPropertyChanged并将名称与“内容”进行比较。
<?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>
<?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>
<local:CustomFrameView>
    <StackLayout BackgroundColor="AliceBlue">
        <Entry x:Name="myentry3"
                Text="Third Entry" />
    </StackLayout>
</local:CustomFrameView>