C# Xamarin forms contentpresenter在电话中出现错误行且为空
我正在尝试使用Xamarin Forms创建我的自定义“GroupBox”控件,这是一个只有两行的网格。第一行应该只显示一个带有标题的框。标题有一个带标签的背景矩形。第二行通过C# Xamarin forms contentpresenter在电话中出现错误行且为空,c#,android,xaml,xamarin.forms,contentpresenter,C#,Android,Xaml,Xamarin.forms,Contentpresenter,我正在尝试使用Xamarin Forms创建我的自定义“GroupBox”控件,这是一个只有两行的网格。第一行应该只显示一个带有标题的框。标题有一个带标签的背景矩形。第二行通过ContentPresenter显示内容。内容位于第二行下方 我相信我已经正确地完成了XAML,这是我在WPF中所做的,它显示在GroupBox.XAMLForms Previewer中,但是当我将其添加到主页面时,由于某种原因,在Previewer中,GroupBox的内容会进入第一行(标题),而不是第二行 此外,当我
ContentPresenter
显示内容。内容位于第二行下方
我相信我已经正确地完成了XAML,这是我在WPF
中所做的,它显示在GroupBox.XAML
Forms Previewer中,但是当我将其添加到主页面时,由于某种原因,在Previewer中,GroupBox的内容会进入第一行(标题),而不是第二行
此外,当我尝试在android手机上运行时,它看起来就像这样:
分组框是没有背景颜色或文本的空白框
这是GroupBox“用户控件”的代码
<?xml version="1.0" encoding="UTF-8"?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XForms.GroupBox">
<Grid.RowDefinitions>
<RowDefinition Height="0.2*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Frame Grid.Row="0" Grid.RowSpan="2" Margin="1" OutlineColor="AliceBlue"/>
<BoxView Grid.Row="0" BackgroundColor="Red" Margin="1"/>
<Label Grid.Row="0" TextColor="White" Text="Label!" Margin="2" />
<Grid Grid.Row="1">
<ContentPresenter/>
</Grid>
</Grid>
此代码的主要形式为:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XForms;assembly=XForms"
x:Class="XForms.MainPage">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<local:GroupBox Grid.Row="0">
<Label Text="I'm some content 1"/>
</local:GroupBox>
<local:GroupBox Grid.Row="1">
<Label Text="I'm some content 2"/>
</local:GroupBox>
<local:GroupBox Grid.Row="2">
<Label Text="I'm some content 3"/>
</local:GroupBox>
</Grid>
</Grid>
</ContentPage>
在主页XAML中,我可以向标签中添加一个网格行,即使内容中没有网格,它也可以在预览器中工作——但在手机上仍然是空白的(这是最重要的)
我不知道
框架的作用是什么,但它目前覆盖了BoxView
和标签,因此使它们不可见。注释掉框架
,您将再次看到标签和框视图
我相信我已经正确地完成了XAML,这是我在WPF中的实现方式,它显示在GroupBox.XAML表单预览器中,但当我将其添加到主页面时,出于某种原因,GroupBox的内容会进入预览器中的第一行(标题),而不是第二行
ContentPresenter
不是这样工作的。在Xamarin.Forms中,它通常用于ControlTemplate
。有关ContentPresenter的用法,请参阅
在您的情况下,如果希望ContentPresenter正常工作。对于GroupBox
,您可以使用ContentView
来查看GroupBox
,而不是使用Grid
,如下所示:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class GroupBox : ContentView
{
public GroupBox()
{
InitializeComponent();
}
}
GroupBox.xaml:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Demo.GroupBox">
<ContentView.ControlTemplate>
<ControlTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.2*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<!--<Frame Grid.Row="0" Grid.RowSpan="2" Margin="1" OutlineColor="AliceBlue"></Frame>-->
<BoxView Grid.Row="0" BackgroundColor="Red" Margin="1" />
<Label Grid.Row="0" TextColor="White" Text="Label!" Margin="2" />
<Grid Grid.Row="1">
<ContentPresenter/>
</Grid>
</Grid>
</ControlTemplate>
</ContentView.ControlTemplate>
太好了,它可以在手机上使用,而且他们现在的位置正确。非常感谢。我使用框架在盒子周围创建了一个轮廓,我可能应该在前面提到它。
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Demo.GroupBox">
<ContentView.ControlTemplate>
<ControlTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.2*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<!--<Frame Grid.Row="0" Grid.RowSpan="2" Margin="1" OutlineColor="AliceBlue"></Frame>-->
<BoxView Grid.Row="0" BackgroundColor="Red" Margin="1" />
<Label Grid.Row="0" TextColor="White" Text="Label!" Margin="2" />
<Grid Grid.Row="1">
<ContentPresenter/>
</Grid>
</Grid>
</ControlTemplate>
</ContentView.ControlTemplate>