2017-07-06 45 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

相關問題