2017-07-06 12 views
0

Xamarin Formsで2行のグリッドであるカスタム 'GroupBox'コントロールを作成しようとしています。最初の行にはヘッダー付きのボックスが表示されます。ヘッダーには、ラベル付きの背景矩形があります。 2番目の行は、ContentPresenterを介してコンテンツを示し、2番目の行でコンテンツが下になります。Xamarinフォームのcontentpresenterが間違った行になり、電話で空になる

私はWPFでXAMLを正しく実行したと思いますが、これはGroupBox.xamlフォームプレビューアで表示されますが、メインページに追加すると、Groupboxの内容が表示されますプレビューア内の何らかの理由で、2行目の代わりに1行目(ヘッダー)に移動します。

the content is going into the header

私はアンドロイド携帯電話上で実行しようとすると、さらに、それだけで次のようになります。グループボックスはなし、背景色やテキスト

と空白フレームです

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> 

このコードは、メインフォームである:私は、コンテンツには、グリッドがなくても、ラベルをグリッド行を追加することができ、メインページのXAMLで

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

、それがプレビューアで動作します - それでも電話では空白です(これが最も重要です)。

<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フォームプレビューアに表示されますが、メインページに追加するとコンテンツグループボックスの最初の行(ヘッダー)は、プレビューア内の何らかの理由で2番目の行ではなく2番目の行に移動します。

ContentPresenterは、それが一般的ControlTemplateで使用されている、that.In Xamarin.Formsのように動作しません。 ContentPresenterの使用については、this blogを参照してください。

あなたの場合、ContentPresenterを動作させたい場合。代わりにGroupBoxためGridを使用して、あなたは以下のようにGroupBoxためContentViewを使用することができます。

[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

関連する問題