2016-05-24 15 views
0

私はXamarin Forms Appを持っています。すべてのページに標準(サブ)ヘッダを入れたいと思います。サブヘッダには、ページごとに変更されるラベルとオプションの戻るボタンが含まれ、メニューのハンバーガーとアプリケーション名を含むAppヘッダーのすぐ下に表示されます。xamarin forms appサブヘッダ

最初に、標準コントロールのレイアウトとこのページから継承したすべてのページを含む「ヘッダーページ」を作成しました。

 HeaderLabel = "Contact Us"; 

:HeaderPageから継承されたページがちょうどHeaderPageコンテンツ

private ContentView _headerPageContent; 
    public new View Content 
    { 
     set { _headerPageContent.Content = value; } 
    } 

と子ページを設定する=コンテンツ設定することができますので、私はただのテキストを設定する必要がありましたContentPage.Contentを「隠し」これは、ContentPage.Contentをマスクする機能が削除されたForms 2.1までは素晴らしい結果でした。

その時、ヘッダーを持つControlTemplateでContentPageを使用することをお勧めしました。私の同僚と同じように、私はこの道を模索しました。そして、コードの複雑さと重複のために、すべてのページににバインディングを置いていたので、私たちはどちらも後退しました。

私は、見出しをビューにすること、またはそれをコントロールにするために何が必要かを検討しました。

今のところ、「Content」変数の名前を「HeaderPageContent」に変更したので、ヘッダーページから継承したすべてのページでContentではなくHeaderPageContentが設定されます。

誰か他にこれに類似したことはありますか?何を見つけましたか?

答えて

0

私は自分のアプリで同じ問題を抱えていて、あなたと同じような解決策を見つけました。

ここでは、XAML

があるXamarin.Forms.Page

- In that class create a `property` named `PageContent` typeof `View`.(Content will be there) 
- (optional)Override the default attribute for class: `[Xamarin.Forms.ContentProperty("PageContent")]` so you will not be have to annotate into xaml each time. 
- Override `OnParentSet` method like you want to style the page. 

サンプルクラス&使用

// PageBaseクラス

から
namespace Test.Views.Base 
{ 
    [Xamarin.Forms.ContentProperty("PageContent")] 
    public class PageBase: Xamarin.Foorms.Page 
    { 

     private Grid _mainLayout; 
     private Label _headerLabel; 
     private Label _footerLabel; 

     public View PageContent{get;set;} 

     public string HeaderText{get;set;} // you can make this property bindable 
     public string FooterText{get;set;} // you can make this property bindable 

     public PageBase():base() 
     { 
      _mainLayout = new Grid(); 
      _mainLayout.RowDefinitions.Add(new RowDefinition(){Height= new GridLenght(50, GridUnitType.Absolute)}); //Header 
      _mainLayout.RowDefinitions.Add(new RowDefinition(){Height= new GridLenght(1, GridUnitType.Star)}); //Content 
      _mainLayout.RowDefinitions.Add(new RowDefinition(){Height= new GridLenght(50, GridUnitType.Absolute)}); //Footer 

      _headerLabel = new Label(); 
      _footerLabel = new Label(); 

      _mainLayout.Children.Add(_headerLabel); 
      _mainLayout.Children.Add(_footerLabel); 

      Grid.SetRow(_footerLabel,2); 
     } 

     protected override void OnParentSet() 
     { 
      base.OnParentSet(); 

      if(PageContent != null) 
      { 
       _mainLayout.Children.Add(PageContent); 
       Grid.SetRow(PageContent,1); 
       this.Content= _mainLayout; 
      } 

      _headerLabel.Text = HeaderText; 
      _footerLabel.Text = FooterText; 
     } 
    } 
} 

を継承PageBaseクラスを作成します。

<?xml version="1.0" encoding="utf-8" ?> 
<local:PageBase xmlns="http://xamarin.com/schemas/2014/forms" 
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
       xmlns:local="clr-namespace:Test.Views.Base;assembly=Test" 
       xmlns:cell="clr-namespace:Test.Views;assembly=Test" 
       x:Class="Test.Views.TestPage" 
       HeaderText="Page Header" 
       FooterText="Page Footer" 
       > 

    <local:PageBase.PageContent> 
    <!-- Here Goes Page Content --> 
    </local:PageBase.PageContent> 
</local:PageBase> 
関連する問題