2017-07-06 94 views
0

我正在Xamarin表格尝试创建我的自定义'GroupBox'控件,这是一个只有两行的网格。第一行应该只显示一个带有标题的框。标题具有带标签的背景矩形。第二行显示的内容通过ContentPresenter内容在第二行下方显示。Xamarin形式contentpresenter进入错误行并在手机上空

我相信我已经正确地完成了XAML,这是我在WPF中的做法,它显示在GroupBox.xaml Forms Previewer中,但是当我将它添加到主页面时,Groupbox的内容就会变成放入第一行(标题),而不是第二行由于某种原因在预览器中。

the content is going into the header

而且,当我尝试在Android手机上运行,​​它只是看起来像这样:

the group boxes are blank frames with no background colour or text

组框是没有背景颜色或文本

空白帧

这是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我可以添加一个网格行的标签,即使在内容没有电网

,它会在预览工作 - 但它仍然是空白的(这是最重要的)。

<local:GroupBox Grid.Row="0"> 
        <Label Text="I'm some content 1" Grid.Row="1"/> 
       </local:GroupBox> 

回答

1

我不知道什么是Frame的,但它是目前覆盖BoxView和标签,从而使他们看不见。注释掉Frame,您将再次看到标签和BoxView

我相信我已经正确地完成了XAML,这是我如何在WPF中执行它,它显示在GroupBox.xaml Forms Previewer中,但是当我将它添加到主页面时,内容Groupbox进入第一行(标题),而不是第二个由于某种原因在预览器中。

ContentPresenter不能像那样工作。在Xamarin.Forms中,它一般用于ControlTemplate。有关ContentPresenter的使用,请参阅this blog

就你而言,如果你想让ContentPresenter工作。除了使用GridGroupBox,你可以使用ContentViewGroupBox象下面这样:

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

+0

大,它工作在手机上,他们是在正确的现在的位置。非常感谢。我正在使用框架围绕框创建一个轮廓,我可能应该在之前提到它。 – pm101